题解:CF2157C Meximum Array 2
题意
构造题。构造长度为
数据保证一定存在合法构造。
分析
按下标从小到大填数。假设当前填到第
- 没有被任何条件限制:填任何数都可以。
- 同时被
\min 和\operatorname{mex} 限制:填k+1 即可。 - 仅被
\min 限制:填k 即可。 - 仅被
\operatorname{mex} 限制:找到上一个仅被\operatorname{mex} 限制的位置j 。如果不存在j ,或i 和j 之间的不存在位置不被\operatorname{mex} 限制,则令a_i=(a_j+1)\bmod k ;否则令a_i=0 。
下面尝试证明这样填数一定是对的。
根据题意,位于
由于数据保证有解,所以对于所有的
代码
#include <bits/stdc++.h>
using namespace std;
#define gc getchar
template<typename T> void read(T &x) {x = 0; int f = 1; char c = gc(); while (!isdigit(c)) {if (c == '-') f = -1; c = gc();} while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = gc(); x *= f;}
template<typename T, typename ... Args> void read(T &x, Args &... args) {read(x); read(args...);}
#define ll long long
#define ull unsigned long long
#define rep(i, s, t) for (int i = (s); i <= (t); i++)
#define per(i, s, t) for (int i = (s); i >= (t); i--)
#define chkmax(x, y) x = max(x, y);
#define chkmin(x, y) x = min(x, y);
const int N = 200005;
int n, k, q, a[N], b[N];
void pd() {
read(n, k, q);
rep(i, 1, n) b[i] = 0;
rep(i, 1, q) {
int c, l, r;
read(c, l, r);
rep(i, l, r) b[i] |= c;
}
int now = 0;
rep(i, 1, n) {
if (b[i] == 0) {
now = 0;
a[i] = 0;
continue;
} else if (b[i] == 1) {
now = 0;
a[i] = k;
continue;
} else if (b[i] == 2) {
a[i] = now;
now = (now + 1) % k;
continue;
} else {
a[i] = k + 1;
continue;
}
}
rep(i, 1, n) printf("%d%c", a[i], " \n"[i == n]);
return;
}
int main() {
int _;
read(_);
while (_--) pd();
return 0;
}