結果

問題 No.876 Range Compress Query
ユーザー betrue12betrue12
提出日時 2019-09-06 22:05:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 1,728 bytes
コンパイル時間 1,776 ms
コンパイル使用メモリ 173,844 KB
実行使用メモリ 5,956 KB
最終ジャッジ日時 2023-09-06 23:58:43
合計ジャッジ時間 4,655 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 223 ms
5,228 KB
testcase_12 AC 184 ms
5,224 KB
testcase_13 AC 181 ms
5,100 KB
testcase_14 AC 215 ms
5,288 KB
testcase_15 AC 156 ms
5,680 KB
testcase_16 AC 213 ms
5,560 KB
testcase_17 AC 210 ms
5,956 KB
testcase_18 AC 223 ms
5,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, Q;
    cin >> N >> Q;
    vector<int64_t> A(N), D(N+1);
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=1; i<N; i++) D[i] = A[i] - A[i-1];

    Segtree<int> st(N+1, [](int a, int b){ return a+b; }, 0);
    for(int i=1; i<N; i++) st.update(i, D[i] != 0);
    while(Q--){
        int t, l, r;
        cin >> t >> l >> r;
        l--; r--;
        if(t == 1){
            int x;
            cin >> x;
            D[l] += x;
            D[r+1] -= x;
            st.update(l, D[l] != 0);
            st.update(r+1, D[r+1] != 0);
        }else{
            int ans = st.between(l+1, r) + 1;
            cout << ans << endl;
        }
    }
    return 0;
}
0