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
| #include<bits/stdc++.h> using namespace std; int vis[1000000 + 10], a[133333 + 10], sum, chg[133333 + 10], pos[133333 + 10]; int n, Q, bel[133333 + 10]; struct query { int l, r, id, t; int ans; friend bool operator<(const query& a, const query& b) { return bel[a.l] == bel[b.l] ? bel[a.r] == bel[b.r] ? a.t < b.t : bel[a.l] % 2 == 1 ? bel[a.r] < bel[b.r] : bel[a.r] > bel[b.r] : bel[a.l] < bel[b.l]; } }q[133333 + 10]; void add(const int& x) { sum += vis[a[x]]++ >= 1; } void remove(const int& x) { sum -= --vis[a[x]] >= 1; } void upt(int& x, const int& j) { x++; if (q[j].l <= pos[x] && pos[x] <= q[j].r) remove(pos[x]), add((swap(a[pos[x]], chg[x]), pos[x])); else swap(a[pos[x]], chg[x]); } void dnt(int& x, const int& j) { if (q[j].l <= pos[x] && pos[x] <= q[j].r) remove(pos[x]), add((swap(a[pos[x]], chg[x]), pos[x])); else swap(a[pos[x]], chg[x]); x--; } signed main() { cin.tie(0)->sync_with_stdio(false); cin >> n >> Q; for (int i = 1; i <= n; ++i) cin >> a[i]; int t1 = 1, t2 = pow(n, 0.666), t3 = 1; for (int i = 1; i <= n; ++i) { bel[i] = t3; if (t1 == t2) t1 = 1, t3++; t1++; } int t4 = 1; for (int j = 1; j <= Q; ++j) { static char opt; cin >> opt >> q[j].l >> q[j].r; if (opt == 'R') { ++t4; chg[t4] = q[j].r; pos[t4] = q[j].l; swap(a[pos[t4]], chg[t4]); j--, Q--; continue; } q[j].id = j; q[j].t = t4; } sort(q + 1, q + Q + 1); q[0].t = t4; for (int j = 1; j <= Q; ++j) { while (q[j - 1].l < q[j].l) remove(q[j - 1].l), q[j - 1].l++; while (q[j - 1].l > q[j].l) add(q[j - 1].l - 1), q[j - 1].l--; while (q[j - 1].r < q[j].r) add(q[j - 1].r + 1), q[j - 1].r++; while (q[j - 1].r > q[j].r) remove(q[j - 1].r), q[j - 1].r--; while (q[j - 1].t < q[j].t) upt(q[j - 1].t, j); while (q[j - 1].t > q[j].t) dnt(q[j - 1].t, j); q[j].ans = q[j].r - q[j].l + 1 - sum; } sort(q + 1, q + Q + 1, [](const query& a, const query& b)->bool {return a.id < b.id; }); for (int i = 1; i <= Q; ++i) cout << q[i].ans << endl; return 0; }
|