题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2120
#include <bits/stdc++.h>
using namespace std;
const int N = 10007;
int n, c[N], sum[N*100], ans[N], belong[N], BLOCK, res = 0; //ans答案数组,totq总询问,totch总修改,res当前答案.sum[c[i]]当前颜色总数
struct node1
{
int l, r, cnt, id;
bool operator < (const node1 &x) const {
return belong[l] < belong[x.l] || (belong[l]==belong[x.l] && belong[r] < belong[x.r]) || (belong[l]==belong[x.l] && belong[r] == belong[x.r] && cnt < x.cnt);
}
}Q[N];
struct node2
{
int x, y;
}C[N];
void add(int p)
{
sum[c[p]] ++;
if(sum[c[p]] == 1) res ++;
}
void dec(int p)
{
sum[c[p]] --;
if(sum[c[p]] == 0) res --;
}
void update(int now, int i)
{
if(C[now].x >= Q[i].l && C[now].x <= Q[i].r) {
if(--sum[c[C[now].x]] == 0) res --;
if(++sum[C[now].y] == 1) res ++;
}
swap(c[C[now].x], C[now].y); //重点
}
int main()
{
char opt[3];
int totq = 0, totch = 0, q;
scanf("%d%d",&n,&q); BLOCK = sqrt(n);
for(int i = 1; i <= n; i ++) scanf("%d",&c[i]);
for(int i = 1; i <= n; i ++) belong[i] = (i - 1) / BLOCK + 1;
for(int i = 1; i <= q; i ++) {
int x, y;
scanf("%s%d%d",opt,&x,&y);
if(opt[0] == 'Q') {
totq ++;
Q[totq].l = x, Q[totq].r = y, Q[totq].id = totq, Q[totq].cnt = totch;
} else {
totch ++;
C[totch].x = x, C[totch].y = y;
}
}
sort(Q+1, Q+1+totq);
for(int i = 1, l = 1, r = 0, now = 0;i <= totq;i ++) {
while(Q[i].r > r) add(++r);
while(Q[i].r < r) dec(r--);
while(Q[i].l > l) dec(l++);
while(Q[i].l < l) add(--l);
while(Q[i].cnt > now) update(now + 1, i), now ++;
while(Q[i].cnt < now) update(now, i), now --;
ans[Q[i].id] = res;
}
for(int i = 1; i <= totq; i ++) printf("%d\n", ans[i]);
return 0;
}