結果

問題 No.876 Range Compress Query
ユーザー kyunakyuna
提出日時 2019-10-06 18:41:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,502 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 73,688 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 04:57:08
合計ジャッジ時間 3,953 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 211 ms
5,376 KB
testcase_12 AC 165 ms
5,376 KB
testcase_13 AC 166 ms
5,376 KB
testcase_14 AC 195 ms
5,376 KB
testcase_15 AC 140 ms
5,376 KB
testcase_16 AC 187 ms
5,376 KB
testcase_17 AC 207 ms
5,376 KB
testcase_18 AC 200 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template<typename Abel> struct BIT {
    const Abel UNITY_SUM = 0;
    vector<Abel> dat;
    BIT(int n) : dat(n, UNITY_SUM) { }  // [0, n)
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i |= i + 1; }
    }
    inline Abel sum(int i) {    // [0, i]
        Abel res = UNITY_SUM;
        while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; }
        return res;
    }
    inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }   // [a, b)
    Abel operator[](int i) { return sum(i, i + 1); }
    friend ostream& operator<<(ostream& os, BIT<Abel> &bit) {
        for (int i = 0; i < bit.dat.size(); i++) os << bit[i] << " ";
        return os;
    }
};

int main() {
    int n, q; cin >> n >> q;
    vector<int> diff(n + 1);
    for (int i = 1; i <= n; i++) {
        int a; cin >> a;
        diff[i - 1] -= a; diff[i] += a;
    }
    BIT<int> bit(n + 1);
    for (int i = 0; i <= n; i++) bit.add(i, diff[i] != 0);
    while (q--) {
        int com; cin >> com;
        if (com == 1) {
            int l, r, x; cin >> l >> r >> x;
            l--;
            bit.add(l, -bit[l]);
            diff[l] -= x;
            bit.add(l, diff[l] != 0);
            bit.add(r, -bit[r]);
            diff[r] += x;
            bit.add(r, diff[r] != 0);
        } else {
            int l, r; cin >> l >> r;
            cout << bit.sum(l, r) + 1 << endl;
        }
    }
    return 0;
}
0