bzoj 2054: 疯狂的馒头
这可是个神题2333
倒过来处理,往上染色,(倒过来的)先染色的是不能被后面的染色覆盖的,所以就可以维护一些一块一块的东西,,(并查集)
1 #include<bits/stdc++.h> 2 #define N 100005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define ls c[x][0] 6 #define rs c[x][1] 7 using namespace std; 8 inline int ra() 9 { 10 int x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 int c[10000002],f[10000002]; 16 int n,m,p,q,tot; 17 int find(int x){return (x==f[x] || !f[x])?f[x]=x:f[x]=find(f[x]);} 18 int main() 19 { 20 n=ra(); m=ra(); p=ra(); q=ra(); 21 for (int i=m; i>=1; i--) 22 { 23 int x=((LL)i*p+q)%n+1,y=((LL)i*q+p)%n+1; 24 if (x>y) swap(x,y); 25 for (int k=find(x); k<=y; k=find(k)){ 26 c[k]=i; 27 f[k]=k+1; 28 } 29 } 30 for (int i=1; i<=n; i++) 31 printf("%d\n",c[i]); 32 return 0; 33 }