带修莫队裸题。
不会算复杂度之类的,就不多说了, 只会瞎几把乱怼, A了就完事, 还不是很喜欢这么暴力玄学的东西。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb push_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 #define _S(X) cout << x << ' '; 14 #define __S(x) cout << x << endl; 15 typedef pair<int,int> pll; 16 const int INF = 0x3f3f3f3f; 17 const LL mod = (int)1e9+7; 18 const int N = 1e6 + 100; 19 int n, m, blo; 20 int col[N], cnt[N], tot[N]; 21 int ans = 0, nl, nr, nt, qsz, tsz; 22 struct Node{ 23 int l, r, time, id; 24 }q[N]; 25 struct NOde{ 26 int ncol, pcol, v; 27 }C[N]; 28 bool cmp(Node x1, Node x2){ 29 if(x1.l/blo == x2.l / blo) { 30 if(x1.r/blo == x2.r/blo) return x1.time < x2.time; 31 return x1.r / blo < x2.r / blo; 32 } 33 return x1.l/blo < x2.l/blo; 34 } 35 void Add(int cc){ 36 cnt[cc]++; 37 if(cnt[cc] == 1) ans++; 38 } 39 void Remove(int cc){ 40 cnt[cc]--; 41 if(!cnt[cc]) ans--; 42 } 43 void Go(int t){ 44 if(nl <= C[t].v && C[t].v <= nr){ 45 Add(C[t].ncol); 46 Remove(col[C[t].v]); 47 } 48 swap(col[C[t].v], C[t].ncol); 49 } 50 int main(){ 51 scanf("%d%d", &n, &m); 52 //blo = pow(n, 2.0 / 3.0); 53 blo = sqrt(n); 54 for(int i = 1; i <= n; i++) scanf("%d", &col[i]); 55 char str[3]; 56 for(int i = 1; i <= m; i++){ 57 scanf("%s", str); 58 if(str[0] == 'Q') { 59 q[qsz].id = qsz; 60 q[qsz].time = tsz; 61 scanf("%d", &q[qsz].l); 62 scanf("%d", &q[qsz].r); 63 ++qsz; 64 } 65 else { 66 ++tsz; 67 scanf("%d%d", &C[tsz].v, &C[tsz].ncol); 68 } 69 } 70 sort(q,q+qsz,cmp); 71 int tl, tt, tr; 72 for(int i = 0; i < qsz; i++){ 73 tl = q[i].l; 74 tr = q[i].r; 75 tt = q[i].time; 76 while(nt < tt) Go(++nt); 77 while(nt > tt) Go(nt--); 78 while(nl < tl) Remove(col[nl++]); 79 while(nl > tl) Add(col[--nl]); 80 while(nr < tr) Add(col[++nr]); 81 while(nr > tr) Remove(col[nr--]); 82 tot[q[i].id] = ans; 83 } 84 for(int i = 0; i < qsz; i++) 85 printf("%d ", tot[i]); 86 return 0; 87 }