1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <cstdio> #include <set> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 2e5 + 10, M = 5e7 + 10, K = 1e6 + 10; int n, m, w[N]; struct Node { int l, r, v; } tr[M]; int idx, rt[N]; set <int> p[K]; void insert (int &x, int t, int k, int l = 0, int r = n - 1) { !x && (x = ++idx); tr[x].v += k; if (l == r) return; int mid = l + r >> 1; t <= mid ? insert(tr[x].l, t, k, l, mid) : insert(tr[x].r, t, k, mid + 1, r); } void modify (int x, int t, int k) { for (; x <= n; x += x & -x) insert(rt[x], t, k); } int query (int x, int k, int l = 0, int r = n - 1) { if (r <= k) return tr[x].v; if (l > k) return 0; int mid = l + r >> 1; return query(tr[x].l, k, l, mid) + query(tr[x].r, k, mid + 1, r); } int main () { read(n), read(m); for (int i = 1; i <= 1e6; i++) p[i].insert(0), p[i].insert(n + 1); for (int i = 1; i <= n; i++) { read(w[i]); modify(i, *--p[w[i]].lower_bound(i), 1); p[w[i]].insert(i); } for (char op[2]; m; m--) { scanf("%s", op); if (op[0] == 'Q') { int l, r, res = 0; read(l), read(r); int t = --l; for (; l; l -= l & -l) res -= query(rt[l], t); for (; r; r -= r & -r) res += query(rt[r], t); write(res), puts(""); } else if (op[0] == 'R') { int t, k, a, b; read(t), read(k); a = *p[w[t]].upper_bound(t), b = *--p[w[t]].lower_bound(t); modify(t, b, -1); modify(a, t, -1); modify(a, b, 1); p[w[t]].erase(t); w[t] = k; a = *p[w[t]].upper_bound(t), b = *--p[w[t]].lower_bound(t); modify(t, b, 1); modify(a, b, -1); modify(a, t, 1); p[w[t]].insert(t); } } return 0; }
|