题意
构造题。构造长度为 的序列 ,满足给定的 个限制条件 :
- :;
- :。
数据保证一定存在合法构造。
分析
按下标从小到大填数。假设当前填到第 位,则可以分为四种情况讨论:
- 没有被任何条件限制:填任何数都可以。
- 同时被 和 限制:填 即可。
- 仅被 限制:填 即可。
- 仅被 限制:找到上一个仅被 限制的位置 。如果不存在 ,或 和 之间的不存在位置不被 限制,则令 ;否则令 。
下面尝试证明这样填数一定是对的。
根据题意,位于 限制区间中的每个数都必须 且每个区间内至少有一个 ;每个 限制区间中必须有 中的每个数,且不能有 。
由于数据保证有解,所以对于所有的 限制区间,其去除中间被 限制的位置后的剩余总长度一定 。因此按照上述填法一定能把 中的每个数都填进 限制区间内。而对于每个 限制区间,由于数据保证有解,而被 限制的位置不能填 ,因此一定存在一个位置使得其不被 限制。于是我们证明了上述填法是正确的。
代码
#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;
}