2120: Number color time limit:6 Sec Memory limit:259 MB
submit:3669 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
34
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.
Exercises
We record Pre[i] as the last point of the same color as I, and one obvious conclusion is that only pre[i]<l for a point I in the query interval [L,R] will contribute to the answer (it can be understood that only the first occurrence of each color is calculated, and if the pre-show is in the [L, R])
So we can do it in chunks. For each piece, we sort the block by the pre value, and then the binary search within the block can be easily resolved. Modify operation because no more than 1000 times, we can be directly violent reconstruction ( I initially wanted to use the balance tree to maintain the precursor and then rebuild and then write it down )
A chunk of DAFA is yelling!
{$S-}{$R-}{$I-} Programj01;varPre,b,bel,c:Array[0..10086] ofLongint; L,r:Array[0..10000] ofLongint; Head:Array[0..1000086] ofLongint; Size,n,m,x,y,i,cnt:longint; Ch:char; procedureqsort (l,r:longint);varI,j,x,y:longint;beginI:=l;j:=r;x:=pre[(I+J)Div 2]; Repeat whilePre[i]<x DoInc (I); whileX<PRE[J] DoDec (j); ifI<=j Then beginy:=pre[i];p re[i]:=pre[j];p re[j]:=y; Inc (I);d EC (j); End; untilI>J; ifI<r Thenqsort (I,R); ifL<j Thenqsort (l,j);End; procedureRes (x:longint);varI:longint;begin forI:=L[X] toR[X] Dopre[i]:=B[i]; Qsort (L[x],r[x]);End; procedureChange (x,y:longint);varI,tmp:longint;beginC[x]:=y; fori:=1 toN Dohead[c[i]]:=0; fori:=1 toN Do begintmp:=B[i]; B[i]:=Head[c[i]]; ifB[i]<>tmp ThenRes (bel[i]); Head[c[i]]:=i; End;End; functionFind (P,x:longint): Longint;varLl,rr,mid:longint;beginLL:=l[p];rr:=R[p]; ifPre[ll]>=x ThenExit0); whileLl<rr Do beginMid:= (LL+RR)Div 2; ifPre[mid+1]<x ThenLl:=mid+1 Elserr:=mid; End; Exit (LL+1-l[p]);End; functionAsk (X,y:longint): Longint;varI,j,ans:longint;beginans:=0; ifBel[x]=bel[y] Then begin forI:=x toY Do ifB[i]<x ThenInc (ANS); Exit (ANS); End; forI:=x toR[BEL[X]] Do ifB[i]<x ThenInc (ANS); forI:=l[bel[y]] toY Do ifB[i]<x ThenInc (ANS); fori:=bel[x]+1 tobel[y]-1 Doans:=ans+find (I,X); Exit (ANS);End; beginreadln (n,m); fori:=1 toN Doread (c[i]); Size:=trunc (sqrt (n) +ln (2*n)/LN (2)); fori:=1 toN Dobel[i]:= (I-1)DivSize+1; CNT:=Bel[n]; fori:=1 toCnt Do beginL[i]:= (I-1) *size+1; ifI=cnt ThenR[i]:=nElser[i]:=i*size; End; Fillchar (Head,sizeof (head),0); fori:=1 toN Do beginPre[i]:=Head[c[i]]; B[i]:=Pre[i]; Head[c[i]]:=i; End; fori:=1 toCnt Doqsort (L[i],r[i]); fori:=1 toM Do beginread (CH); while not(Chinch['Q','R']) Doread (CH); READLN (x, y); ifCh='Q' ThenWriteln (Ask (x, y)); ifCh='R' ThenChange (x, y); End;End.
BZOJ2120: Number of colors