結果

問題 No.876 Range Compress Query
ユーザー koi_kotyakoi_kotya
提出日時 2019-09-06 22:15:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 2,768 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 173,152 KB
実行使用メモリ 12,920 KB
最終ジャッジ日時 2023-09-07 00:42:46
合計ジャッジ時間 6,034 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 1 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,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 309 ms
12,368 KB
testcase_12 AC 258 ms
12,176 KB
testcase_13 AC 254 ms
12,324 KB
testcase_14 AC 308 ms
12,360 KB
testcase_15 AC 215 ms
12,788 KB
testcase_16 AC 303 ms
12,920 KB
testcase_17 AC 298 ms
12,788 KB
testcase_18 AC 321 ms
12,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair<ll,ll> P;

#define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

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

    void eval(int k,int l,int r) {
        if (lazy[k] != 0) {
            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 (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            lazy[k] += (r-l)*x;
            eval(k,l,r);
        } else {
            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 getsum(int a,int b,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (b <= l || r <= a) return 0;
        eval(k,l,r);
        if (a <= l && r <= b) return node[k];
        ll vl = getsum(a,b,k*2+1,l,(l+r)/2);
        ll vr = getsum(a,b,2*k+2,(l+r)/2,r);
        return vl+vr;
    }
};

int main() {
    int n,q;
    cin >> n >> q;
    vector<ll> a(n);
    for (int i = 0;i < n;++i) scanf("%lld",&a[i]);
    LazySegmentTree val(a),ans(vector<ll>(n,0));
    for (int i = 0;i < n-1;++i) if (a[i] != a[i+1]) ans.add(i,i+1,1);
    for (int z = 0;z < q;++z) {
        int c,l,r,x;
        cin >> c >> l >> r;
        if (c == 1) {
            cin >> x;
            val.add(l-1,r,x);
            ll l1,l2,r1,r2;
            if (l > 1) {
                l1 = val.getsum(l-2,l-1);
                r1 = val.getsum(l-1,l);
                if (l1 == r1-x) ans.add(l-2,l-1,1);
                else if (l1 == r1) ans.add(l-2,l-1,-1);
            }
            if (r < n) {
                l2 = val.getsum(r-1,r);
                r2 = val.getsum(r,r+1);
                if (l2-x == r2) ans.add(r-1,r,1);
                else if (l2 == r2) ans.add(r-1,r,-1);
            }
        } else {
            cout << ans.getsum(l-1,r-1)+1 << "\n";
        }
    }
    return 0;
}
0