【STL 薄纱一切】AT_abc330_e
RainRecall · · 题解
事情是这样的,我昨天打开 AT,发现有比赛,但是时间不多了。我前面题都没看,直接开这道题,写了个值域线段树,然后没调出来。
大家都看到了,线段树和平衡树等数据结构是比较容易写挂的,所以我们很容易就联想到去用 STL。观察这题,发现可以用 set 实现。
先想一下我们怎么去暴力的搞:直接开桶记录每一个数出现的次数,从
还有一个优化,就是答案肯定不会超过
这样是 set 进行优化。正难则反,把没出现过的数放到 set 里面。插入时如果当前数的计数器为 erase 掉。同样,删除时如果当前数的计数器为 insert 进去。
然后这题的操作可以拆成一个插入和一个删除,因为 set 自带排序,每次求的答案就是 set 的第一个数。然后这题就做完了。
AC code
#include<bits/stdc++.h>
#define N 200005
using namespace std;
int n,m,x,cnt[N*2],id,a[N*2];
set<int>s;
int main(){
cin>>n>>m;
for(int i=0;i<=N;++i)s.insert(i);
for(int i=1;i<=n;++i){
cin>>a[i];
if(a[i]>n)a[i]=n+1;
if(cnt[a[i]]==0)s.erase(a[i]);
++cnt[a[i]];
}
while(m--){
cin>>id>>x;
if(cnt[a[id]]==1)s.insert(a[id]);
--cnt[a[id]];
if(x>n)x=n+1;
a[id]=x;
if(cnt[x]==0)s.erase(x);
++cnt[x];
cout<<*s.begin()<<'\n';
}
return 0;
}