注意,决定 的不是下降数,而是前缀最大值个数。
设前 项中前缀最大值的数量为:
。
则 。
一轮冒泡过程中始终有一个数向右传递,最后留下全局最大值,所以结果最后一位必须是 。
反向还原时,每遇到一个前缀最大值都有两种选择,其余位置只有一种选择,因此前驱数量为 。
于是,正的 一定是 的幂。
令:,。
问题转化为统计满足以下条件的排列:
- 字典序位于 。
- 最后一项为 。
- 前 项恰有 个前缀最大值。
若 ,还需要额外统计所有满足 的排列,因为这些排列满足 。
无符号第一类斯特林数 恰好表示长度为 、具有 个前缀最大值的排列数量。
定义 。
由第一类斯特林数递推:
得到 。
分别计算:
- 满足条件且 的数量。
- 满足条件且 的数量。
两者相减即可。
扫描边界排列 ,在位置 第一次选择比 小的未使用数字 。
设:
- 当前前缀最大值为 。
- 当前已有 个前缀最大值。
- 后面还剩 个位置。
- 选择 后的新最大值为 。
- 剩余数字中大于 的数字数量为 。
只有这些大于 的数字可能产生新的前缀最大值。
若后面新增 个前缀最大值,补全方案数为:
候选 分为两类。
第一类为 。
此时最大值和已有前缀最大值数量不变,这一类候选可以合并计算。
第二类为 。
此时新增一个前缀最大值,并且 构成一段连续区间。
所以每个位置只会产生常数个形如 的询问。
把这些询问离线挂在区间端点上。
枚举 时,对 关于 求前缀和,就可以用 处理每个事件。
注意: 当 时:
用康托展开分别求出 中:
- 所有排列的数量
all。 - 最后一项为 的排列数量
en。
那么满足 的排列数量为 (记得开 long long),再将这一部分加入答案。
时间复杂度为 。
代码(有点屎山):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 5, E = N * 4 + 5, P = 998244353;
int n, lo, hi, tot, mxk = -1, ans;
ll L, R;
int a[N], b[N], fac[N], iv[N], tr[N];
int el[E], er[E], ec[E], nh[E], nl[E];
int hh[65], hl[65], nd[65], lm[65];
struct IO {
static const int S = 1 << 20;
int l = 0, p = 0;
char b[S];
char gc() {
if (p == l) l = fread(b, 1, S, stdin), p = 0;
return p == l ? 0 : b[p++];
}
ll rd() {
ll x = 0;
char c = gc();
while (c < '0' || c > '9') c = gc();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc();
return x;
}
} io;
inline int sum(int x) {
int s = 0;
for (; x; x -= x & -x) s += tr[x];
return s;
}
inline void addbit(int x) {
for (; x <= n; x += x & -x) ++tr[x];
}
inline void adde(int l, int r, int c, int x, int y) {
if (x < 0 || l > r || !c) return;
int e = ++tot;
el[e] = l;
er[e] = r;
ec[e] = c;
nh[e] = hh[x];
hh[x] = e;
nd[x] = max(nd[x], r);
mxk = max(mxk, x);
if (y >= 0) {
nl[e] = hl[y];
hl[y] = e;
nd[y] = max(nd[y], r);
mxk = max(mxk, y);
} else nl[e] = 0;
}
void build(int *v, int sg, int inc, int gen, int &rk, int &ed, int &okn) {
memset(tr, 0, (n + 1)*sizeof(int));
int m = 0, c = 0, ok = 1;
rk = ed = 0;
for (int i = 1; i <= n; i++) {
int x = v[i], z = x - 1 - sum(x - 1);
rk = (rk + (ll)z * fac[n - i]) % P;
if (i < n && ok) {
int rem = n - 1 - i;
ed = (ed + (ll)z * fac[rem]) % P;
if (gen && c <= hi) {
int w = x > m ? m - i + 1 : z;
int co = (ll)w * fac[rem] % P;
if (sg < 0 && co) co = P - co;
if (w) adde(n - 1 - m, n - 1 - m, co, hi - c, lo - 1 - c);
if (x > m + 1 && c < hi) {
co = fac[rem];
if (sg < 0 && co) co = P - co;
adde(n - x, n - m - 2, co, hi - c - 1, lo - c - 2);
}
}
if (x == n) ok = 0;
else if (x > m) m = x, ++c;
}
addbit(x);
}
okn = ok;
if (inc && ok && lo <= c && c <= hi) {
ans += sg;
if (ans < 0) ans += P;
if (ans >= P) ans -= P;
}
}
inline int getsum(int *s, int l, int r) {
int x = s[r] - (l ? s[l - 1] : 0);
return x < 0 ? x + P : x;
}
inline void work(int k, int *s) {
for (int e = hh[k]; e; e = nh[e]) {
int x = (ll)ec[e] * getsum(s, el[e], er[e]) % P;
ans += x;
if (ans >= P) ans -= P;
}
for (int e = hl[k]; e; e = nl[e]) {
int x = (ll)ec[e] * getsum(s, el[e], er[e]) % P;
ans -= x;
if (ans < 0) ans += P;
}
}
int main() {
n = io.rd();
L = io.rd();
R = io.rd();
for (int i = 1; i <= n; i++) a[i] = io.rd();
for (int i = 1; i <= n; i++) b[i] = io.rd();
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = (ll)fac[i - 1] * i % P;
int kmx = -1;
if (R) {
ll x = 1;
kmx = 0;
while (x <= R / 2) x <<= 1, ++kmx;
}
int kmn = 1;
if (L > 2) {
ll x = 2;
while (x < L) x <<= 1, ++kmn;
}
lo = kmn;
hi = min(kmx, n - 1);
memset(nd, -1, sizeof(nd));
int rp, rq, ep, eq, op, oq;
int gen = hi >= lo;
build(b, 1, gen, gen, rq, eq, oq);
build(a, -1, 0, gen, rp, ep, op);
if (gen && mxk >= 0) {
lm[mxk] = max(0, nd[mxk]);
for (int k = mxk - 1; k >= 0; k--) lm[k] = max(0, max(nd[k], lm[k + 1] - 1));
int mx = 0;
for (int k = 0; k <= mxk; k++) mx = max(mx, lm[k]);
if (mx) {
iv[1] = 1;
for (int i = 2; i <= mx; i++) iv[i] = P - (ll)(P / i) * iv[P % i] % P;
}
int *pr = a, *d = b, *s = tr;
memset(pr, 0, (lm[0] + 1)*sizeof(int));
pr[0] = 1;
for (int i = 0; i <= lm[0]; i++) s[i] = 1;
work(0, s);
for (int k = 1; k <= mxk; k++) {
int z = lm[k];
d[0] = 1;
s[0] = 1;
for (int h = 1; h <= z; h++) {
int q = pr[h - 1] - d[h - 1];
if (q < 0) q += P;
int x = d[h - 1] + (ll)q * iv[h] % P;
if (x >= P) x -= P;
d[h] = x;
x += s[h - 1];
if (x >= P) x -= P;
s[h] = x;
}
swap(pr, d);
work(k, s);
}
}
int all = rq + 1 - rp;
if (all < 0) all += P;
if (all >= P) all -= P;
int en = eq + oq - ep;
if (en < 0) en += P;
if (en >= P) en -= P;
if (L == 0) {
ans += all;
if (ans >= P) ans -= P;
ans -= en;
if (ans < 0) ans += P;
}
printf("%d\n", ans);
}