【BZOJ 2054】 疯狂的馒头

Input

第一行四个正整数N,M,p,q

Output

一共输出N行,第i行表示第i个馒头的最终颜色(如果最终颜色是白色就输出0)。

Sample Input

4 3 2 4

Sample Output

2
2
3
0

HINT

这道题线段树不会卡,因为时限是10s, 然后加入并查集优化一下就好了。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int n, m, p, q;
int fa[10000005], a[1000005];
int find(int x) {
    if(!fa[x] || fa[x] == x)
        return fa[x] = x;
    return fa[x] = find(fa[x]);
}//递归
int x, y;
int main() {
    scanf("%d %d %d %d", &n, &m, &p, &q);
    for(int i = m; i >= 1; i--) {
        x = ((long long)i * p + q) % n + 1;
        y = ((long long)i * q + p) % n + 1;
        if(x > y)
            swap(x,y);
        for(int j = find(x); j <= y;j = find(j)) {
            a[j] = i;
            fa[j] = j + 1;
        }
    }
    for(int i = 1; i <= n; i++)
        printf("%d\n", a[i]);
    return 0;
}
View Code

 

posted @ 2019-05-29 17:15  机器闵  阅读(123)  评论(0编辑  收藏  举报