2120: Number color time limit:6 Sec Memory limit:259 MB
submit:3665 solved:1422
[Submit] [Status] [Discuss] Description
Ink bought a set of n color brushes (some of which may be the same color), in a row, you need to answer the ink question. Ink will be like you publish the following command: 1, Q L R represents a number of different colors from the L-brush to the R-branch brush. 2, R P Col replaces the P-branch brush with the color col. Do you know what you need to do to meet the requirements of ink and ink?
Input
The 1th line of two integer n,m, respectively, represents the number of initial brushes and the number of things the ink will do. The 2nd row n integers, representing the color of the I-pen in the original brush row, respectively. Line 3rd to 2+m, each line represents the ink will do one thing, the format of the cadre points.
Output
For each query, you need to give a number in the corresponding line, representing the brush of the L-pen to the R-branch brush in a total of several different colors.
Sample Input6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
Sample Output4
4
3
4
HINT
For 100% of data, n≤10000,m≤10000, no more than 1000 modifications, all the integers appearing in the input data are greater than or equal to 1 and no more than 10^6.
2016.3.2 new data and two groups by Nano_ape
Resources:
1.http://blog.csdn.net/a_crazy_czy/article/details/51544720
2.http://blog.csdn.net/werkeytom_ftd/article/details/51540342
Block of sequences
For the query operation, the block with the left boundary is the first keyword, the block of the right border is the second keyword, and the time of the last modification is the Third keyword sort
Maintain current interval l,r and modify operation to the time now
Move now is to put a number outm and then another number INM, notice whether to make this change or undo the change
Moving L and R is the same as the normal MO team algorithm.
For the subject Cnt[x] indicates the number of occurrences in the current interval x
The transfer is clearly O (1), as long as the addition and subtraction can
There is one more problem, from the analysis of complexity:
Chunking sizeSFor< Span id= "mathjax-span-19" class= "Mrow" > n2/3 so we have < Span id= "mathjax-span-28" class= "Mrow" > n 1/3 block.
The original team has only two keywords, one is the left border and one is the right border. And with the Change Mo team to add one dimension: After the first few operations.
We sort by the block of the left border, the block of the right boundary, and the third keyword (the one above).
Statistical answer when more maintenance of a pointer to record where the modification operation, the move can be modified directly, if within the interval also to calculate its impact on the answer.
Number of left pointer movements:o ( Nx N2/ 3) =o (n5/< Span id= "mathjax-span-57" class= "mn" >3) 。
Number of right pointer moves:O(NXN2/3+ n 1/3x n) =o (n5/< Span id= "mathjax-span-90" class= "mn" >3) 。
To modify the pointer movement:O((N1/3)2xn)=O(n5/3) 。
So the total complexity of time O(n5/3).
It should be a N2/3, but the N1/2 is dozens of Ms faster than he is .............
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespaceStd;typedefLong Longll;Const intn=1e4+5, m=1e2+5, s=1e6+5; inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intN,m,x,y,bl,c[n],last[n],pos[n];//Last[x] x position before change colorChars[Ten];intt1,t2;structdata1{intL,r,id,tme; ll A, B; BOOL operator< (ConstData1 &x)Const{ if(pos[l]==POS[X.L]) { if(POS[R]==POS[X.R])returntme<X.tme; Else returnpos[r]<POS[X.R]; }Else returnpos[l]<POS[X.L]; }}q[n];structdata2{intX,d,pre;} P[n];intCnt[s];intL=1, r,now,ans,a[n];//inline void Outm (int x) {//cnt[x]--;//if (cnt[x]==0) ans--;//}InlinevoidOUTM (intx) {ans-= (--cnt[x]) = =0;} InlinevoidInmintx) {ans+ = (++cnt[x]) = =1;}//inline void inm (int x) {//cnt[x]++;//if (cnt[x]==1) ans++;//}InlinevoidChangeintXintd) { if(l<=x&&x<=R) {OUTM (c[x]); INM (d); } C[x]=D;}intMain () {n=read (); m=read (); BL=sqrt (n); for(intI=1; i<=n;i++) C[i]=last[i]=read (), pos[i]= (i-1)/bl+1; for(intI=1; i<=m;i++) {scanf ("%s", s); X=read (); y=read (); if(s[0]=='Q') q[++t1].l=x,q[t1].r=y,q[t1].id=t1,q[t1].tme=T2; Elsep[++t2].x=x,p[t2].d=y,p[t2].pre=last[x],last[x]=y; } sort (Q+1, q+1+t1); for(intI=1; i<=t1;i++){ while(now<q[i].tme) { Now++; Change (P[NOW].X,P[NOW].D); } while(now>q[i].tme) {Change (P[NOW].X,P[NOW].PRE); now--; } while(l<Q[I].L) {OUTM (c[l]); l++; } while(l>Q[I].L) {L--; INM (C[l]); } while(r<Q[I].R) {R++; INM (C[r]); } while(r>Q[I].R) {OUTM (c[r]); R--; } A[q[i].id]=ans; } for(intI=1; i<=t1;i++) printf ("%d\n", A[i]);}
Bzoj 2120 Number of colors &2453 maintenance queue [with modified MO team algorithm] "Learning notes"