結果

問題 No.876 Range Compress Query
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-09-07 20:26:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 2,612 bytes
コンパイル時間 3,070 ms
コンパイル使用メモリ 172,348 KB
実行使用メモリ 4,964 KB
最終ジャッジ日時 2023-09-09 10:38:59
合計ジャッジ時間 5,501 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 216 ms
4,620 KB
testcase_12 AC 180 ms
4,692 KB
testcase_13 AC 180 ms
4,564 KB
testcase_14 AC 215 ms
4,768 KB
testcase_15 AC 152 ms
4,572 KB
testcase_16 AC 207 ms
4,948 KB
testcase_17 AC 209 ms
4,964 KB
testcase_18 AC 220 ms
4,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

template<class Monoid> struct SegmentTree {
private:
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int n;
    vector<Monoid> node,cnt0;
public:
    //m=size, f=[](M a,M b){return ;}
    SegmentTree(int m, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        n = 1; while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
    }

    SegmentTree(const vector<Monoid>& v, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
        int sz = v.size();
        n = 1; while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        REP(i, sz) {
            if (v[i] != 0) node[i + n - 1] = 1;
        }
        REPR(i, n - 2) {
            node[i] = F(node[2 * i + 1], node[2 * i + 2]);
        }
    }
    // 0-indexed
    void update(int x, int val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        if (val == 0) node[x] = 0;
        else node[x] = 1;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    // [a,b)
    Monoid get(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return 0;
        if (a <= l && r <= b) return node[k];
        int vl = get(a, b, 2 * k + 1, l, (r - l) / 2 + l);
        int vr = get(a, b, 2 * k + 2, (r - l) / 2 + l, r);
        return F(vl,vr);
    }

    Monoid operator[](int k) const {
        if (0 <= k || k < n) return node[n + k - 1];
        else return UNITY;
    }
};

int main() {
    int n,q; cin >> n >> q;
    vector<int> a(n),b(n-1);
    REP(i, n) cin >> a[i];
    REP(i, n - 1) b[i] = a[i + 1] - a[i];
    SegmentTree<int> seg(b, [](int a, int b){ return a+b; }, 0);
    REP(i, q) {
        int t, l, r; cin >> t >> l >> r;
        l--; r--;
        if (t == 1) {
            int x; cin >> x;
            if (l > 0) {
                seg.update(l - 1, b[l-1] + x);
                b[l - 1] += x;
            }
            if (r < n - 1) {
                seg.update(r, b[r] - x);
                b[r] -= x;
            }
        }
        else {
            cout << seg.get(l, r)+1 << endl;
        }
    }
    getchar(); getchar();
}
0