結果
| 問題 |
No.879 Range Mod 2 Query
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2019-09-06 23:17:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 988 ms / 3,000 ms |
| コード長 | 2,528 bytes |
| コンパイル時間 | 2,082 ms |
| コンパイル使用メモリ | 172,804 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-24 22:23:05 |
| 合計ジャッジ時間 | 10,791 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
V<lint> a(n); for (auto&& e : a) cin >> e;
constexpr int B = 512;
V<lint> sum((n + B - 1) / B);
V<lint> oc((n + B - 1) / B);
V<lint> ec((n + B - 1) / B);
bitset<1000> bs;
V<lint> ol((n + B - 1) / B, 1);
V<lint> el((n + B - 1) / B);
V<lint> laz((n + B - 1) / B);
auto push = [&](int k) -> void {
for (int i = k * B; i < min((k + 1) * B, n); ++i) {
if (bs[k]) {
a[i] = a[i] & 1 ? ol[k] : el[k];
} else {
a[i] += laz[k];
}
}
bs[k] = false;
ol[k] = 1;
el[k] = 0;
laz[k] = 0;
};
auto build = [&](int k) -> void {
sum[k] = oc[k] = ec[k] = 0;
for (int i = k * B; i < min((k + 1) * B, n); ++i) {
sum[k] += a[i];
oc[k] += a[i] & 1;
ec[k] += ~a[i] & 1;
}
};
auto act = [&](int l, int r, lint x) -> void {
if (l % B) {
push(l / B);
while (l < r and l % B) {
if (x == -1) a[l] &= 1;
else a[l] += x;
++l;
}
build((l - 1) / B);
}
if (r % B) {
push(r / B);
while (l < r and r % B) {
--r;
if (x == -1) a[r] &= 1;
else a[r] += x;
}
build(r / B);
}
while (l < r) {
if (x == -1) {
sum[l / B] = oc[l / B];
bs[l / B] = true;
ol[l / B] &= 1;
el[l / B] &= 1;
} else {
sum[l / B] += (oc[l / B] + ec[l / B]) * x;
ol[l / B] += x;
el[l / B] += x;
if (x & 1) {
swap(oc[l / B], ec[l / B]);
}
laz[l / B] += x;
}
l += B;
}
};
auto acc = [&](int l, int r) -> lint {
lint res = 0;
if (l % B) {
push(l / B);
while (l < r and l % B) {
res += a[l];
++l;
}
build((l - 1) / B);
}
if (r % B) {
push(r / B);
while (l < r and r % B) {
--r;
res += a[r];
}
build(r / B);
}
while (l < r) {
res += sum[l / B];
l += B;
}
return res;
};
for (int k = 0; k < (n + B - 1) / B; ++k) {
build(k);
}
while (q--) {
int tp; cin >> tp;
int l, r; cin >> l >> r, --l;
if (tp == 1) {
act(l, r, -1);
} else if (tp == 2) {
int x; cin >> x;
act(l, r, x);
} else {
cout << acc(l, r) << '\n';
}
}
}
risujiroh