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
| #include <bits/stdc++.h>
using namespace std; const int N = 2e5 + 10, B = 320; int a[N], tot; int cnt[B][N]; int id[N]; unordered_map<int, int> h;
int read() { int x = 0, y = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') y = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * y; }
void modify(int x, int c) { if (!h[c]) h[c] = ++tot; c = h[c]; cnt[id[x]][a[x]]--, cnt[id[x]][c]++; a[x] = c; }
int query(int l, int r, int k) { k = h[k]; if (id[l] == id[r]) { int res = 0; for (int i = l; i <= r; i++) if (a[i] == k) res++; return res; } int res = 0; for (int i = l; id[i] == id[l]; i++) if (a[i] == k) res++; for (int i = r; id[i] == id[r]; i--) if (a[i] == k) res++; for (int i = id[l] + 1; i <= id[r] - 1; i++) res += cnt[i][k]; return res; }
int main() { int n = read(), m = read(); for (int i = 1; i <= n; i++) { a[i] = read(); if (!h[a[i]]) h[a[i]] = ++tot; a[i] = h[a[i]]; id[i] = (i - 1) / B + 1; cnt[id[i]][a[i]]++; } while (m--) { char op; cin >> op; if (op == 'C') { int x = read(), c = read(); modify(x, c); } else { int l = read(), r = read(), k = read(); printf("%d\n", query(l, r, k)); } } return 0; }
|