Dynamic len(set(a[L:R])) UVA

给出一个有n个元素的数组,有以下两种操作:Q x y,求出区间[x,y)内不同元素的个数,

M x y,把第x个元素的值修改为y。注意题目中的下标是从0开始的

这题超级超级坑

妈的一个水题找了几个小时的BUG

  这样AC
这样WA
 吃一堑长一智  妈的毒瘤啊 
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1e6 + 10;
 5 int n, m, L, R, ans, sz, qsz, tsz;
 6 int a[maxn], ANS[maxn], num[maxn], now[maxn], flag[maxn];
 7 struct node {
 8     int l, r, id, t;
 9     node() {}
10     node(int l, int r, int id, int t): l(l), r(r), id(id), t(t) {}
11     bool operator < (const node &b)const {
12         if (l / sz == b.l / sz) {
13             if (r / sz == b.r / sz) return t < b.t;
14             return r < b.r;
15         }
16         return l < b.l;
17     }
18 } qu[maxn];
19 struct node1 {
20     int pos, x, y;
21     node1() {}
22     node1(int pos, int x, int y): pos(pos), x(x), y(y) {}
23 } c[maxn];
24 void add(int val) {
25     if (num[a[val]] == 0) ans++;
26     num[a[val]]++;
27 
28 }
29 void del(int val)  {
30     num[a[val]]--;
31     if (num[a[val]] == 0) ans--;
32 }
33 void change(int pos, int x) {
34     if (L <= pos && pos <= R) del(pos);
35     a[pos] = x;
36     if (L <= pos && pos <= R) add(pos);
37 }
38 int main() {
39     scanf("%d%d", &n, &m);
40     sz = (int)sqrt((double)n + 0.5);
41     qsz = tsz = 0;
42     for (int i = 1 ; i <= n ; i++) {
43         scanf("%d", &a[i]);
44         now[i] = a[i];
45     }
46     for (int i = 1 ; i <= m ; i++) {
47         char s[10];
48         scanf("%s", s);
49         if(s[0] == 'Q') {
50             int l, r;
51             scanf("%d%d", &l, &r);
52             l++;
53             qsz++;
54             qu[qsz] = node(l, r, qsz, tsz);
55         } else {
56             int pos, x;
57             scanf("%d%d", &pos, &x);
58             pos++;
59             tsz++;
60             c[tsz].pos = pos, c[tsz].x = x, c[tsz].y = now[pos];
61             now[pos] = x;
62         }
63     }
64     for (int i = 1 ; i <= n ; i++) now[i] = a[i];
65     int t = 0;
66     sort(qu + 1, qu + 1 + qsz);
67     L = 1, R = 0, ans = 0;
68     for (int i = 1 ; i <= qsz ; i++) {
69         while(L < qu[i].l) del(L++);
70         while(R > qu[i].r) del(R--);
71         while(L > qu[i].l) add(--L);
72         while(R < qu[i].r) add(++R);
73         while(t < qu[i].t) t++, change(c[t].pos, c[t].x);
74         while(t > qu[i].t) change(c[t].pos, c[t].y), t--;
75         ANS[qu[i].id] = ans;
76     }
77     for (int i = 1 ; i <= qsz ; i++)
78         printf("%d
", ANS[i]);
79     return 0;
80 }
原文地址:https://www.cnblogs.com/qldabiaoge/p/9363044.html