跳到正文

题解

题解:P11928 [PA 2025] 子序列 / Podciągi

沉石鱼惊旋约 18 分钟
原文
文章信息与版本

文章信息

发布
2025年3月18日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 18 分钟
获赞
6
题目
P11928

版本

  1. v1当前

题意

给定长度 nn 字符集长度为前 mm 小的字符串 ss,输出有多少本质不同子序列出现了至少两次。

qq 次修改,每次给出 x,tx,t 表示 sx←ts_x\gets t。修改是永久的。在所有修改之前和每次修改后输出共 q+1q+1 个答案。保证 tt 也在字符集中。

答案对 998244353998244353 取模。

3≤n≤5×1043\leq n\leq 5\times 10^4,0≤q≤5×1040\leq q\leq 5\times 10^4,m=6m=6。

做法

出现至少两次直接计数非常困难,考虑用 Part A 所有本质不同子序列的个数减掉 Part B 恰好出现一次的子序列个数。

Part A 所有本质不同的子序列个数是个大典题。

考虑 DP。设 fi,cf_{i,c} 表示前 ii 个数 cc 结尾的方案数。转移有 fi,j←{fi−1,csi≠c∑d=0mfi−1,dsi=cf_{i,j}\gets \begin{cases}f_{i-1,c}&s_i\neq c \\\sum\limits_{d=0}^m f_{i-1,d} &s_i=c\end{cases}。

我们把前 mm 小个字符映射成 [0,m−1][0,m-1],fi,mf_{i,m} 是空串,有固定的一个方案数。

而 Part B 恰好出现一次的子序列个数对着这个 DP 改一改就好。

考虑何时会算重,就是 cc 的上一个出现位置 lstclst_c 比 dd 的上一个出现位置 lstdlst_d 要靠后。即 lstc>lstdlst_c\gt lst_d。这种情况不可以转移。

暴力的代码 O(nq)\mathcal O(nq) 如下:

C++
int solve()
{
    vector<array<Z, 6>> f(n + 1);
    vector<array<Z, 6>> g(n + 1);
    vector<int> lst(6);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (s[i] - 'a' == j)
            {
                for (int k = 0; k < m; k++)
                    f[i][j] += f[i - 1][k];
                f[i][j]++;
            }
            else
            {
                f[i][j] = f[i - 1][j];
            }
        }
    }
    Z ans = 0;
    for (int j = 0; j < m; j++)
        ans += f[n][j];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (s[i] - 'a' == j)
            {
                for (int k = 0; k < m; k++)
                    if (lst[j] <= lst[k])
                        g[i][j] += g[i - 1][k];
                if (!lst[j])
                    g[i][j]++;
            }
            else
            {
                g[i][j] = g[i - 1][j];
            }
        }
        lst[s[i] - 'a'] = i;
    }
    for (int j = 0; j < m; j++)
        ans -= g[n][j];
    return ans.val();
}

考虑带修怎么做。

Part A 带修是容易的,有朴素的线段树维护矩阵的做法。

https://loj.ac/p/6074 这题是若干次区间查询本质不同子序列个数。套到本题的线段树一样。

具体的,我们维护 AiA_i 为 ii 的转移矩阵。那么有 Ai=(1  0  0  0  01  1  1  1  10  0  1  0  00  0  0  1  00  0  0  0  1)A_i = \left(\begin{array}{l}1\ \ 0\ \ 0\ \ 0\ \ 0 \\1\ \ 1\ \ 1\ \ 1\ \ 1 \\0\ \ 0\ \ 1\ \ 0\ \ 0 \\0\ \ 0\ \ 0\ \ 1\ \ 0 \\0\ \ 0\ \ 0\ \ 0\ \ 1\end{array}\right),即为单位矩阵加上某一行全填 11。就是第 sis_i 行全填 11。

然后线段树单点修改全局查询矩阵乘积是简单的。

Part B 考虑难在处理 lstlst。我们线段树维护这样的矩阵信息:fid,a,bf_{id,a,b} 表示这个节点 ii 的范围内以 aa 开头 bb 结尾的子序列的方案数。

合并的时候就是 fls,a,b×valid⁡(b,c)×frs,c,df_{ls,a,b}\times \operatorname{valid}(b,c)\times f_{rs,c,d}。valid⁡\operatorname{valid} 即为判断 lsls 的最后一个 bb 和 rsrs 的第一个 cc 中间是否包含其他的 bb 或 cc。

这个形式很像一般的矩阵乘法。我们把 valid⁡\operatorname{valid} 也看为一个矩阵,那么 fid←fls×valid⁡×frsf_{id}\gets f_{ls}\times \operatorname{valid}\times f_{rs} 即可。

这样还有个问题:这只能处理合并左右部分,如果右边某个字符没有出现,左边还有一部分信息要更新上去。这个直接判字符有没有在右边出现就好了。维护的时候维护 lid,c,rid,cl_{id,c},r_{id,c} 表示节点 idid 的第一个 cc 和最后一个 cc 出现位置。用来求 valid⁡\operatorname{valid} 和这部分信息的更新。

总复杂度 O(m3qlog⁡n)\mathcal O(m^3q\log n)。

代码

https://www.luogu.com.cn/record/208779585

C++
#include <bits/stdc++.h>
using namespace std;
template <int P>
class mod_int
{
    using Z = mod_int;

private:
    static int mo(int x) { return x < 0 ? x + P : x; }

public:
    int x;
    int val() const { return x; }
    mod_int() : x(0) {}
    template <class T>
    mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
    bool operator==(const Z &rhs) const { return x == rhs.x; }
    bool operator!=(const Z &rhs) const { return x != rhs.x; }
    Z operator-() const { return Z(x ? P - x : 0); }
    Z pow(long long k) const
    {
        Z res = 1, t = *this;
        while (k)
        {
            if (k & 1)
                res *= t;
            if (k >>= 1)
                t *= t;
        }
        return res;
    }
    Z &operator++()
    {
        x < P - 1 ? ++x : x = 0;
        return *this;
    }
    Z &operator--()
    {
        x ? --x : x = P - 1;
        return *this;
    }
    Z operator++(int)
    {
        Z ret = x;
        x < P - 1 ? ++x : x = 0;
        return ret;
    }
    Z operator--(int)
    {
        Z ret = x;
        x ? --x : x = P - 1;
        return ret;
    }
    Z inv() const { return pow(P - 2); }
    Z &operator+=(const Z &rhs)
    {
        (x += rhs.x) >= P && (x -= P);
        return *this;
    }
    Z &operator-=(const Z &rhs)
    {
        (x -= rhs.x) < 0 && (x += P);
        return *this;
    }
    Z operator-() { return -x; }
    Z &operator*=(const Z &rhs)
    {
        x = 1ULL * x * rhs.x % P;
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                  \
    friend T operator o(const Z &lhs, const Z &rhs) \
    {                                               \
        Z res = lhs;                                \
        return res o## = rhs;                       \
    }
    setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
        friend istream &
        operator>>(istream &is, mod_int &x)
    {
        long long tmp;
        is >> tmp;
        x = tmp;
        return is;
    }
    friend ostream &operator<<(ostream &os, const mod_int &x)
    {
        os << x.val();
        return os;
    }
};
const int P = 998244353;
using Z = mod_int<P>;
const int inf = 1000000000;
int n, q;
const int m = 6;
string s;
struct Matrix
{
    Z a[7][7];
    Matrix() { memset(a, 0, sizeof(a)); }
    void build()
    {
        for (int i = 0; i <= m; i++)
            a[i][i] = 1;
    }
    void clear() { memset(a, 0, sizeof(a)); }
    Z *operator[](int x) { return a[x]; }
} mtx[6], U, V;
Matrix operator*(const Matrix &x, const Matrix &y)
{
    Matrix z;
    for (int i = 0; i < 7; i++)
        for (int k = 0; k < 7; k++)
            if (x.a[i][k] != 0)
                for (int j = 0; j < 7; j++)
                    z.a[i][j] += x.a[i][k] * y.a[k][j];
    return z;
}
Matrix operator+(const Matrix &x, const Matrix &y)
{
    Matrix z;
    for (int i = 0; i < 7; i++)
        for (int j = 0; j < 7; j++)
            z.a[i][j] += x.a[i][j] + y.a[i][j];
    return z;
}
struct SegTree
{
    struct node
    {
        Matrix mtx_1;
        Matrix mtx_2;
        int l[6];
        int r[6];
    } t[200020 << 2];
#define ls id << 1
#define rs id << 1 | 1
    Matrix valid(int l, int r)
    {
        Matrix ret;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < m; j++)
                ret[i][j] = t[l].r[j] <= t[l].r[i] &&
                            t[r].l[j] <= t[r].l[i];
        }
        return ret;
    }
    void pushup_1(int id)
    {
        t[id].mtx_1 = t[ls].mtx_1 * t[rs].mtx_1;
    }
    void pushup_2(int id)
    {
        for (int i = 0; i < m; i++)
        {
            t[id].l[i] = min(t[ls].l[i], t[rs].l[i]);
            t[id].r[i] = max(t[ls].r[i], t[rs].r[i]);
        }
        t[id].mtx_2 = t[ls].mtx_2 * valid(ls, rs) * t[rs].mtx_2;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (t[rs].l[j] == inf)
                    t[id].mtx_2[i][j] += t[ls].mtx_2[i][j];
                if (t[ls].r[i] == -inf)
                    t[id].mtx_2[i][j] += t[rs].mtx_2[i][j];
            }
        }
    }
    void build(int id = 1, int l = 1, int r = n)
    {
        if (l == r)
        {
            int x = s[l] - 'a';
            t[id].mtx_1 = ::mtx[x];
            t[id].mtx_2[x][x] = 1;
            for (int i = 0; i < m; i++)
                t[id].l[i] = inf, t[id].r[i] = -inf;
            t[id].l[x] = t[id].r[x] = l;
            return;
        }
        int mid = l + r >> 1;
        build(ls, l, mid);
        build(rs, mid + 1, r);
        pushup_1(id);
        pushup_2(id);
    }
    void update_1(int pos, int id = 1, int l = 1, int r = n)
    {
        if (r < pos || l > pos)
            return;
        if (pos <= l && r <= pos)
            return t[id].mtx_1 = ::mtx[s[l] - 'a'], void();
        int mid = l + r >> 1;
        update_1(pos, ls, l, mid);
        update_1(pos, rs, mid + 1, r);
        pushup_1(id);
    }
    void update_2(int pos, int x, int y, int id = 1, int l = 1, int r = n)
    {
        if (r < pos || l > pos)
            return;
        if (pos <= l && r <= pos)
        {
            t[id].mtx_2.clear();
            t[id].mtx_2[y][y] = 1;
            t[id].l[x] = inf;
            t[id].r[x] = -inf;
            t[id].l[y] = t[id].r[y] = pos;
            return;
        }
        int mid = l + r >> 1;
        update_2(pos, x, y, ls, l, mid);
        update_2(pos, x, y, rs, mid + 1, r);
        pushup_2(id);
    }
} T;
void init()
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j <= m; j++)
            mtx[i][j][j] = 1;
        for (int j = 0; j <= m; j++)
            mtx[i][i][j] = 1;
    }
}
int solve()
{
    Matrix r = T.t[1].mtx_1;
    Z ans = 0;
    for (int i = 0; i < m; i++)
        ans += r[i][m];
    r = T.t[1].mtx_2;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
            ans -= r[i][j];
    return ans.val();
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> q >> s;
    s = ' ' + s;
    init();
    T.build();
    cout << solve() << '\n';
    while (q--)
    {
        int x;
        char c;
        cin >> x >> c;
        T.update_2(x, s[x] - 'a', c - 'a');
        s[x] = c;
        T.update_1(x);
        cout << solve() << '\n';
    }
    return 0;
}
原文
存档评论1 条抓取于