結果

問題 No.876 Range Compress Query
ユーザー kyort0nkyort0n
提出日時 2019-09-06 21:33:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 2,517 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 169,060 KB
実行使用メモリ 5,328 KB
最終ジャッジ日時 2023-09-06 22:47:14
合計ジャッジ時間 3,716 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 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,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 132 ms
4,932 KB
testcase_12 AC 110 ms
4,812 KB
testcase_13 AC 108 ms
4,948 KB
testcase_14 AC 129 ms
4,888 KB
testcase_15 AC 88 ms
5,328 KB
testcase_16 AC 124 ms
5,224 KB
testcase_17 AC 122 ms
5,212 KB
testcase_18 AC 133 ms
5,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll delta[105000];
int N, Q;
int a[105000];
struct SegmentTree {
private:
    int n;
    vector<int> node;
 
public:
    SegmentTree() {
        int sz = 100050;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) node[i+n-1] = 0;
        for(int i=n-2; i>=0; i--) node[i] = min(node[2*i+1], node[2*i+2]);
    }
 
    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = (node[2*x+1] + node[2*x+2]);
        }
    }
    // hannkaikukann 
    int getsum(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 = getsum(a, b, 2*k+1, l, (l+r)/2);
        int vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return (vl + vr);
    }
};


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) cin >> a[i];
    for(int i = 1; i <= N - 1; i++) {
        delta[i] = a[i+1] - a[i];
    }
    SegmentTree seg;
    for(int i = 1; i <= N - 1; i++) {
        if(delta[i] != 0) {
            seg.update(i, 1);
        }
    }
    while(Q--) {
        int ope;
        cin >> ope;
        if(ope == 2) {
            int l, r;
            cin >> l >> r;
            int ans = seg.getsum(l, r) + 1;
            cout << ans << endl;
            continue;
        } else if(ope == 1) {
            int l, r, x;
            cin >> l >> r >> x;
            if(l > 1) {
                delta[l-1] += x;
                if(delta[l-1] == 0) {
                    seg.update(l-1, 0);
                } else {
                    seg.update(l-1, 1);
                }
            }
            if(r < N) {
                delta[r] -= x;
                if(delta[r] == 0) {
                    seg.update(r, 0);
                } else {
                    seg.update(r, 1);
                }
            }
        }
    }
    return 0;
}
0