提交时间:2023-08-16 12:06:11

运行 ID: 98601

#include<bits/stdc++.h> using namespace std; int n,m; int a[200001]; unordered_map<int,int> ma;int tot,T; int tnow; struct ask{ int l,r,id,time; }q[200001];int cnt; struct add{ int x,c,last; }b[200001]; int vis[200001]; int in[200001]; bool cmp(ask x,ask y){ if(in[x.l]==in[y.l]){ if(in[x.r]==in[y.r]) return x.time<y.time; return x.r<y.r; } return x.l<y.l; } int ans[200001],ansnow; int t[200001]; void Add(int x){ if(!t[x]) ansnow++; t[x]++; } void Del(int x){ if(t[x]==1) ansnow--; t[x]--; } int main(){ // freopen("3.in","r",stdin); // freopen("_.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; if(n<=5000){ for(int i=1;i<=n;i++){ cin>>a[i]; if(!ma[a[i]]) ma[a[i]]=++tot; a[i]=ma[a[i]]; } while(m--){ int op,l,r,c;cin>>op>>l>>r; if(op==1){ cin>>c; if(!ma[c]) ma[c]=++tot; c=ma[c]; for(int i=l;i<=r;i++) a[i]=c; } else{ T++; int ans=0; for(int i=l;i<=r;i++){ if(vis[a[i]]==T) continue; ans++,vis[a[i]]=T; } cout<<ans<<'\n'; } } return 0; } int size=4300; for(int i=1;i<=n;i++){ in[i]=(i-1)/size+1; cin>>a[i]; if(!ma[a[i]]) ma[a[i]]=++tot; a[i]=ma[a[i]]; } for(int i=1;i<=m;i++){ int op;cin>>op; if(op==1){ tnow++; cin>>b[tnow].x>>b[tnow].x>>b[tnow].c; if(!ma[b[tnow].c]) ma[b[tnow].c]=++tot; b[tnow].c=ma[b[tnow].c]; b[tnow].last=a[b[tnow].x]; a[b[tnow].x]=b[tnow].c; } else{ cnt++; cin>>q[cnt].l>>q[cnt].r; q[cnt].id=cnt; q[cnt].time=tnow; } } sort(q+1,q+1+cnt,cmp); int l=1,r=0; for(int i=1;i<=cnt;i++){ while(tnow>q[i].time){ if(l<=b[tnow].x&&b[tnow].x<=r){ Del(a[b[tnow].x]); a[b[tnow].x]=b[tnow].last; Add(a[b[tnow].x]); } else a[b[tnow].x]=b[tnow].last; tnow--; } while(tnow<q[i].time){ tnow++; if(l<=b[tnow].x&&b[tnow].x<=r){ Del(a[b[tnow].x]); a[b[tnow].x]=b[tnow].c; Add(a[b[tnow].x]); } else a[b[tnow].x]=b[tnow].c; } while(r<q[i].r){ r++; Add(a[r]); } while(l>q[i].l){ l--; Add(a[l]); } while(r>q[i].r){ Del(a[r]); r--; } while(l<q[i].l){ Del(a[l]); l++; } ans[q[i].id]=ansnow; } for(int i=1;i<=cnt;i++) cout<<ans[i]<<'\n'; } /* 5 3 1 1 2 3 4 2 2 4 1 2 2 2 2 2 4 */