結果

問題 No.876 Range Compress Query
ユーザー gasingasin
提出日時 2019-09-07 13:16:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 71,296 KB
実行使用メモリ 5,660 KB
最終ジャッジ日時 2023-09-08 15:45:35
合計ジャッジ時間 4,170 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 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,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 213 ms
5,280 KB
testcase_12 AC 177 ms
5,148 KB
testcase_13 AC 181 ms
5,092 KB
testcase_14 AC 214 ms
5,220 KB
testcase_15 AC 150 ms
5,392 KB
testcase_16 AC 206 ms
5,360 KB
testcase_17 AC 208 ms
5,660 KB
testcase_18 AC 219 ms
5,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string.h>
#define rep(i,n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

// 区間和、要素更新のsegtree

template<typename T>
struct segtree {
    int n;
    vector<T> dat;

    void init(int n_){
        n = 1;
        while(n < n_) n *= 2;
        dat.assign(2*n-1, 0);
    }

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

    //(a,b,0,0,seg.n)で呼ぶ
    T query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a,b,k*2+1,l,(l+r)/2);
            T vr = query(a,b,k*2+2,(l+r)/2,r);
            return vl + vr;
        }
    }
};

segtree<int> seg;
int n, q;
ll a[100000];
ll d[100000];

int main() {
    cin >> n >> q;
    seg.init(n);
    rep(i,n) {
        cin >> a[i];
        if (i > 0) {
            d[i-1] = a[i]-a[i-1];
            if (d[i-1] != 0) seg.update(i-1, 1);
        }
    }
    rep(i,q) {
        int f;
        cin >> f;
        if (f == 1) {
            int l, r, x;
            cin >> l >> r >> x;
            l--; r--;
            if (l > 0) {
                if (d[l-1] != 0) seg.update(l-1, -1);
                d[l-1] += x;
                if (d[l-1] != 0) seg.update(l-1, 1);
            }
            if (r != n-1) {
                if (d[r] != 0) seg.update(r, -1);
                d[r] -= x;
                if (d[r] != 0) seg.update(r, 1);
            }
        } else {
            int l, r;
            cin >> l >> r;
            l--; r--;
            cout << seg.query(l, r, 0, 0, seg.n) + 1 << endl;
        }
    }
}
0