結果
問題 | No.879 Range Mod 2 Query |
ユーザー | yosupot |
提出日時 | 2019-09-06 22:33:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 242 ms / 3,000 ms |
コード長 | 4,447 bytes |
コンパイル時間 | 2,371 ms |
コンパイル使用メモリ | 208,756 KB |
実行使用メモリ | 22,272 KB |
最終ジャッジ日時 | 2024-06-24 22:16:52 |
合計ジャッジ時間 | 5,819 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 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 | 224 ms
18,304 KB |
testcase_12 | AC | 145 ms
18,560 KB |
testcase_13 | AC | 176 ms
18,048 KB |
testcase_14 | AC | 168 ms
21,120 KB |
testcase_15 | AC | 159 ms
15,872 KB |
testcase_16 | AC | 226 ms
21,760 KB |
testcase_17 | AC | 240 ms
22,272 KB |
testcase_18 | AC | 242 ms
21,376 KB |
testcase_19 | AC | 178 ms
21,760 KB |
testcase_20 | AC | 178 ms
22,144 KB |
testcase_21 | AC | 192 ms
21,504 KB |
ソースコード
//#pragma GCC optimize("Ofast") //#pragma GCC target("avx") //#undef LOCAL #include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #ifdef LOCAL struct PrettyOS { ostream& os; bool first; template <class T> auto operator<<(T&& x) { if (!first) os << ", "; first = false; os << x; return *this; } }; template <class... T> void dbg0(T&&... t) { (PrettyOS{cerr, true} << ... << t); } #define dbg(...) \ do { \ cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \ dbg0(__VA_ARGS__); \ cerr << endl; \ } while (false); #else #define dbg(...) #endif template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << "P(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const V<T>& v) { os << "["; for (auto d : v) os << d << ", "; return os << "]"; } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct Node { using NP = Node*; NP l, r; ll sm, mi, mic, ma, mac; using P = pair<ll, bool>; P lz = P(0LL, false); // x -> (x + lz.first) * (lz.second ? -1 : 1) int sz; void update() { assert(lz == P(0LL, false)); sm = l->sm + r->sm; mi = min(l->mi, r->mi); ma = max(l->ma, r->ma); mic = mac = 0; if (l->mi == mi) mic += l->mic; if (r->mi == mi) mic += r->mic; if (l->ma == ma) mac += l->mac; if (r->ma == ma) mac += r->mac; } void lzdata(P p) { ll x = p.first; sm += x * sz; mi += x; ma += x; lz.first += (lz.second ? -x : x); if (p.second) { sm *= -1; mi *= -1; ma *= -1; swap(mi, ma); lz.second = !lz.second; } } void push() { if (lz != P(0LL, false)) { l->lzdata(lz); r->lzdata(lz); lz = P(0LL, false); } } Node(const V<ll>& v, int _sz, int off) : sz(_sz) { if (sz == 1) { sm = mi = ma = v[off]; mic = mac = 1; return; } l = new Node(v, sz / 2, off); r = new Node(v, sz - sz / 2, off + sz / 2); update(); } void mod2(int a, int b) { if (b <= 0 || sz <= a) return; if (a <= 0 && sz <= b && ma - mi <= 1) { if (mi - mi % 2 == ma - ma % 2) { lzdata(P(mi % 2 - mi, false)); } else { lzdata(P(- mi % 2 - mi, true)); } return; } push(); l->mod2(a, b); r->mod2(a - sz / 2, b - sz / 2); update(); } void add(int a, int b, ll x) { if (b <= 0 || sz <= a) return; if (a <= 0 && sz <= b) { lzdata(P(x, false)); return; } push(); l->add(a, b, x); r->add(a - sz / 2, b - sz / 2, x); update(); } ll que(int a, int b) { if (b <= 0 || sz <= a) return 0; if (a <= 0 && sz <= b) return sm; push(); return l->que(a, b) + r->que(a - sz / 2, b - sz / 2); } ~Node() { if (sz == 1) return; delete l; delete r; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; V<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto tr = new Node(a, n, 0); for (int ph = 0; ph < q; ph++) { int ty, l, r; cin >> ty >> l >> r; l--; if (ty == 1) { tr->mod2(l, r); } else if (ty == 2) { ll x; cin >> x; tr->add(l, r, x); } else { cout << tr->que(l, r) << "\n"; } /* for (int i = 0; i < n; i++) { cout << tr->que(i, i + 1) << " "; } cout << endl;*/ } delete tr; return 0; }