题解:P11872 异或盒子 1
比赛结束后几秒钟调出来的,提交失败,写个题解纪念一下。
考虑每次“加一”操作对每个二进制位的影响(代码中的
在此基础上,可以动态维护
时空复杂度
#include <array>
#include <iostream>
#include <vector>
#define endl '\n'
using namespace std;
int n, q;
vector<int> v;
vector<array<int, 20>> changes;
void add_num(int u, int o = 0)
{
for(int i = 0; i < 20; ++i) {
int j = o + (-u & ((1 << i) - 1));
if(j > q) break;
++changes[j][i]; // 自然溢出
}
}
void update_changes(int i)
{
for(int j = 0; j < 20; ++j) {
if(i - (1 << j) >= 0) changes[i][j] += changes[i - (1 << j)][j]; // 自然溢出
}
}
int main()
{
int ans = 0;
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
v.resize(n);
changes.assign(q + 1, { 0 });
for(int &u : v) cin >> u, add_num(u), ans ^= u;
int s = 0;
for(int i = 0; i < q; ++i) {
int o, x, y;
cin >> o;
if(o == 0) {
cin >> x >> y, --x;
add_num(v[x] + s, s);
ans ^= v[x] + s;
v[x] = y - s;
ans ^= v[x] + s;
add_num(v[x] + s, s);
} else if(o == 1) {
update_changes(++s);
for(int j = 0; j < 20; ++j) ans ^= (changes[s][j] & 1) << j;
} else {
cout << ans << endl;
}
}
return 0;
}