P11164 [BalkanOI 2023] Permutations
给出 的排列 。 次询问给出 ,求满足下列条件的 的排列数目:
- 排列的前缀为 。
- 排列的 LDS 长度 。LDS 为最长下降子序列。
对 取模。
,2 秒,250 MB。
先考虑没有前缀限制怎么做,打表发现答案数列的前几项是 ,猜测答案为 Catalan 数 。
我们尝试将合法排列和格路计数的模型建立双射。
首先考虑从排列构造格路。我们求出前缀最大值 ,然后将 画在平面上,先向上再向右走即可。因为 必成立,所以这是一条始终在 上方的格路。
接着考虑从格路构造排列。先求出每个横坐标对应的高度 。对于 的位置 ,必有 ,即 是前缀最大值。对于非前缀最大值,我们让它们按照严格递增的顺序,依次填入剩下的空位。
在这个构造中,前缀最大值和非前缀最大值构成的子序列都是递增的。由 Dilworth 定理, 当且仅当排列可以被划分为至多两个递增子序列。可以看到,这确实是满足要求的。
现在我们强制排列的前缀为 ,这相当于固定了格路的前半段。先假设我们会判定无解,下面按有解讨论。
记 ,此时格路的起点变成 。对于 的非前缀最大值,它们已经被钦定了相对顺序,即按严格递增填入空位,故不会产生贡献。我们只要关注 的数。用格路计数的语言来刻画,就是说从 出发不越过 走到 的格路数目。
用单线反射容斥来算,总方案数 ,将起点关于 作对称,不合法方案数转化成 的朴素格路计数。问题的答案是
原题中 , 是区间 。现在的难点在于判无解,这是一个和数数没有关系的纯 ds 题。
设 的区间 为 ,将 加到 后面,则前缀不合法当且仅当序列 的 。等价于下列两个条件至少有一个成立:
- 。
- 。
根据区间 等于补集 的经典结论, 可以用前后缀 算出来。对上面两个条件分别讨论:
-
第一个条件:我们用单调栈求出 和 。条件转化为 。
这等价于 。由 可以发现 的条件没有用。因此只要判断 成立。
将 画到二维平面上,询问变成 2-side 矩形数点,扫描线,维护 坐标历史 直接判定即可。
-
第二个条件:仍用单调栈求得 ,条件转化为 。我们写成 。
将 画到二维平面上,询问是 3-side 矩形求 ,扫描线,树状数组支持单点修改后缀 即可。
视 同阶,判定部分 ,计数部分 。
代码
const int N = 3e5 + 5, inf = 1e9;
int n, q, a[N], f[N], g[N], pre[N], suf[N];
bool wa[N];
comb<mint, 2 * N> C(2*N-1);
using node1 = tuple<int, int, int, int, int>; // (y, x, type, id, l)
using node2 = tuple<int, int, int, int>; // (l, r, x, id)
vector<node1> q1; vector<node2> q2;
pair<int, int> q0[N];
struct table {
int st[20][N];
void build() {
copy(a + 1, a + n + 1, st[0] + 1);
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++)
st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
int ask(int l, int r) {
int k = lg(r - l + 1);
return max(st[k][l], st[k][r - (1 << k) + 1]);
}
} R;
void prework() {
R.build();
stack<int> st;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
f[i] = st.empty() ? 0 : st.top(), st.emplace(i);
}
while (!st.empty()) st.pop();
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] > a[i]) st.pop();
g[i] = st.empty() ? n + 1 : st.top(), st.emplace(i);
}
pre[0] = suf[n + 1] = inf;
for (int i = 1; i <= n; i++) pre[i] = min(pre[i - 1], a[i]);
for (int i = n; i >= 1; i--) suf[i] = min(suf[i + 1], a[i]);
for (int i = 1; i <= n; i++) q1.emplace_back(g[i], f[i], 0, -1, -1);
}
struct segtree {
int tr[N];
void add(int x, int c) {
for (; x >= 1; x -= lowbit(x)) chkmax(tr[x], c);
}
int ask(int x) {
int res = -inf;
for (; x <= n; x += lowbit(x)) chkmax(res, tr[x]);
return res;
}
} T;
void solve1() {
sort(q1.begin(), q1.end(), [](const node1& a, const node1& b) -> bool {
if ($0(a) == $0(b)) return $2(a) < $2(b);
return $0(a) < $0(b);
});
int h = -inf;
for (auto [y, x, type, id, l] : q1) {
if (type == 0) chkmax(h, x);
else wa[id] |= h >= l;
}
}
void solve2() {
int i = 1;
sort(q2.begin(), q2.end(), [](const node2& a, const node2& b) -> bool {
return $1(a) < $1(b);
});
for (auto [l, r, x, id] : q2) {
for (; i <= r; i++) T.add(f[i], a[i]);
wa[id] |= T.ask(l) > x;
}
}
void _main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
prework(), cin >> q;
for (int i = 1, l, r; i <= q; i++) {
cin >> l >> r; q0[i] = {l, r};
int x = min(pre[l - 1], suf[r + 1]);
q1.emplace_back(r, 0, 1, i, l), q2.emplace_back(l, r, x, i);
}
solve1(), solve2();
for (int i = 1; i <= q; i++) {
if (wa[i]) {
cout << 0 << '\n'; continue;
}
auto [l, r] = q0[i];
int k = r - l + 1, m = R.ask(l, r);
cout << C(2 * n - m - k, n - k) - C(2 * n - m - k, n - m - 1) << '\n';
}
}