#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 50004, INF = 0x7fffffff; int n, m, pos[maxn], s[1000005], c[maxn], ans, t[maxn]; int qsz, csz; struct node { int l, r, t, id, res; }Node[maxn]; void add(int l, int r, int t, int id) { Node[id].l = l; Node[id].r = r; Node[id].t = t; Node[id].id = id; } int l = 1, r = 0, T = 0; struct change { int pos, Old, New; }Cha[maxn]; void add_(int pos, int New, int Old, int cnt) { Cha[cnt].pos = pos; Cha[cnt].New = New; Cha[cnt].Old = Old; } bool cmp(node a, node b) { if(pos[a.l] == pos[b.l]) { if(pos[a.r] == pos[b.r]) return a.t < b.t; return pos[a.r] < pos[b.r]; } return pos[a.l] < pos[b.l]; } bool cmp_id(node a, node b) { return a.id < b.id; } void ad(int val) { s[val]++; if(s[val] == 1) ans++; } void de(int val) { s[val]--; // cout<< ans <<endl; if(s[val] == 0) ans--; } void go(int idx, int val) { if(l <= idx && idx <= r) { de(c[idx]); ad(val); } c[idx] = val; } int main() { qsz = csz = 0; ans = 0; scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) { scanf("%d", &c[i]); t[i] = c[i]; } int block = pow(n, 2.0/3); for(int i=1; i<=n; i++) pos[i] = (i-1)/block + 1; for(int i=1; i<=m; i++) { char str[5]; int x, y; scanf("%s%d%d", str, &x, &y); if(str[0] == ‘Q‘) { add(++x, y, csz, ++qsz); } else { add_(++x, y, t[x], ++csz); t[x] = y; } } sort(Node+1, Node+1+qsz, cmp); // for(int i=1; i<=qsz; i++) // cout<< Node[i].l << " " << Node[i].r << endl; for(int i=1; i<=qsz; i++) { for(; T < Node[i].t; T++) go(Cha[T+1].pos, Cha[T+1].New); for(; T > Node[i].t; T--) go(Cha[T].pos, Cha[T].Old); for(; r < Node[i].r; r++) ad(c[r+1]); for(; r > Node[i].r; r--) de(c[r]); for(; l < Node[i].r; l++) de(c[l]); for(; l > Node[i].l; l--) ad(c[l-1]); // for(; T < Node[i].t; T++) // go(Cha[T+1].pos, Cha[T+1].New); // for(; T > Node[i].t; T--) // go(Cha[T].pos, Cha[T].Old); Node[i].res = ans; } sort(Node+1, Node+qsz+1, cmp_id); for(int i=1; i<=qsz; i++) printf("%d\n",Node[i].res); return 0; }
原文地址:https://www.cnblogs.com/WTSRUVF/p/9348152.html
时间: 2024-11-10 11:41:33