题解:P11687 [JOIGST 2024] 年轮蛋糕 2 / バームクーヘン 2
Suin_tryin · · 题解
P11687 [JOIGST 2024] 年轮蛋糕 2 / バームクーヘン 2(青)
Part 1 思路
考虑固定一个人的蛋糕,假设这种情况合法,这样切割开的
接着考虑固定一个人的蛋糕,判断是否合法。对于每个人,可以维护他已经确定要选的位置构成的区间的左右端点(合法区间),如果它在切割开的蛋糕中只占了一块,是合法的,如果占了多个块,就不合法。
最后,考虑不固定蛋糕,需要快速知道哪些是合法的分割方案。首先如果有一个人的区间长度
Part 2 实现
这题的实现有点小难啊。笔者的代码能力太弱,只能提供一些实现思路和注意事项。
先是维护左右端点,这等价对于同一个人的点,维护一个最大的间隔,再取反,可以使用 set 进行。这段有一个环形分讨,例如,如果找不到当前点的后继就要去找最小的那个点。
提供一个求左右端点的代码: ::::info[code1]
其中
set< ll > s[N], u, tot;
ll clen(pll x) { //求长度
if (x.l <= x.r) return x.r - x.l + 1;
else return 1ll * n * m - x.l + 1 + x.r;
}
struct cmp {
bool operator()(const pll &x, const pll &y) const {
if (clen(x) != clen(y)) {
return clen(x) < clen(y);
}
else if (x.l != y.l) {
return x.l < y.l;
}
return x.r < y.r;
}
};
multiset< pll, cmp > d[N];
pll add(int _, ll pos) { //维护间隔
if (s[_].find(pos) != s[_].end()) return *d[_].rbegin();
if (s[_].empty()) {
++ cnt;
s[_].insert(pos);
d[_].insert({pos + 1, pos});
return *d[_].rbegin();
}
auto nxt = s[_].upper_bound(pos);
if (nxt == s[_].end()) nxt = s[_].begin();
auto pre = s[_].lower_bound(pos);
if (pre == s[_].begin()) pre = --s[_].end();
else -- pre;
d[_].erase(d[_].find({(*pre) + 1, *nxt}));
d[_].insert({(*pre) + 1, pos});
d[_].insert({pos + 1, *nxt});
s[_].insert(pos);
return *d[_].rbegin();
}
...
solve() {
pll now = add(i, p);
ll nl = now.r, nr = now.l - 1; //nl nr 是区间左右端点
}
::::
然后是判断区间交的问题,这个可以和
给出一段求交的代码:
::::info[code2]
void qj(ll l, ll r) { // 求交
l %= m; if (l <= 0) l += m;
r %= m; if (r <= 0) r += m;
if (l <= r) {
for (auto pos = u.begin(); pos != u.end() && *pos < l; pos = u.erase(pos)) { }
for (auto pos = u.upper_bound(r); pos != u.end(); pos = u.erase(pos)) { }
}
else {
for (auto pos = u.upper_bound(r); pos != u.end() && *pos < l; pos = u.erase(pos)) { }
}
}
::::
如果不是第一次出现的点,直接:
qj(nr, nl - 1);
如果是第一次出现的点就比较麻烦,需要找到上一个和下一个区间,对空隙求交。前驱后继又需要环形特判:
::::info[code3]
ll _l, _r;
auto pre = tot.lower_bound(p);
if (pre == tot.begin()) _l = *tot.rbegin() - 1ll * n * m;
else _l = *(-- pre);
auto nxt = tot.upper_bound(p);
if (nxt == tot.end()) _r = *tot.begin() + 1ll * n * m;
else _r = *nxt;
if (p - _l < m) qj(_l, p - 1);
if (_r - p < m) qj(p, _r - 1);
::::
基本就是全部了。完整代码:
::::info[code]
#include <bits/stdc++.h>
using namespace std;
#define ONLINE_JUDGE
typedef long long ll;
typedef unsigned long long ull;
#define l first
#define r second
#define pb push_back
#define vec vector
#define pii pair<int, int>
#define pll pair<ll, ll>
constexpr int N = 2e5 + 10;
int t;
int n, m, mod, q, cnt;
ll fac[N];
set< ll > s[N], u, tot;
ll clen(pll x) {
if (x.l <= x.r) return x.r - x.l + 1;
else return 1ll * n * m - x.l + 1 + x.r;
}
struct cmp {
bool operator()(const pll &x, const pll &y) const {
if (clen(x) != clen(y)) {
return clen(x) < clen(y);
}
else if (x.l != y.l) {
return x.l < y.l;
}
return x.r < y.r;
}
};
multiset< pll, cmp > d[N];
pll add(int _, ll pos) {
if (s[_].find(pos) != s[_].end()) return *d[_].rbegin();
if (s[_].empty()) {
++ cnt;
s[_].insert(pos);
d[_].insert({pos + 1, pos});
return *d[_].rbegin();
}
auto nxt = s[_].upper_bound(pos);
if (nxt == s[_].end()) nxt = s[_].begin();
auto pre = s[_].lower_bound(pos);
if (pre == s[_].begin()) pre = --s[_].end();
else -- pre;
d[_].erase(d[_].find({(*pre) + 1, *nxt}));
d[_].insert({(*pre) + 1, pos});
d[_].insert({pos + 1, *nxt});
s[_].insert(pos);
return *d[_].rbegin();
}
void qj(ll l, ll r) { // 求交
l %= m; if (l <= 0) l += m;
r %= m; if (r <= 0) r += m;
if (l <= r) {
for (auto pos = u.begin(); pos != u.end() && *pos < l; pos = u.erase(pos)) { }
for (auto pos = u.upper_bound(r); pos != u.end(); pos = u.erase(pos)) { }
}
else {
for (auto pos = u.upper_bound(r); pos != u.end() && *pos < l; pos = u.erase(pos)) { }
}
}
bool flg;
void solve() {
cin >> n >> m >> mod >> q;
fac[0] = 1;
for (int i = 1; i <= n; i ++) fac[i] = fac[i - 1] * i % mod;
for (int i = 1; i <= m; i ++) u.insert(i);
while (q --) {
int i; ll p; cin >> p >> i;
bool tmp = s[i].empty();
if (tmp && !tot.empty()) {
ll _l, _r;
auto pre = tot.lower_bound(p);
if (pre == tot.begin()) _l = *tot.rbegin() - 1ll * n * m;
else _l = *(-- pre);
auto nxt = tot.upper_bound(p);
if (nxt == tot.end()) _r = *tot.begin() + 1ll * n * m;
else _r = *nxt;
if (p - _l < m) qj(_l, p - 1);
if (_r - p < m) qj(p, _r - 1);
}
pll now = add(i, p);
ll nl = now.r, nr = now.l - 1, len = clen({nl, nr});
if (!tmp) {
if (len > m) flg = 1;
else qj(nr, nl - 1);
}
tot.insert(p);
if (u.empty()) flg = 1;
// cout << flg << '\n';
if (flg) cout << 0 << '\n';
else cout << fac[n - cnt] * (ll)u.size() % mod << '\n';
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
t = 1;
// cin >> t;
while (t --) solve();
return 0;
}
::::