結果

問題 No.876 Range Compress Query
ユーザー lumc_lumc_
提出日時 2019-09-07 00:21:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,541 bytes
コンパイル時間 1,108 ms
コンパイル使用メモリ 105,968 KB
実行使用メモリ 4,648 KB
最終ジャッジ日時 2023-09-07 04:11:33
合計ジャッジ時間 3,202 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 63 ms
4,380 KB
testcase_12 AC 54 ms
4,376 KB
testcase_13 AC 53 ms
4,380 KB
testcase_14 AC 64 ms
4,384 KB
testcase_15 AC 48 ms
4,376 KB
testcase_16 AC 63 ms
4,588 KB
testcase_17 AC 64 ms
4,648 KB
testcase_18 AC 69 ms
4,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// .add(i, v)   : bit[i] += v
// .get(i)      : bit[i]
// .sum(i)      : bit[0] + ... + bit[i]
// .range(l, r) : bit[l] + ... + bit[r]
// .lower_bound(T v) : min i that satisfies .sum(i) >= v
//    - use only when bit[i] >= 0 for all i > 0
/// --- Binary Indexed Tree {{{ ///
#include <cassert>
#include <vector>
template < class T = long long >
struct BinaryIndexedTree {
  using size_type = std::size_t;
  size_type n, m;
  T identity;
  std::vector< T > data;
  BinaryIndexedTree() : n(0) {}
  BinaryIndexedTree(int n, T identity = T())
    : n(n), identity(identity), data(n, identity) {
      m = 1;
      while(m < n) m <<= 1;
    }
  void add(size_type i, T x) {
    assert(i < n);
    i++;
    while(i <= n) {
      data[i - 1] = data[i - 1] + x;
      i += i & -i;
    }
  }
  void set(size_type i, T x) {
    add(i, x - get(i));
  }
  T sum(int i) {
    if(i < 0) return identity;
    if(i >= n) i = n - 1;
    i++;
    T s = identity;
    while(i > 0) {
      s = s + data[i - 1];
      i -= i & -i;
    }
    return s;
  }
  T get(int i) { return sum(i) - sum(i - 1); }
  T range(int a, int b) { return sum(b) - sum(a - 1); }
  size_type lower_bound(T w) {
    size_type i = 0;
    for(size_type k = m; k > 0; k >>= 1) {
      if(i + k <= n && data[i + k - 1] < w) w -= data[(i += k) - 1];
    }
    return i;
  }
};
/// }}}--- ///

template < class T = long long >
using BIT = BinaryIndexedTree< T >;

int n, q;
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> q;
  BIT<> b1(n + 1), b2(n + 1);
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    b2.add(i, a - (i - 1 >= 0 ? b2.sum(i-1) : 0));
  }
  for(int i = 0; i < n; i++) {
    b1.add(i, b2.sum(i) != b2.sum(i + 1));
  }
  for(int i = 0; i < q; i++) {
    int t, l, r;
    cin >> t >> l >> r;
    l--, r--;
    if(t == 1) {
      int x;
      cin >> x;
      b2.add(l, x);
      b2.add(r + 1, -x);
      if(l-1 >= 0) b1.set(l-1, b2.sum(l-1) != b2.sum(l));
      b1.set(r, b2.sum(r) != b2.sum(r + 1));
    } else {
      auto x = b1.get(r);
      b1.set(r, 1);
      cout << b1.range(l, r) << "\n";
      b1.set(r, x);
    }

  }
  return 0;
}
0