题解:P5287 [HNOI2019] JOJO

· · 题解

本题需要进行可持久化的操作,我们可以考虑先将所有操作离线并将所有第一种操作建成一棵树。

为简化问题,我们先考虑无还原操作。我们可以考虑将一段同一次插入的字符看成一个字符求出 nxt 数组。这样的做法之所以正确,是因为题目保证每一次插入时当前末尾字符与待插入字符不同。考虑原字符串的 nxt 数组,如果最后一个字符的 nxt 值正好跳到一段同一次插入的字符中间,那新插入的字符一定无法在该位置匹配。我们不断往前跳 nxt,记录新插入字符中已经匹配的个数,并统计答案。如果当前位置后有一段与新插入字符长度和字符都相等的一段字符,则新插入字符的 nxt 就等于它,否则为 0。

再考虑树上操作,发现 KMP 算法的均摊复杂度在树上并不适用,因为可以构造出上面是一条链、底下是菊花状的树。考虑优化,设当前跳到的位置为 p,p 和前面所有字符的总长度为 s_p,如果 2 \times s_{nxt_p}\ge s_p,则说明当前串存在周期,接下来一段 nxt 对答案都没有贡献,也不会成为新字符的 nxt,我们直接跳到周期中最前面的位置就行了,此时长度必然减少一半。如果 2 \times s_{nxt_p}\lt s_p,直接跳 nxt 就行了。时间复杂度 O(nlogn)。

代码示例:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr int maxn = 100000, mod = 998244353, inv2 = 499122177;
int head[maxn + 5], vert[2 * maxn + 5], nt[2 * maxn + 5], tot;
int len[maxn + 5], ans[maxn + 5], pos[maxn + 5], id[maxn + 5], nxt[maxn + 5], last[maxn + 5];
ll sum[maxn + 5];
char c[maxn + 5];

inline void add(int x, int y)
{
    vert[++tot] = y;
    nt[tot] = head[x];
    head[x] = tot;
}

inline int get_sum(ll l, ll r)     //   求 l + (l + 1) + (l + 2) + ... + (r - 1) + r
{
    if (l > r) return 0;
    l %= mod; r %= mod;
    return (l + r) % mod * ((r - l + 1) % mod) % mod * inv2 % mod;
}

void dfs(int x, int f, int dep)    //   dfs 求解答案
{
    id[dep] = x; pos[x] = dep; sum[x] = sum[f] + len[x];    //  id 记录当前字符串,pos 记录深度,sum 记录原字符串总长度
    int cur = f, cnt = 0; ans[x] = ans[f]; last[x] = x;     //  ans 记录答案, last 记录周期的开头
    if (0 == f)
        ans[x] = get_sum(1, len[x] - 1);
    else if (f > 0)
    {
        while (cur >= 0)
        {
            int y = id[pos[nxt[cur]] + 1];
            if (c[y] == c[x])
            {
                ans[x] = (ans[x] + get_sum(sum[nxt[cur]] + cnt + 1, sum[nxt[cur]] + min(len[y], len[x]))) % mod;
                if (len[y] == len[x])
                { cur = y; break; }
                else
                    cnt = max(cnt, min(len[x], len[y]));
            }
            if (last[cur] != cur) cur = last[cur];
            else cur = nxt[cur];
        }
        if (cur >= 0)
        {
            nxt[x] = cur;
            if (pos[cur] << 1 >= pos[x])
                last[x] = last[cur];
        }
        if (-1 == cur && cnt < len[x] && c[id[1]] == c[x])
        {
            ans[x] = (ans[x] + 1ll * (len[x] - cnt) * len[id[1]] % mod) % mod;
            nxt[x] = id[1];
        }
    }

    for (int i = head[x]; i; i = nt[i])
    {
        int y = vert[i];
        dfs(y, x, dep + 1);
    }
}

int main()
{
    int n; scanf("%d", &n);
    for (int i = 1, cur = 0; i <= n; i++)
    {
        int op; scanf("%d", &op);
        if (1 == op)
        {
            scanf("%d %s", &len[i], &c[i]);
            add(cur, i); cur = i; pos[i] = i;
        }
        else
        {
            int last; scanf("%d", &last);
            ans[i] = -1; cur = pos[last];
            pos[i] = cur;
        }
    }
    nxt[0] = -1;
    dfs(0, -1, 0);

    for (int i = 1; i <= n; i++)
        if (ans[i] >= 0)
            printf("%d\n", (ans[i] + mod) % mod);
        else
            printf("%d\n", (ans[pos[i]] + mod) % mod);

    return 0;
}