1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; namespace Legendgod { namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char *iS, *iT, buf[Siz]; #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ ) #define getchar gc #endif template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); } #undef getchar }
using namespace Read;
const int maxn = 2e5 + 5; constexpr int mod = 998244353; typedef pair<int, int> pr; int n, m, ans[maxn], ln[maxn], fl[maxn], fl1[maxn], ed(0), prs[maxn], go[maxn]; vector<int> vc[maxn]; char s[10]; pr a[maxn]; bool vis[maxn]; int clc(int l,int r) { return (1ll * (l + r) * (r - l + 1) >> 1) % mod; } pair<int*, int> st[maxn * 6]; pr b[maxn];
void Ins(int &a,int b) { st[++ ed] = {&a, a}, a = b; } void Roll(int x) { while(ed > x) { *(st[ed].first) = st[ed].second; -- ed; } } void Add(int &a,long long b) { a = (a + b) % mod; }
void Insert(int p,int c,int x) {
int l = ++ ln[p]; Ins(b[l].first, c), Ins(b[l].second, x), Ins(prs[l], prs[l - 1] + x); if(l == 1) return Add(ans[p], clc(0, x - 1)); int np = fl[l - 1], lim = 1;
while(np >= 0 && b[np + 1] != b[l]) { if(b[np + 1].first == b[l].first && b[np + 1].second >= lim && lim <= x) { Add(ans[p], clc(prs[np] + lim, prs[np] + min(x, b[np + 1].second))); lim = min(b[np + 1].second, x) + 1;
} if(go[np + 1]) np = fl1[np + 1] - 1; else np = fl[np]; } ++ np; if(np && (l - np) * 2 <= l) Ins(go[l], 1), Ins(fl1[l], fl[l % (l - np) + l - np]); if(!np && b[1].first == c && b[1].second <= x) ++ np; if(np) { if(b[np].second >= lim && lim <= x) { Add(ans[p], clc(prs[np - 1] + lim, prs[np - 1] + min(b[np].second, x))); lim = min(b[np].second, x) + 1; } if(np == 1 && lim <= x) { Add(ans[p], 1ll * prs[np] * (x - lim + 1)); lim = x + 1; } }
Ins(fl[l], np); }
void dfs(int p) { int tmp = ed; if(vis[p]) Insert(p, a[p].first, a[p].second); for(int v : vc[p]) { ln[v] = ln[p], ans[v] = ans[p]; dfs(v); } Roll(tmp); }
signed main() { int i, j; r1(n); fl[0] = -1; for(i = 1; i <= n; ++ i) { int opt, x; r1(opt, x); if(opt == 2) { vc[x].emplace_back(i); continue; } scanf("%s", s + 1); a[i] = { s[1] - 'a' + 1, x }, vis[i] = 1; vc[i - 1].emplace_back(i); } dfs(0); for(i = 1; i <= n; ++ i) printf("%d\n", ans[i]); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|