题解:
可修改莫队
我们加入一个时间T
然后在排序的时候考虑一下时间
在计算的时候也要考虑
代码:
#include<bits/stdc++.h> using namespace std; const int N=100005; struct Query { int l,r,Tim,id; }q[N]; struct Change { int pos,New,Old; }c[N]; char sign[3]; int n,m,s[N],color[N*100],t,x,y,Time,now[N],unit,Be[N],ans[N],Ans,l=1,r,T; int cmp(Query a,Query b) { if (Be[a.l]!=Be[b.l])return a.l<b.l; if (Be[a.r]!=Be[b.r])return a.r<b.r; return a.Tim<b.Tim; } void revise(int x,int d) { color[x]+=d; if (d>0)Ans+=color[x]==1; if (d<0)Ans-=color[x]==0; } void doit(int x,int d) { if (l<=x&&x<=r) { revise(d,1); revise(s[x],-1); } s[x]=d; } int main() { scanf("%d%d",&n,&m); int unit=pow(n,0.666666); for (int i=1;i<=n;i++) { scanf("%d",&s[i]); now[i]=s[i]; Be[i]=i/unit+1; } for (int i=1;i<=m;i++) { scanf("%s%d%d",&sign,&x,&y); if (sign[0]=='Q')q[++t]=(Query){x,y,Time,t}; if (sign[0]=='R')c[++Time]=(Change){x,y,now[x]},now[x]=y; } sort(q+1,q+t+1,cmp); for (int i=1;i<=t;i++) { while (T<q[i].Tim)doit(c[T+1].pos,c[T+1].New),T++; while (T>q[i].Tim)doit(c[T].pos,c[T].Old),T--; while (l<q[i].l)revise(s[l],-1),l++; while (l>q[i].l)revise(s[l-1],1),l--; while (r<q[i].r)revise(s[r+1],1),r++; while (r>q[i].r)revise(s[r],-1),r--; ans[q[i].id]=Ans; } for (int i=1;i<=t;i++)printf("%d ",ans[i]); }