#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1.4e5,M=1e6+10;
int n,m,lt,a[N];
struct nod1 {
int id,l,r,t;
} Q[N];
struct nod2 {
int x,k;
} CH[N];
int tot1,tot2,sum,c[M],ans[N];
bool cmp(nod1 a,nod1 b) {
if((a.l-1)/lt!=(b.l-1)/lt) return a.l<b.l;
if((a.r-1)/lt!=(b.r-1)/lt) {
if((a.l-1)/lt&1)return a.r<b.r;
return a.r>b.r;
}
if((a.r-1)/lt&1)return a.t<b.t;
return a.t>b.t;
}
void del(int x) {
--c[x];
if(!c[x])sum--;
return;
}
void add(int x) {
if(!c[x])sum++;
c[x]++;
return;
}
void change(int pos,int p) {
if(CH[pos].x>=Q[p].l&&CH[pos].x<=Q[p].r) {
del(a[CH[pos].x]);
add(CH[pos].k);
}
swap(a[CH[pos].x],CH[pos].k);
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++)cin>>a[i];
lt=pow(n,0.6667);
for(int i=0; i<m; i++) {
char ch;
cin>>ch;
if(ch=='Q') {
tot1++;
Q[tot1].id=tot1;
cin>>Q[tot1].l>>Q[tot1].r;
Q[tot1].t=tot2;
} else cin>>CH[++tot2].x>>CH[tot2].k;
}
sort(Q+1,Q+1+tot1,cmp);
int l=1,r=0,t=0;
for(int i=1; i<=tot1; i++) {
while(l<Q[i].l)del(a[l++]);
while(l>Q[i].l)add(a[--l]);
while(r<Q[i].r)add(a[++r]);
while(r>Q[i].r)del(a[r--]);
while(t<Q[i].t)change(++t,i);
while(t>Q[i].t)change(t--,i);
ans[Q[i].id]=sum;
}
for(int i=1; i<=tot1; i++)cout<<ans[i]<<'\n';
return 0;
}
结果分别为
4 4 4 4
4 4 3 4