题解 CF940F 【Machine Learning】

· · 题解

这题太**了。。理解题意都用了半天。

此处补一下样例解释:
对于第二次查询,区间[2,8]中出现了:

$1,2,4$都出现了,最小没有出现的正整数是$3$,所以第二个询问输出`3`。 **** 下面是题解: 考虑莫队,我们需要写一个update函数,用来维护区间中增加/减少一个数带来的影响。 首先开一个数组$\text{cnt}_1$,其中$\text{cnt}_1[i]$表示原数组区间中数字$i$的出现次数,在update的时候对应项$+1/-1$即可。 然后再开一个数组$\text{cnt}_2$,$\text{cnt}_2[i]$表示$\text{cnt}_1$中$i$的出现次数。 现在要做的,就是在$\text{cnt2}$中求$\text{mex}$了。 在$\text{cnt}_1$中删除了一个数$x$时(也就是$\text{cnt}_2[x]$减少了$1$),若$\text{cnt}_2[x]$为$0$,那说明$x$在$\text{cnt}_1$中没有出现过。这时需要把答案和$x$取$\min$。 类似的,若$\text{cnt}_1$中多了一个数$x$,就暴力地找比$x$大且最小的、没有在$\text{cnt}_1$中出现过的数。 剩下的,就是写带修莫队的板子上去。注意$a_i$的范围$[1,10^9]$,需要离散化一下。 我在这里原本用的是`unordered_map`,然后就完美TLE了。。这里一定要注意。 你也许会说:“每次暴力地找$\text{mex}$,时间复杂度显然为$\text{O}(n)$啊?” 这里可以(但我不会)证明,每次update操作是均摊$\Theta(1)$的,每次询问就是均摊$\Theta(n^\frac23)$。 这个好像lxl说过?具体我也找不着了QAQ 参考代码: ```cpp #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #define N 400003 #define reg register using namespace std; struct query{ int l,r,t,id; query(int l=0,int r=0,int t=0,int id=0):l(l),r(r),t(t),id(id){} }; struct change{ int pos,last,next; change(int pos=0,int last=0,int next=0):pos(pos),last(last),next(next){} }; query q[N]; change c[N]; int a[N],be[N],dt[N],ans[N],cnt[N],lsh[N],mp[N]; int n,m,unit,qc,res,l,r; inline void read(int &x); void print(int x); inline bool cmp(query x,query y); //莫队的询问排序 inline void modify(int i,int t); //修改 inline void update(int i,int t); //更新 void discretization(); //离散化 int main(){ int op,t = 0; read(n),read(m); unit = pow(n,0.666666666); for(reg int i=1;i<=n;++i){ read(a[i]); lsh[i] = dt[i] = a[i]; be[i] = i/unit+1; } for(reg int i=1;i<=m;++i){ read(op),read(l),read(r); if(op==1){ ++qc; q[qc] = query(l,r,t,qc); }else{ c[++t] = change(l,dt[l],r); lsh[n+t] = dt[l] = r; } } discretization(); sort(q+1,q+1+qc,cmp); l = 1; r = t = 0; cnt[0] = n<<2; //一开始全部是0,所以要把cnt[0]设的很大 for(reg int i=1;i<=qc;++i){ while(t<q[i].t) modify(c[t+1].pos,c[t+1].next),++t; while(t>q[i].t) modify(c[t].pos,c[t].last),--t; while(l<q[i].l) update(a[l],-1),++l; while(l>q[i].l) update(a[l-1],1),--l; while(r<q[i].r) update(a[r+1],1),++r; while(r>q[i].r) update(a[r],-1),--r; ans[q[i].id] = res; } for(reg int i=1;i<=qc;++i){ print(ans[i]); putchar('\n'); } return 0; } void discretization(){ sort(lsh+1,lsh+1+n+t); int len = unique(lsh+1,lsh+n+t+1)-lsh-1; for(reg int i=1;i<=n;++i) a[i] = lower_bound(lsh+1,lsh+len+1,a[i])-lsh; for(reg int i=1;i<=t;++i){ c[i].last = lower_bound(lsh+1,lsh+len+1,c[i].last)-lsh; c[i].next = lower_bound(lsh+1,lsh+len+1,c[i].next)-lsh; } } inline void update(int i,int t){ int x = mp[i]; --cnt[x]; if(cnt[x]==0) res = min(res,x); mp[i] += t; if(x+t>=0) ++cnt[x+t]; x = res; while(cnt[x]>0) ++x; //这里是均摊O(1)的 res = x; } inline void modify(int i,int t){ if(l<=i&&i<=r){ update(a[i],-1); update(t,1); } a[i] = t; } inline bool cmp(query x,query y){ if(be[x.l]==be[y.l]){ if(be[x.r]==be[y.r]) return x.t < y.t; return x.r < y.r; } return x.l < y.l; } inline void read(int &x){ x = 0; char c = getchar(); while(c<'0'||c>'9') c = getchar(); while(c>='0'&&c<='9'){ x = (x<<3)+(x<<1)+(c^48); c = getchar(); } } void print(int x){ if(x>9) print(x/10); putchar(x%10+'0'); } ```