BZOJ2054: 疯狂的馒头(并查集)

题意

题目链接

Sol

直接倒着并查集一波就行了

#include<bits/stdc++.h>
// #define int long long 
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, M, p, q, ans[MAXN], fa[MAXN];
int find(int x) {
	return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
main() {
//	freopen("a.in", "r", stdin);
	N = read(); M = read(); p = read(); q = read();
	for(int i = 1; i <= N + 1; i++) fa[i] = i;
	for(int i = M; i >= 1; i--) {
		int l = (i * p + q) % N + 1, r = (i * q + p) % N + 1;
		if(l > r) swap(l, r);
		for(int j = find(l); j <= r; j = (fa[j] = find(j + 1))) ans[j] = i;
	}
	for(int i = 1; i <= N; i++) printf("%d\n", ans[i]);
	return 0;
}

posted @ 2018-09-28 15:57  自为风月马前卒  阅读(387)  评论(1编辑  收藏  举报

Contact with me