带修改的莫队板子。听说按n^ 2/3分块更快??其实无非就是多了个修改,我们为每一个询问打上一个时间戳。然后照做无误就好了。只是在每一次做之前,先看看时间戳对不对。。不对的就给他暴力改对。所以对每一个修改我们要记该位置改之前和改之后是什么样子的。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
#define N 200005
using namespace std;
int n,m,a[N],f[1000005],last[N],now=0,tot=0,block,ANS[N],ans=0;
bool vis[N];
struct query{
int l,r,id,time,block;
}q[N];
struct modify{
int pos,x,pre;
}w[N];
inline int read(){
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
bool cmp(query x,query y){
if(x.block==y.block)
return x.r==y.r?x.time<y.time:x.r<y.r;
else return x.block<y.block;
}
inline void update(int x){
if(vis[x]){//在队列中,说明上次询问中有他,这次一定是删他
f[a[x]]--;
if(!f[a[x]]) ans--;
}
else{//add
f[a[x]]++;
if(f[a[x]]==1) ans++;
}
vis[x]^=1;
}
inline void change(int x,int val){
if(vis[x]){//只有上次询问包含的才需要更新ans
update(x);
a[x]=val;
update(x);
}
else a[x]=val;
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;++i) a[i]=read(),last[i]=a[i];
block=sqrt(n);
for(int i=1;i<=m;++i){
char op[10];
scanf("%s",op);
if(op[0]=='Q'){
q[++tot].l=read();q[tot].r=read();
q[tot].id=tot;q[tot].time=now;
q[tot].block=(q[tot].l-1)/block;
}
else{
w[++now].pos=read();w[now].pre=last[w[now].pos];
w[now].x=read();last[w[now].pos]=w[now].x;
}
}
sort(q+1,q+tot+1,cmp);
int l=1,r=0;
for(int i=1;i<=tot;++i){
//按时间把修改做正确。
for(int j=q[i-1].time+1;j<=q[i].time;++j) change(w[j].pos,w[j].x);
for(int j=q[i-1].time;j>q[i].time;--j) change(w[j].pos,w[j].pre);
for(;l<q[i].l;++l) update(l);
for(;l>q[i].l;--l) update(l-1);
for(;r<q[i].r;++r) update(r+1);
for(;r>q[i].r;--r) update(r);
ANS[q[i].id]=ans;
}
for(int i=1;i<=tot;++i) printf("%d\n",ANS[i]);
return 0;
}