結果
問題 | No.879 Range Mod 2 Query |
ユーザー | QCFium |
提出日時 | 2020-01-26 13:21:48 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 242 ms / 3,000 ms |
コード長 | 3,009 bytes |
コンパイル時間 | 1,974 ms |
コンパイル使用メモリ | 173,536 KB |
実行使用メモリ | 11,904 KB |
最終ジャッジ日時 | 2024-09-14 10:58:28 |
合計ジャッジ時間 | 5,299 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 234 ms
11,776 KB |
testcase_12 | AC | 138 ms
11,904 KB |
testcase_13 | AC | 178 ms
11,776 KB |
testcase_14 | AC | 157 ms
11,904 KB |
testcase_15 | AC | 161 ms
7,680 KB |
testcase_16 | AC | 228 ms
11,904 KB |
testcase_17 | AC | 237 ms
11,904 KB |
testcase_18 | AC | 242 ms
11,904 KB |
testcase_19 | AC | 162 ms
11,904 KB |
testcase_20 | AC | 161 ms
11,904 KB |
testcase_21 | AC | 172 ms
11,904 KB |
ソースコード
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } struct SegTree { int n; struct Node { int64_t sum; int added; int compressed; int oddcnt; int l; int r; }; std::vector<Node> nodes; SegTree (const std::vector<int> &a) { for (n = 1; n < (int) a.size(); n <<= 1); nodes.resize(n << 1); for (int i = 0; i < (int) a.size(); i++) nodes[i + n] = {a[i], 0, 0, a[i] & 1, i, i + 1}; for (int i = a.size(); i < n; i++) nodes[i + n] = {0, 0, 0, 0, i, i + 1}; for (int i = n - 1; i; i--) fetch(i); } void fetch(int i) { nodes[i].sum = nodes[i << 1].sum + nodes[i << 1 | 1].sum; nodes[i].oddcnt = nodes[i << 1].oddcnt + nodes[i << 1 | 1].oddcnt; nodes[i].l = nodes[i << 1].l; nodes[i].r = nodes[i << 1 | 1].r; } void flush(int i) { if (nodes[i].compressed) { if (nodes[i].compressed & 1) nodes[i].oddcnt = nodes[i].r - nodes[i].l - nodes[i].oddcnt, nodes[i].compressed = 1; else nodes[i].compressed = 2; nodes[i].sum = nodes[i].oddcnt; if (i < n) { nodes[i << 1].compressed += nodes[i].compressed; if (nodes[i << 1].added) { if (nodes[i << 1].added & 1) nodes[i << 1].compressed++; nodes[i << 1].added = 0; } nodes[i << 1 | 1].compressed += nodes[i].compressed; if (nodes[i << 1 | 1].added) { if (nodes[i << 1 | 1].added & 1) nodes[i << 1 | 1].compressed++; nodes[i << 1 | 1].added = 0; } } nodes[i].compressed = 0; } if (nodes[i].added) { if (nodes[i].added & 1) nodes[i].oddcnt = nodes[i].r - nodes[i].l - nodes[i].oddcnt; nodes[i].sum += (int64_t) (nodes[i].r - nodes[i].l) * nodes[i].added; if (i < n) { nodes[i << 1].added += nodes[i].added; nodes[i << 1 | 1].added += nodes[i].added; } nodes[i].added = 0; } } void mod2(int l, int r, int node = 1) { flush(node); if (l >= nodes[node].r || r <= nodes[node].l) return; if (l <= nodes[node].l && r >= nodes[node].r) { nodes[node].compressed = 2; flush(node); } else { mod2(l, r, node << 1); mod2(l, r, node << 1 | 1); fetch(node); } } void add(int l, int r, int val, int node = 1) { flush(node); if (l >= nodes[node].r || r <= nodes[node].l) return; if (l <= nodes[node].l && r >= nodes[node].r) { nodes[node].added = val; flush(node); } else { add(l, r, val, node << 1); add(l, r, val, node << 1 | 1); fetch(node); } } int64_t sum(int l, int r, int node = 1) { flush(node); if (l >= nodes[node].r || r <= nodes[node].l) return 0; if (l <= nodes[node].l && r >= nodes[node].r) return nodes[node].sum; int64_t res = sum(l, r, node << 1) + sum(l, r, node << 1 | 1); fetch(node); return res; } }; int main() { int n = ri(), q = ri(); std::vector<int> a(n); for (auto &i : a) i = ri(); SegTree tree(a); for (int i = 0; i < q; i++) { int t = ri(); int l = ri() - 1; int r = ri(); if (t == 1) tree.mod2(l, r); else if (t == 2) tree.add(l, r, ri()); else if (t == 3) printf("%lld\n", (long long) tree.sum(l, r)); } return 0; }