結果

問題 No.876 Range Compress Query
ユーザー SSRSSSRS
提出日時 2021-12-18 18:30:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 2,458 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 194,452 KB
実行使用メモリ 6,468 KB
最終ジャッジ日時 2023-10-13 18:00:08
合計ジャッジ時間 5,598 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 230 ms
6,016 KB
testcase_12 AC 191 ms
5,972 KB
testcase_13 AC 192 ms
5,940 KB
testcase_14 AC 226 ms
6,204 KB
testcase_15 AC 162 ms
5,900 KB
testcase_16 AC 221 ms
6,256 KB
testcase_17 AC 222 ms
6,468 KB
testcase_18 AC 236 ms
6,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
struct dual_segment_tree{
  int N;
  vector<long long> ST;
  dual_segment_tree(vector<int> &A){
    int N2 = A.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<long long>(N * 2 - 1, 0);
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i] = A[i];
    }
  }
  long long operator [](int k){
    k += N - 1;
    long long ans = ST[k];
    while (k > 0){
      k = (k - 1) / 2;
      ans += ST[k];
    }
    return ans;
  }
  void range_add(int L, int R, int x, int i, int l, int r){
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      ST[i] += x;
    } else {
      int m = (l + r) / 2;
      range_add(L, R, x, i * 2 + 1, l, m);
      range_add(L, R, x, i * 2 + 2, m, r);
    }
  }
  void range_add(int L, int R, int x){
    range_add(L, R, x, 0, 0, N);
  }
};
struct binary_indexed_tree{
  int N;
  vector<int> BIT;
  binary_indexed_tree(vector<int> &A){
    N = A.size();
    BIT = vector<int>(N + 1, 0);
    for (int i = 0; i < N; i++){
      BIT[i + 1] += A[i];
    }
    for (int i = 1; i < N; i++){
      if (i + (i & -i) <= N){
        BIT[i + (i & -i)] += BIT[i];
      }
    }
  }
  void add(int i, int x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  int sum(int i){
    int ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  int sum(int L, int R){
    return sum(R) - sum(L);
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
  }
  vector<int> b(N - 1, 0);
  for (int i = 0; i < N - 1; i++){
    if (a[i] != a[i + 1]){
      b[i]++;
    }
  }
  dual_segment_tree ST(a);
  binary_indexed_tree BIT(b);
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int l, r, x;
      cin >> l >> r >> x;
      l--;
      if (l > 0){
        if (ST[l] != ST[l - 1]){
          BIT.add(l - 1, -1);
        }
      }
      if (r < N){
        if (ST[r] != ST[r - 1]){
          BIT.add(r - 1, -1);
        }
      }
      ST.range_add(l, r, x);
      if (l > 0){
        if (ST[l] != ST[l - 1]){
          BIT.add(l - 1, 1);
        }
      }
      if (r < N){
        if (ST[r] != ST[r - 1]){
          BIT.add(r - 1, 1);
        }
      }
    }
    if (t == 2){
      int l, r;
      cin >> l >> r;
      l--;
      cout << BIT.sum(l, r - 1) + 1 << endl;
    }
  }
}
0