結果
問題 | No.879 Range Mod 2 Query |
ユーザー | fine |
提出日時 | 2019-11-09 20:57:30 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 171 ms / 3,000 ms |
コード長 | 6,923 bytes |
コンパイル時間 | 1,873 ms |
コンパイル使用メモリ | 174,884 KB |
実行使用メモリ | 13,156 KB |
最終ジャッジ日時 | 2024-09-15 04:52:04 |
合計ジャッジ時間 | 5,124 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 164 ms
12,772 KB |
testcase_12 | AC | 97 ms
12,776 KB |
testcase_13 | AC | 121 ms
12,776 KB |
testcase_14 | AC | 110 ms
13,028 KB |
testcase_15 | AC | 108 ms
8,296 KB |
testcase_16 | AC | 149 ms
13,036 KB |
testcase_17 | AC | 171 ms
13,156 KB |
testcase_18 | AC | 169 ms
13,032 KB |
testcase_19 | AC | 150 ms
12,908 KB |
testcase_20 | AC | 148 ms
13,032 KB |
testcase_21 | AC | 158 ms
12,900 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, int>; // 参考: http://kazuma8128.hatenablog.com/entry/2017/12/29/081929 template <class MonoidOp> struct LazySegmentTree { using Tm = typename MonoidOp::Tm; using To = typename MonoidOp::To; int n; int h; vector<Tm> data; vector<To> lazy; LazySegmentTree(int size, Tm initial_data_value = MonoidOp::unit()) { n = 1; h = 0; while (n < size) { h++; n <<= 1; } data.assign((n << 1), initial_data_value); lazy.assign((n << 1), MonoidOp::op_unit()); if (initial_data_value != MonoidOp::unit()) { for (int i = n - 1; i > 0; i--) data[i] = MonoidOp::merge(data[(i << 1)], data[(i << 1) + 1]); } } LazySegmentTree(const vector<Tm>& v) { int size = v.size(); n = 1; h = 0; while (n < size) { h++; n <<= 1; } data.assign((n << 1), MonoidOp::unit()); lazy.assign((n << 1), MonoidOp::op_unit()); for (int i = 0; i < size; i++) data[i + n] = v[i]; for (int i = n - 1; i > 0; i--) data[i] = MonoidOp::merge(data[(i << 1)], data[(i << 1) + 1]); } // https://komiyam.hatenadiary.org/entry/20131202/1385992406 inline int getSegLen(int k) { return n / (1 << (31 - __builtin_clz(k))); } Tm getLeaf(int k) { return query(k, k + 1); } void push(int k) { if (lazy[k] == MonoidOp::op_unit()) return; int seg_len = getSegLen(k); data[k] = MonoidOp::apply(data[k], lazy[k], seg_len); if (seg_len > 1) { lazy[(k << 1)] = MonoidOp::op_merge(lazy[(k << 1)], lazy[k]); lazy[(k << 1) + 1] = MonoidOp::op_merge(lazy[(k << 1) + 1], lazy[k]); } lazy[k] = MonoidOp::op_unit(); } void update(int k) { Tm vl = MonoidOp::apply(data[(k << 1)], lazy[(k << 1)], getSegLen(k << 1)); Tm vr = MonoidOp::apply(data[(k << 1) + 1], lazy[(k << 1) + 1], getSegLen((k << 1) + 1)); data[k] = MonoidOp::merge(vl, vr); } //区間[a, b)に対する更新 void update(int a, int b, To x) { a += n, b += n - 1; for (int i = h; i > 0; i--) { push(a >> i); push(b >> i); } for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = MonoidOp::op_merge(lazy[l], x), l++; if (r & 1) --r, lazy[r] = MonoidOp::op_merge(lazy[r], x); } while (a >>= 1, b >>= 1, a) { if (lazy[a] == MonoidOp::op_unit()) update(a); if (lazy[b] == MonoidOp::op_unit()) update(b); } } //区間[a, b)に対するクエリに答える Tm query(int a, int b) { a += n, b += n - 1; for (int i = h; i > 0; i--) { push(a >> i); push(b >> i); } Tm vl = MonoidOp::unit(), vr = MonoidOp::unit(); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) { vl = MonoidOp::merge(vl, MonoidOp::apply(data[l], lazy[l], getSegLen(l))); l++; } if (r & 1) { --r; vr = MonoidOp::merge(MonoidOp::apply(data[r], lazy[r], getSegLen(r)), vr); } } return MonoidOp::merge(vl, vr); } }; // 以下、MonoidOpの例 template<class U = ll, class V = U> struct RangeSumAdd { using Tm = U; using To = V; static Tm merge(Tm x, Tm y) { return x + y; } static To op_merge(To x, To y) { return x + y; } static Tm apply(Tm vm, To vo, int seg_len) { return vm + vo * seg_len; } static constexpr Tm unit() { return Tm(0); } static constexpr To op_unit() { return To(0); } }; template<class U = ll, class V = U> struct RangeMinUpdate { using Tm = U; using To = V; static Tm merge(Tm x, Tm y) { return min(x, y); } static To op_merge(To x, To y) { return y; } static Tm apply(Tm vm, To vo, int seg_len) { return vo == op_unit() ? vm : vo; } static constexpr Tm unit() { return numeric_limits<Tm>::max(); } static constexpr To op_unit() { return numeric_limits<To>::max(); } }; // 作用素の初期値は更新クエリで与えられる値の定義域の範囲外の値にする template<class U = ll, class V = U> struct RangeSumUpdate { using Tm = U; using To = V; static Tm merge(Tm x, Tm y) { return x + y; } static To op_merge(To x, To y) { return y; } static Tm apply(Tm vm, To vo, int seg_len) { return vo == op_unit() ? vm : vo * seg_len; } static constexpr Tm unit() { return Tm(0); } static constexpr To op_unit() { return numeric_limits<To>::min(); } }; // 初期値 != 単位元 の場合に注意 template<class U = ll, class V = U> struct RangeMinAdd { using Tm = U; using To = V; static Tm merge(Tm x, Tm y) { return min(x, y); } static To op_merge(To x, To y) { return x + y; } static Tm apply(Tm vm, To vo, int seg_len) { return vm + vo; } static constexpr Tm unit() { return numeric_limits<Tm>::max(); } static constexpr To op_unit() { return To(0); } }; template<class U = P, class V = U> struct RangeMod2 { using Tm = U; using To = V; static Tm merge(const Tm& x, const Tm& y) { return Tm(x.first + y.first, x.second + y.second); } static To op_merge(const To& target, const To& x) { if ((x.second & 1) > 0) { return To(x.first, ((target.second ^ x.second) | 1)); } else { return To(target.first + x.first, (target.second ^ x.second)); } } static Tm apply(const Tm& target, const To& x, int seg_len) { Tm res = target; if (((x.second & 2) > 0) ^ (x.first % 2 == 1)) { res.second = seg_len - res.second; } if ((x.second & 1) > 0) { res.first = res.second; } if ((x.first % 2 == 1)) { res.second = seg_len - res.second; } res.first += x.first * seg_len; return res; } static constexpr Tm unit() { return Tm(0, 0); } static constexpr To op_unit() { return To(0, 0); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector<P> v; for (int i = 0; i < n; i++) { ll a; cin >> a; v.emplace_back(a, a % 2); } LazySegmentTree< RangeMod2<> > st(v); for (int i = 0; i < q; i++) { int c, l, r; cin >> c >> l >> r; l--; if (c == 1) { st.update(l, r, P(0, 1)); } else if (c == 2) { ll x; cin >> x; st.update(l, r, P(x, x % 2 * 2)); } else { cout << st.query(l, r).first << "\n"; //cerr << st.query(l, r).second << "\n"; } } return 0; }