#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int read() {
int s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s * w;
}
void print(int x) {
if (x < 0) { //判负
putchar('-');
x = -x;
}
if (x >= 10)
print(x / 10);
putchar(x % 10 + '0');
return;
}
struct node {
int L, R, K;
} q[N];
int pos[N], ans[N], cnt[N], a[N];
bool cmp(node a, node b) {
if (pos[a.L] != pos[b.L]) {
return pos[a.L] < pos[b.L];
}
if (pos[a.L] & 1) return a.R > b.R;
return a.R < b.R;
}
int ANS = 0;
void add(int x) {
cnt[a[x]]++;
if (cnt[a[x]] == 1) ANS++;
}
void del(int x) {
cnt[a[x]]--;
if (cnt[a[x]] == 0) ANS--;
}
int main() {
int n = read(),m = read();
int block = sqrt(n);
for (int i = 1; i <= n; i++) {
a[i] = read();
pos[i] = (i - 1) / block + 1;
}
for (int i = 1; i <= m; i++) {
char opr;
cin >> opr;
if(opr == 'Q')
{
q[i].L = read();
q[i].R = read();
q[i].K = i;
}
else
{
int x,k;
cin >> x >> k;
a[x] = k;
}
}
sort(q + 1, q + 1 + m, cmp);
int L = 1, R = 0;
for (int i = 1; i <= m; i++) {
while (L < q[i].L) del(L++);
while (R > q[i].R) del(R--);
while (L > q[i].L) add(--L);
while (R < q[i].R) add(++R);
ans[q[i].K] = ANS;
}
for (int i = 1; i <= m; i++) {
print(ans[i]);
puts("");
}
}
编译时拒绝访问?
玄三关,求帮助