And check the color of the dye and together. Dyed backwards.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 10000050using namespacestd;intn,m,l[maxn],r[maxn],father[maxn/Ten],p,q,col[maxn/Ten];intGetfather (intx) { if(X!=father[x]) father[x]=Getfather (father[x]); returnfather[x];}voidUnionn (intXinty) { if((!col[x]) && (!col[y]))return; intF1=getfather (x), f2=Getfather (y); FATHER[F1]=F2;}intMain () {scanf ("%d%d%d%d",&n,&m,&p,&q); for(intI=1; i<=m;i++) {L[i]= (i*p+q)%n+1; r[i]= (i*q+p)%n+1; if(l[i]>R[i]) swap (l[i],r[i]); } for(intI=1; i<=n;i++) father[i]=i; for(inti=m;i>=1; i--) { intnow=L[i]; if(Col[now]) Now=getfather (now) +1; while(now<=R[i]) {Unionn ( now-1, now); Unionn (now,now+1); Col[now]=i; now++; if(Col[now]) Now=getfather (now) +1; } } for(intI=1; i<=n;i++) printf ("%d\n", Col[i]); return 0;}
Bzoj 2054 Crazy Steamed Buns