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;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。