题解:AT_abc470_c [ABC470C] Inc, Dec, Xor
Cookie_King · · 题解
Problem
给定一个长度为
1 x:将A_x 的值增加1 。2:对于所有的1 \le i \le N ,如果A_i > 0 ,将A_i 减1 。
每次操作后,要求输出
Solution
题外:赛时一直在考虑拆成二进制,但是一直解决不了操作
不难发现,只有进行过操作
为了能快速统计答案,我们需要利用一个定理:任意一个整数疑异或其本身得到的值一定为
复杂度分析:因为操作
Code
AC 记录
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
const int N=500005,M=21;
int n,q,op,x,a[N];
set<int> st;
int main(){
scanf("%d%d",&n,&q);
int ans=0;
for(;q--;){
scanf("%d",&op);
if(op==1){
scanf("%d",&x);
ans^=a[x];//删除旧值
a[x]++;
ans^=a[x];//加入新值
st.insert(x);
}
else{
for(set<int>::iterator it=st.begin();it!=st.end();){
int x=(*it);
ans^=a[x];
a[x]--;
ans^=a[x];
if(!a[*it])//一定要把没用的元素删掉!不然会很容易TLE掉的
it=st.erase(it);//在C++11之后,STL中erase函数会返回删除该值之后下一个值的指针
else
++it;
}
}
printf("%d\n",ans);
}
return 0;
}