Main topic: There are N steamed bread in a row, the initial color of 0, for M-time dyeing, I Will (I*P+Q) mod n to (i*q+p) mod n steamed bread all dyed into color I, for the last of all steamed bread color. N<=10^6 m<=10^7
Analysis: NM is very large can not segment tree, we may consider using and check set, we found that each steamed bread may be dyed several times, but only the last dyeing can determine its final color, it is inverted to do, and make each point color only modified once, for the interval [x, y], from the beginning of x will be except the point father all point to So this interval all points to Y, the next modification will skip the interval, because only modify the n points so efficiency is O (n)
Code:
ProgramASFD;varA:Array[0..1000001] ofLongint; F:Array[0..1000001] ofLongint; N,i,m,p,q,k,x,y,t,j:longint;functionFind (X:longint): Longint;varI,j,k:longint;beginI:=x; j:=x; whileI<>f[i] Doi:=F[i]; whileI<>j Do beginK:=F[J]; F[j]:=i; j:=K; End; Exit (i);End;beginreadln (N,M,P,Q); fori:=1 toN DoF[i]:=i; k:=0; forI:=mDownto 1 Do beginx:= (i*pMoDN+Q)MoDn+1; Y:= (i*qMoDN+P)MoDn+1; ifX>y Then beginT:=x; X:=y; y:=t;End; J:=find (x); whileJ<=y Do beginA[j]:=i; f[j]:=j+1; Inc (k); ifK=n ThenBreak ; J:=find (j); End; End; fori:=1 toN DoWriteln (A[i]); READLN;End.
View Code
BOZJ 2045: Crazy steamed buns (and check set)