結果

問題 No.876 Range Compress Query
ユーザー face4face4
提出日時 2019-09-06 21:55:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 3,233 bytes
コンパイル時間 1,932 ms
コンパイル使用メモリ 82,036 KB
実行使用メモリ 9,888 KB
最終ジャッジ日時 2023-09-06 23:41:26
合計ジャッジ時間 5,198 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 309 ms
9,392 KB
testcase_12 AC 254 ms
9,112 KB
testcase_13 AC 260 ms
9,372 KB
testcase_14 AC 313 ms
9,428 KB
testcase_15 AC 210 ms
9,520 KB
testcase_16 AC 305 ms
9,888 KB
testcase_17 AC 298 ms
9,548 KB
testcase_18 AC 320 ms
9,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

struct STsum{
private:
    int n;
    vector<int> dat;
public:
    STsum(vector<int> ini){
        int siz = ini.size();
        n = 1;
        while(n < siz)   n *= 2;
        dat.resize(2*n-1, 0);
        for(int i = 0; i < siz; i++)    dat[n - 1 + i] = ini[i];
        for(int i = n-2; i >= 0; i--)   dat[i] = dat[2*i+1]+dat[2*i+2];
    }

    void update(int x, int val){
        x += (n-1);
        dat[x] = val;
        while(x > 0){
            x = (x-1)/2;
            dat[x] = dat[2*x+1]+dat[2*x+2];
        }
    }

    // focus on k-th node, who controls [l, r)
    int query(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 dat[k];

        int lx = query(a, b, 2*k+1, l, (l+r)/2);
        int rx = query(a, b, 2*k+2, (l+r)/2, r);
        return lx+rx;
    }
};

class STlazy{
private:
    int n;
    vector<ll> node, lazy;

public:
    STlazy(vector<ll> v){
        int siz = v.size();
        n = 1;
        while(n < siz)  n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        
        for(int i = 0; i < siz; i++)    node[n-1+i] = v[i];
        for(int i = n-2; i >= 0; i--)   node[i] = node[2*i+1] + node[2*i+2];
    }

    void eval(int k, int l, int r){
        if(lazy[k] == 0)    return;
        node[k] += lazy[k];
        if(r-l > 1){
            lazy[2*k+1] += lazy[k]/2;
            lazy[2*k+2] += lazy[k]/2;
        }
        lazy[k] = 0;
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        eval(k, l, r);
        if(r <= a || b <= l)    return;
        if(a <= l && r <= b){
            lazy[k] += (r-l)*x;
            eval(k, l, r);
            return;
        }
        add(a, b, x, 2*k+1, l, (l+r)/2);
        add(a, b, x, 2*k+2, (l+r)/2, r);
        node[k] = node[2*k+1] + node[2*k+2]; // 自分の全ての子孫の値伝播が終わったら自分を更新する
    }

    ll query(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;
        eval(k, l, r); // 最初の呼び出しが必ずeval(0,0,n)となるので漏れなく上から値の伝播がなされている
        if(a <= l && r <= b)    return node[k];
        ll lx = query(a, b, 2*k+1, l, (l+r)/2);
        ll rx = query(a, b, 2*k+2, (l+r)/2, r);
        return lx+rx;
    }
};

int main(){
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for(int i = 0; i < n; i++)  cin >> a[i];
    STlazy lseg(a);
    vector<int> diff(n, 0);
    for(int i = 0; i+1 < n; i++)    diff[i] = a[i]!=a[i+1];
    STsum seg(diff);
    while(q-- > 0){
        int op;
        cin >> op;
        if(op == 1){
            int l, r, x;
            cin >> l >> r >> x; l--, r--;
            lseg.add(l, r+1, x);
            if(l > 0)   seg.update(l-1, lseg.query(l-1,l)!=lseg.query(l,l+1));
            if(r+1 < n) seg.update(r, lseg.query(r,r+1)!=lseg.query(r+1,r+2));
        }else{
            int l, r;
            cin >> l >> r;  l--, r--;
            cout << seg.query(l, r)+1 << endl;
        }
    }
    return 0;
}
0