就过了#13,#1至#5 WA,#6至#12 TLE
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int s,n,cnt,num,tot,m;
int a[N],b[N],c;
int ans[N];
struct node{
int l,r,time,id;
}z[N],g[N];
bool cmp(node x,node y){
if(x.l/s!=y.l/s){
return x.l<y.l;
}
if(x.r/s!=y.r/s){
return x.r<x.r;
}
return x.time<y.time;
}
/*bool cmp (const node &a, const node &b)
{
return a.l / s == b.l / s ? a.r / s == b.r / s ? a.time < b.time : a.r < b.r : a.l < b.l;
}*/
void add(int x){
if(b[x]==0){
c++;
}
b[x]++;
}
void del(int x){
if(b[x]==1){
c--;
}
b[x]--;
}
void xiu(int i,int x){
if(z[i].l<=g[x].l&&g[x].l<=z[i].r ){
add(g[x].r);
del(a[g[x].l]);
}
swap(a[g[x].l],g[x].r);
//aacout<<a[g[x].l]<<endl;
}
int main(){
scanf("%d%d",&n,&m);
s = pow(n, 0.667);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=m;i++){
int x,y;
char opt;
cin>>opt;
scanf("%d%d",&x,&y);
//cout<<x<<" "<<y<<endl;
if(opt=='Q'){
cnt++;
z[cnt].l=x;
z[cnt].r=y;
z[cnt].id=cnt;
z[cnt].time=tot;
}
else{
tot++;
g[tot].l=x;
g[tot].r=y;
}
}
sort(z+1,z+1+cnt,cmp);
int ll=1,rr=0,tt=0;
for(int i=1;i<=cnt;i++){
int il=z[i].l,ir=z[i].r,it=z[i].time;
while(ll>il){
ll--;
add(a[ll]);
}
while(rr>ir){
del(a[rr]);
rr--;
}
while(ll<il){
del(a[ll]);
ll++;
}
while(rr<ir){
rr++;
add(a[rr]);
}
while(tt<it){
tt++;
xiu(i,a[tt]);
}
while(tt>it){
xiu(i,a[tt]);
tt--;
}
ans[z[i].id]=c;
//cout<<c<<endl;
}
for(int i=1;i<=cnt;i++){
printf("%d\n",ans[i]);
}
}