結果
問題 | No.876 Range Compress Query |
ユーザー | yuji9511 |
提出日時 | 2019-09-06 22:01:23 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 219 ms / 2,000 ms |
コード長 | 3,766 bytes |
コンパイル時間 | 1,598 ms |
コンパイル使用メモリ | 167,928 KB |
実行使用メモリ | 8,960 KB |
最終ジャッジ日時 | 2024-06-24 17:53:22 |
合計ジャッジ時間 | 4,335 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
8,952 KB |
testcase_01 | AC | 8 ms
8,832 KB |
testcase_02 | AC | 7 ms
8,828 KB |
testcase_03 | AC | 8 ms
8,952 KB |
testcase_04 | AC | 7 ms
8,832 KB |
testcase_05 | AC | 7 ms
8,832 KB |
testcase_06 | AC | 8 ms
8,960 KB |
testcase_07 | AC | 8 ms
8,960 KB |
testcase_08 | AC | 8 ms
8,832 KB |
testcase_09 | AC | 7 ms
8,832 KB |
testcase_10 | AC | 7 ms
8,832 KB |
testcase_11 | AC | 219 ms
8,952 KB |
testcase_12 | AC | 177 ms
8,960 KB |
testcase_13 | AC | 173 ms
8,832 KB |
testcase_14 | AC | 207 ms
8,832 KB |
testcase_15 | AC | 143 ms
8,960 KB |
testcase_16 | AC | 199 ms
8,828 KB |
testcase_17 | AC | 202 ms
8,948 KB |
testcase_18 | AC | 215 ms
8,832 KB |
ソースコード
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> lpair; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define print2(x,y) cout << (x) << " " << (y) << endl; #define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " \n"[i==n-1];}; struct LazySegmentTree { private: int n; vector<ll> node, lazy; public: LazySegmentTree(vector<ll> v) { int sz = (int)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; eval(k, l, r); if(b <= l || r <= a) return 0; if(a <= l && r <= b) return node[k]; ll vl = getsum(a, b, 2*k+1, l, (l+r)/2); ll vr = getsum(a, b, 2*k+2, (l+r)/2, r); return vl + vr; } }; struct BIT { //1-indexed private: int n,n2; vector<ll> bit; public: BIT(ll N){ n = N; bit.assign(n+1, 0); n2 = 1; while(n2 * 2 <= n) n2 *= 2; } ll sum(ll x){ //[1, x] ll res = 0; for(ll i = x; i > 0; i -= i & -i) res += bit[i]; return res; } void add(ll x, ll v){ if(x == 0) return; for(ll i = x; i <= n; i += i & -i) bit[i] += v; } ll lower_bound(ll w) { if (w <= 0) return 0; ll x = 0; for (ll k = n2; k > 0; k /= 2) { if (x + k <= n && bit[x + k] < w) { w -= bit[x + k]; x += k; } } return x + 1; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N,Q; cin >> N >> Q; vector<ll> v(100010); rep(i,0,N) cin >> v[i]; LazySegmentTree sg(v); BIT bit(100010); rep(i,0,N-1){ if(v[i] != v[i+1]) bit.add(i+1, 1); } while(Q--){ ll n,l,r,x; cin >> n; if(n == 1){ cin >> l >> r >> x; l--; r--; sg.add(l, r+1, x); if(l != 0){ ll v1 = sg.getsum(l-1, l); ll v2 = sg.getsum(l, l+1); ll v3 = bit.sum(l) - bit.sum(l-1); if(v1 == v2){ bit.add(l, -v3); }else{ bit.add(l, -v3+1); } } if(r != N-1){ ll v1 = sg.getsum(r,r+1); ll v2 = sg.getsum(r+1, r+2); ll v3 = bit.sum(r+1) - bit.sum(r); if(v1 == v2){ bit.add(r+1, -v3); }else{ bit.add(r+1, -v3+1); } } }else{ cin >> l >> r; l--; r--; print(1 + bit.sum(r) - bit.sum(l)); } } }