并查集把染过色的并在一起。倒着染色。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 10000050 using namespace std; int n,m,l[maxn],r[maxn],father[maxn/10],p,q,col[maxn/10]; int getfather(int x) { if (x!=father[x]) father[x]=getfather(father[x]); return father[x]; } void unionn(int x,int y) { if ((!col[x]) && (!col[y])) return; int f1=getfather(x),f2=getfather(y); father[f1]=f2; } int main() { scanf("%d%d%d%d",&n,&m,&p,&q); for (int i=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 (int i=1;i<=n;i++) father[i]=i; for (int i=m;i>=1;i--) { int now=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 (int i=1;i<=n;i++) printf("%d\n",col[i]); return 0; }
时间: 2024-12-17 16:55:35