結果
問題 | No.879 Range Mod 2 Query |
ユーザー | tjake |
提出日時 | 2019-09-07 11:47:48 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 380 ms / 3,000 ms |
コード長 | 4,558 bytes |
コンパイル時間 | 946 ms |
コンパイル使用メモリ | 90,484 KB |
実行使用メモリ | 13,820 KB |
最終ジャッジ日時 | 2024-06-26 07:19:16 |
合計ジャッジ時間 | 5,711 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 4 ms
5,376 KB |
testcase_03 | AC | 4 ms
5,376 KB |
testcase_04 | AC | 4 ms
5,376 KB |
testcase_05 | AC | 4 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 4 ms
5,376 KB |
testcase_10 | AC | 4 ms
5,376 KB |
testcase_11 | AC | 366 ms
13,480 KB |
testcase_12 | AC | 216 ms
13,528 KB |
testcase_13 | AC | 265 ms
13,544 KB |
testcase_14 | AC | 249 ms
13,460 KB |
testcase_15 | AC | 253 ms
8,704 KB |
testcase_16 | AC | 356 ms
13,620 KB |
testcase_17 | AC | 369 ms
13,644 KB |
testcase_18 | AC | 380 ms
13,600 KB |
testcase_19 | AC | 326 ms
13,820 KB |
testcase_20 | AC | 331 ms
13,696 KB |
testcase_21 | AC | 347 ms
13,696 KB |
ソースコード
#include<iostream> #include<string> #include<vector> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #include<functional> #include<cstdio> #include<cstdlib> #include<cmath> #include<cassert> #include<ctime> using namespace std; #define mind(a,b) (a>b?b:a) #define maxd(a,b) (a>b?a:b) #define absd(x) (x<0?-(x):x) #define pow2(x) ((x)*(x)) #define rep(i,n) for(int i=0; i<n; ++i) #define repr(i,n) for(int i=n-1; i>=0; --i) #define repl(i,s,n) for(int i=s; i<=n; ++i) #define replr(i,s,n) for(int i=n; i>=s; --i) #define repf(i,s,n,j) for(int i=s; i<=n; i+=j) #define repe(e,obj) for(auto e : obj) #define SP << " " << #define COL << " : " << #define COM << ", " << #define ARR << " -> " << #define PNT(STR) cout << STR << endl #define POS(X,Y) "(" << X << ", " << Y << ")" #define DEB(A) " (" << #A << ") " << A #define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl #define ALL(V) (V).begin(), (V).end() #define INF 1000000007 #define INFLL 1000000000000000007LL #define EPS 1e-9 typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned long long ull; typedef long long ll; typedef long double ld; #define P_TYPE ll typedef pair<P_TYPE, P_TYPE> P; typedef pair<P, P_TYPE> PI; typedef pair<P_TYPE, P> IP; typedef pair<P, P> PP; typedef priority_queue<P, vector<P>, greater<P> > pvqueue; #define N (1 << 17) class SegmentTree { ll data[2*N], lazy[2*N], cnt0[2*N], cnt1[2*N]; bool flag[2*N]; int p[2*N]; int n0, n; void propagate(int k) { if(k >= n0-1) return; if(p[k]) { swap(cnt0[2*k+1], cnt1[2*k+1]); p[2*k+1] ^= 1; swap(cnt0[2*k+2], cnt1[2*k+2]); p[2*k+2] ^= 1; p[k] = 0; } if(flag[k]) { data[2*k+1] = cnt1[2*k+1]; data[2*k+2] = cnt1[2*k+2]; if(lazy[2*k+1] & 1) p[2*k+1] ^= 1; if(lazy[2*k+2] & 1) p[2*k+2] ^= 1; lazy[2*k+1] = lazy[2*k+2] = 0; flag[2*k+1] = flag[2*k+2] = true; flag[k] = false; } if(lazy[k] != 0) { if(lazy[k] & 1) { swap(cnt0[2*k+1], cnt1[2*k+1]); swap(cnt0[2*k+2], cnt1[2*k+2]); } data[2*k+1] += lazy[k] * (cnt0[2*k+1] + cnt1[2*k+1]); data[2*k+2] += lazy[k] * (cnt0[2*k+2] + cnt1[2*k+2]); lazy[2*k+1] += lazy[k]; lazy[2*k+2] += lazy[k]; lazy[k] = 0; } } void build(int k) { cnt0[k] = cnt0[2*k+1] + cnt0[2*k+2]; cnt1[k] = cnt1[2*k+1] + cnt1[2*k+2]; data[k] = data[2*k+1] + data[2*k+2]; } void _set(int a, int b, int k, int l, int r) { propagate(k); if(b <= l || r <= a) { return; } if(a <= l && r <= b) { flag[k] = true; data[k] = cnt1[k]; lazy[k] = 0; return; } _set(a, b, 2*k+1, l, (l+r)/2); _set(a, b, 2*k+2, (l+r)/2, r); build(k); } void _add(ll x, int a, int b, int k, int l, int r) { propagate(k); if(b <= l || r <= a) { return; } if(a <= l && r <= b) { if(x & 1) { swap(cnt0[k], cnt1[k]); } data[k] += x * (cnt0[k] + cnt1[k]); lazy[k] += x; return; } _add(x, a, b, 2*k+1, l, (l+r)/2); _add(x, a, b, 2*k+2, (l+r)/2, r); build(k); } ll _query(int a, int b, int k, int l, int r) { propagate(k); if(b <= l || r <= a) { return 0; } if(a <= l && r <= b) { return data[k]; } ll lv = _query(a, b, 2*k+1, l, (l+r)/2); ll rv = _query(a, b, 2*k+2, (l+r)/2, r); build(k); return lv + rv; } public: SegmentTree(int n, ll *a) { n0 = 1; while(n0 < n) n0 <<= 1; for(int i=0; i<n; ++i) { data[n0-1+i] = a[i]; if(a[i] % 2) { cnt1[n0-1+i] = 1; cnt0[n0-1+i] = 0; } else { cnt1[n0-1+i] = 0; cnt0[n0-1+i] = 1; } } for(int i=n; i<n0; ++i) { data[n0-1+i] = 0; cnt0[n0-1+i] = cnt1[n0-1+i] = 0; } for(int i=0; i<2*n0-1; ++i) lazy[i] = 0, flag[i] = false, p[i] = 0; for(int i=n0-2; i>=0; --i) build(i); } void set(int l, int r) { _set(l, r, 0, 0, n0); } void add(int l, int r, ll x) { _add(x, l, r, 0, 0, n0); } ll query(int l, int r) { return _query(l, r, 0, 0, n0); } }; ll a[N]; int main() { int n, q; cin >> n >> q; rep(i, n) cin >> a[i]; SegmentTree st(n, a); while(q--) { int t, l, r; ll x; cin >> t >> l >> r; switch(t) { case 1: st.set(l-1, r); break; case 2: cin >> x; st.add(l-1, r, x); break; case 3: cout << st.query(l-1, r) << endl; break; } } return 0; }