結果
問題 | No.879 Range Mod 2 Query |
ユーザー | tsutaj |
提出日時 | 2019-09-07 00:15:59 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,846 bytes |
コンパイル時間 | 1,583 ms |
コンパイル使用メモリ | 116,740 KB |
実行使用メモリ | 28,820 KB |
最終ジャッジ日時 | 2024-06-24 22:39:39 |
合計ジャッジ時間 | 7,581 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 517 ms
28,532 KB |
testcase_20 | AC | 497 ms
28,820 KB |
testcase_21 | AC | 548 ms
28,584 KB |
ソースコード
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long int; using int64 = long long int; template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; template <typename MonoidType, typename OperatorType> struct LazySegmentTree { using MMtoM = function< MonoidType(MonoidType, MonoidType) >; using OOtoO = function< OperatorType(OperatorType, OperatorType) >; using MOtoM = function< MonoidType(MonoidType, OperatorType) >; using OItoO = function< OperatorType(OperatorType, int) >; // node, lazy, update flag (for lazy), identity element int n; vector<MonoidType> node; vector<OperatorType> lazy; vector<bool> need_update; MonoidType E0; OperatorType E1; // update / combine / lazy / accumulate function MOtoM upd_f; MMtoM cmb_f; OOtoO lzy_f; OItoO acc_f; void build(int m, vector<MonoidType> v = vector<MonoidType>()) { if(v != vector<MonoidType>()) m = v.size(); n = 1; while(n < m) n *= 2; node = vector<MonoidType>(2*n-1, E0); lazy = vector<OperatorType>(2*n-1, E1); need_update = vector<bool>(2*n-1, false); if(v != vector<MonoidType>()) { for(int i=0; i<m; i++) { node[n-1+i] = v[i]; } for(int i=n-2; i>=0; i--) { node[i] = cmb_f(node[2*i+1], node[2*i+2]); } } } // initialize LazySegmentTree() {} LazySegmentTree(int n_, MonoidType E0_, OperatorType E1_, MOtoM upd_f_, MMtoM cmb_f_, OOtoO lzy_f_, OItoO acc_f_, vector<MonoidType> v = vector<MonoidType>()) : E0(E0_), E1(E1_), upd_f(upd_f_), cmb_f(cmb_f_), lzy_f(lzy_f_), acc_f(acc_f_) { build(n_, v); } void eval(int k, int l, int r) { if(!need_update[k]) return; node[k] = upd_f(node[k], acc_f(lazy[k], r - l)); if(r - l > 1) { lazy[2*k+1] = lzy_f(lazy[2*k+1], lazy[k]); lazy[2*k+2] = lzy_f(lazy[2*k+2], lazy[k]); need_update[2*k+1] = need_update[2*k+2] = true; } lazy[k] = E1; need_update[k] = false; } void update(int a, int b, OperatorType x, int l, int r, int k) { eval(k, l, r); if(b <= l or r <= a) return; if(a <= l and r <= b) { lazy[k] = lzy_f(lazy[k], x); need_update[k] = true; eval(k, l, r); } else { int mid = (l + r) / 2; update(a, b, x, l, mid, 2*k+1); update(a, b, x, mid, r, 2*k+2); node[k] = cmb_f(node[2*k+1], node[2*k+2]); } } MonoidType query(int a, int b, int l, int r, int k) { if(b <= l or r <= a) return E0; eval(k, l, r); if(a <= l and r <= b) return node[k]; int mid = (l + r) / 2; MonoidType vl = query(a, b, l, mid, 2*k+1); MonoidType vr = query(a, b, mid, r, 2*k+2); return cmb_f(vl, vr); } // update [a, b)-th element (applied value, x) void update(int a, int b, OperatorType x) { update(a, b, x, 0, n, 0); } // range query for [a, b) MonoidType query(int a, int b) { return query(a, b, 0, n, 0); } void dump() { fprintf(stderr, "[lazy]\n"); for(int i=0; i<2*n-1; i++) { if(i == n-1) fprintf(stderr, "xxx "); if(lazy[i] == E1) fprintf(stderr, " E "); else fprintf(stderr, "%3d ", lazy[i]); } fprintf(stderr, "\n"); fprintf(stderr, "[node]\n"); for(int i=0; i<2*n-1; i++) { if(i == n-1) fprintf(stderr, "xxx "); if(node[i] == E0) fprintf(stderr, " E "); else fprintf(stderr, "%3d ", node[i]); } fprintf(stderr, "\n"); } }; struct Elem { ll sum, cnt_o, cnt_e, t; Elem() : sum(0), cnt_o(0), cnt_e(0), t(-1) {} Elem(ll a, ll b, ll c, ll d) : sum(a), cnt_o(b), cnt_e(c), t(d) {} void change_sum() { sum = cnt_o; } bool operator==(Elem e) const { bool ok = true; ok &= (sum == e.sum); ok &= (cnt_o == e.cnt_o); ok &= (cnt_e == e.cnt_e); return ok; } }; int main() { int N, Q; cin >> N >> Q; vector<Elem> A(N); for(int i=0; i<N; i++) { int v; cin >> v; A[i].sum = v; if(v % 2) A[i].cnt_o = 1; else A[i].cnt_e = 1; } LazySegmentTree<Elem, Elem> seg(N, Elem(), Elem(), [](Elem a, Elem b) { // たす (b には変化させたい値をいれる) if(b.t == 1 or b.t == 2) { a.sum += b.sum; // mod 2 かわるかも if(b.sum % 2) { swap(a.cnt_o, a.cnt_e); } } // mod 2 おきかえ if(b.t == 0 or b.t == 2) { a.change_sum(); } return a; }, [](Elem a, Elem b) { return Elem(a.sum + b.sum, a.cnt_o + b.cnt_o, a.cnt_e + b.cnt_e, -1); }, [](Elem a, Elem b) { if(a.t < 0) return b; if(a.t == 0 and b.t == 0) return a; if(b.t == 1) { a.sum += b.sum; } bool cont_0 = (a.t == 0 or b.t == 0); bool cont_1 = (a.t == 1 or b.t == 1); if(cont_0 and cont_1) a.t = 2; return a; }, [](Elem a, int x) { a.sum *= x; return a; }, A); while(Q--) { int q; cin >> q; if(q == 1) { int l, r; cin >> l >> r; l--; Elem e(0, 0, 0, 0); seg.update(l, r, e); } if(q == 2) { int l, r, x; cin >> l >> r >> x; l--; Elem e(x, 0, 0, 1); seg.update(l, r, e); } if(q == 3) { int l, r; cin >> l >> r; l--; Elem e = seg.query(l, r); cout << e.sum << endl; } } return 0; }