結果
| 問題 | No.879 Range Mod 2 Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-11 06:12:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,904 bytes |
| 記録 | |
| コンパイル時間 | 2,316 ms |
| コンパイル使用メモリ | 204,920 KB |
| 最終ジャッジ日時 | 2025-01-07 21:29:00 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 3 WA * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<int n>
struct Segt {
int psum[n << 2][2];
int64_t vsum[n << 2];
int64_t atag[n << 2];
int ptag[n << 2];
Segt(vector<int> a) {
a.resize(n);
function<void(int, int, int)> build = [&](int lb, int rb, int t) {
atag[t] = 0;
ptag[t] = 0;
if (rb - lb == 1) {
psum[t][a[lb] & 1] = 1;
psum[t][~a[lb] & 1] = 0;
vsum[t] = a[lb];
return;
}
int mb = lb + rb >> 1;
build(lb, mb, t << 1);
build(mb, rb, t << 1 | 1);
pull(t);
};
build(0, n, 1);
}
void pull(int t) {
for (int i = 0; i < 2; ++i) {
psum[t][i] = psum[t << 1][i] + psum[t << 1 | 1][i];
}
vsum[t] = vsum[t << 1] + vsum[t << 1 | 1];
}
void push(int lb, int rb, int t) {
int mb = lb + rb >> 1;
if (ptag[t]) {
for (int c = 0; c < 2; ++c) {
vsum[t << 1 | c] = psum[t << 1 | c][1];
atag[t << 1 | c] = 0;
ptag[t << 1 | c] = 1;
}
ptag[t] = 0;
}
if (atag[t]) {
vector<int> len({ mb - lb, rb - mb });
for (int c = 0; c < 2; ++c) {
vsum[t << 1 | c] += 1LL * len[c] * atag[t];
atag[t << 1 | c] += atag[t];
if (atag[t] & 1) swap(psum[t << 1 | c][0], psum[t << 1 | c][1]);
}
atag[t] = 0;
}
}
void mod2(int ql, int qr, int lb = 0, int rb = n, int t = 1) {
if (qr <= lb || rb <= ql) return;
if (ql <= lb && rb <= qr) {
vsum[t] = psum[t][1];
atag[t] = 0;
ptag[t] = 1;
return;
}
push(lb, rb, t);
int mb = lb + rb >> 1;
mod2(ql, qr, lb, mb, t << 1);
mod2(ql, qr, mb, rb, t << 1 | 1);
pull(t);
}
void add(int v, int ql, int qr, int lb = 0, int rb = n, int t = 1) {
if (qr <= lb || rb <= ql) return;
if (ql <= lb && rb <= qr) {
vsum[t] += 1LL * (rb - lb) * v;
atag[t] += v;
if (v & 1) swap(psum[t][0], psum[t][1]);
return;
}
push(lb, rb, t);
int mb = lb + rb >> 1;
add(v, ql, qr, lb, mb, t << 1);
add(v, ql, qr, mb, rb, t << 1 | 1);
pull(t);
}
int64_t query(int ql, int qr, int lb = 0, int rb = n, int t = 1) {
if (qr <= lb || rb <= ql) return 0;
if (ql <= lb && rb <= qr) return vsum[t];
push(lb, rb, t);
int mb = lb + rb >> 1;
return query(ql, qr, lb, mb, t << 1) + query(ql, qr, mb, rb, t << 1 | 1);
}
};
int main() {
ios::sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
static Segt<(1 << 17)> tree(A);
while (Q--) {
int P;
cin >> P;
if (P == 1) {
int L, R;
cin >> L >> R;
tree.mod2(L - 1, R);
} else if (P == 2) {
int L, R, X;
cin >> L >> R >> X;
tree.add(X, L - 1, R);
} else {
int L, R;
cin >> L >> R;
cout << tree.query(L - 1, R) << endl;
}
}
return 0;
}