题解:P17171 现在

· · 题解

注意,决定 \operatorname{cnt}(A) 的不是下降数,而是前缀最大值个数。

设前 n-1 项中前缀最大值的数量为:

则 $ \operatorname{cnt}(A)= \begin{cases} 0,&A_n\ne n,\\2^{c(A)},&A_n=n。\end{cases}$。 一轮冒泡过程中始终有一个数向右传递,最后留下全局最大值,所以结果最后一位必须是 $n$。 反向还原时,每遇到一个前缀最大值都有两种选择,其余位置只有一种选择,因此前驱数量为 $2^{c(A)}$。 于是,正的 $\operatorname{cnt}(A)$ 一定是 $2$ 的幂。 令:$lo=\max(1,\lceil\log_2L\rceil) $,$hi=\min(n-1,\lfloor\log_2R\rfloor)$。 问题转化为统计满足以下条件的排列: - 字典序位于 $[P,Q]$。 - 最后一项为 $n$。 - 前 $n-1$ 项恰有 $[lo,hi]$ 个前缀最大值。 若 $L=0$,还需要额外统计所有满足 $A_n\ne n$ 的排列,因为这些排列满足 $ \operatorname{cnt}(A)=0 $。 无符号第一类斯特林数 $ \begin{bmatrix}h\\k\end{bmatrix}$ 恰好表示长度为 $h$、具有 $k$ 个前缀最大值的排列数量。 定义 $ p_{k,h}=\frac{\begin{bmatrix}h\\k\end{bmatrix}}{h!} $。 由第一类斯特林数递推: $$ \begin{bmatrix}h\\k\end{bmatrix} =(h-1)\begin{bmatrix}h-1\\k\end{bmatrix} +\begin{bmatrix}h-1\\k-1\end{bmatrix} $$ 得到 $ p_{k,h}=\frac{(h-1)p_{k,h-1}+p_{k-1,h-1}}h $。 分别计算: - 满足条件且 $A\le Q$ 的数量。 - 满足条件且 $A<P$ 的数量。 两者相减即可。 扫描边界排列 $V$,在位置 $i$ 第一次选择比 $V_i$ 小的未使用数字 $x$。 设: - 当前前缀最大值为 $m$。 - 当前已有 $c$ 个前缀最大值。 - 后面还剩 $h=n-1-i$ 个位置。 - 选择 $x$ 后的新最大值为 $M=\max(m,x)$。 - 剩余数字中大于 $M$ 的数字数量为 $H=n-1-M$。 只有这些大于 $M$ 的数字可能产生新的前缀最大值。 若后面新增 $j$ 个前缀最大值,补全方案数为: $ h!\frac{\begin{bmatrix}H\\j\end{bmatrix}}{H!} =h!p_{j,H}

候选 x 分为两类。

第一类为 x\le m。

此时最大值和已有前缀最大值数量不变,这一类候选可以合并计算。

第二类为 m<x<V_i。

此时新增一个前缀最大值,并且 H=n-1-x 构成一段连续区间。

所以每个位置只会产生常数个形如 h!\sum_{j=l}^{r}\sum_{H=L}^{R}p_{j,H} 的询问。

把这些询问离线挂在区间端点上。

枚举 j 时,对 p_{j,H} 关于 H 求前缀和,就可以用 O(1) 处理每个事件。

注意: 当 L=0 时:

用康托展开分别求出 [P,Q] 中:

那么满足 \operatorname{cnt}(A)=0 的排列数量为 all-en (记得开 long long),再将这一部分加入答案。

时间复杂度为 O(n\log n+n\log R) 。

代码(有点屎山):

#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);
}