题解:AT_abc470_c [ABC470C] Inc, Dec, Xor
Wangruofan · · 题解
AT_abc470_c [ABC470C] Inc, Dec, Xor
题意
给定长度为
1.1 x 令
2.2 对于所有
求每次操作后整个序列的异或和。
思路
暴力显然是
注意到序列初始值为
因此,考虑使用 set 动态维护序列中非零值的位置。设
时间复杂度
代码
#include<iostream>
#include<set>
using namespace std;
const int N=5e5+9;
int n,q,a[N],op,x,S;
set<int> s;
int main(){
cin>>n>>q;
for(int i=1;i<=q;++i){
cin>>op;
if(op==1){
cin>>x;
S^=a[x];
++a[x];
S^=a[x];
s.insert(x);
}
else{
S=0;
auto it=s.begin();
while(it!=s.end()){
int pos=*it;
--a[pos];
S^=a[pos];
if(!a[pos]) it=s.erase(it);
else ++it;
}
}
cout<<S<<'\n';
}
return 0;
}