結果

問題 No.876 Range Compress Query
ユーザー goodbatongoodbaton
提出日時 2019-09-06 21:46:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,439 bytes
コンパイル時間 892 ms
コンパイル使用メモリ 97,616 KB
実行使用メモリ 5,624 KB
最終ジャッジ日時 2023-09-06 23:27:04
合計ジャッジ時間 2,667 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
5,624 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 57 ms
4,616 KB
testcase_12 AC 48 ms
4,376 KB
testcase_13 AC 48 ms
4,452 KB
testcase_14 AC 56 ms
4,404 KB
testcase_15 AC 42 ms
4,560 KB
testcase_16 AC 57 ms
4,748 KB
testcase_17 AC 57 ms
4,780 KB
testcase_18 AC 61 ms
4,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Binary Indexed Tree(0-index) */

template <typename Type = int>
struct BIT{
  int bit_size;
  vector<Type> data;

  BIT(int n){
    bit_size = n;
    data.assign(n+2, 0);
  }

  //add x to kth value
  void add(int k, Type x){
    k++;
    while(k <= bit_size){
      data[k] += x;
      k += k & (-k);
    }
  }

  //sum of [0,k)
  Type query(int k){
    Type rec = 0;
    while(k > 0){
      rec += data[k];
      k -= k & (-k);
    }
    return rec;
  }

  //sum of [a, b)
  Type query(int a, int b){
    return query(b) - query(a);
  }
};


int main(){
  int N, Q, a[SIZE];
  ll d[SIZE];

  scanf("%d%d", &N, &Q);

  for(int i=0; i<N; i++) {
    scanf("%d", a+i);
    if(i > 0) d[i-1] = a[i-1] - a[i];
  }

  N--;

  BIT<int> bit(N);

  for(int i=0; i<N; i++)
    bit.add(i, !!d[i]);

  for(int j=0; j<N; j++) debug(d[j]);

  for(int i=0; i<Q; i++) {
    int q, a, b, x;

    scanf("%d", &q);

    if (q == 1) {
      scanf("%d%d%d", &a, &b, &x);
      a--; b--;

      if (a > 0) {
        bit.add(a-1, -!!d[a-1]);
        d[a-1] -= x;
        bit.add(a-1, !!d[a-1]);
      }


      {
        bit.add(b, -!!d[b]);
        d[b] += x;
        bit.add(b, !!d[b]);
      }

      for(int j=0; j<N; j++) debug(d[j]);

    } else {
      scanf("%d%d", &a, &b);
      a--; b--;

      for(int j=0; j<N; j++) debug(bit.query(j, j+1));
      debug(a); debug(b);
      int ans = bit.query(a, b) + 1;
      printf("%d\n", ans);
    }
  }



  return 0;
}
0