考虑动态 dp 的套路,先树剖一下,令 \(son(x)\) 为点 \(x\) 的重儿子。 \(g_x=\prod\limits_{u\in C(x)\land u\neq son(x)}f_u\)。
于是有 \(f_x=f_{son(x)}g_x+a_x\)。
于是 \(\begin{bmatrix}f_x&1\end{bmatrix}=\begin{bmatrix}f_{son(x)}&1\end{bmatrix}\begin{bmatrix}g_x&0\\ a_x&1\end{bmatrix}\)。
然后直接线段树维护矩阵乘法就行了。
不懂的可以做一下 P4719。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N = 2e5 + 5, p = 998244353;
int dfn[N], top[N], dn[N], fa[N], son[N], sz[N], a[N], c[N], f[N], g[N], g2[N], ts, n;
bool zz[N];
vector<int> e[N];
struct M
{
int a[2][2];
M operator*(M x)
{
M y; memset(y.a, 0, sizeof y.a);
for(int i = 0; i < 2; i ++) for(int j = 0; j < 2; j ++) for(int k = 0; k < 2; k ++)
(y.a[i][k] += a[i][j] * x.a[j][k]) %= p;
return y;
}
};
struct sgt
{
M a[N << 2];
void pu(int x) {a[x] = a[x << 1 | 1] * a[x << 1];}
void upd(int q, int l, int r, int x, M v)
{
if(l == r) return a[x] = v, void();
int mid = l + r >> 1;
if(mid >= q) upd(q, l, mid, x << 1, v);
else upd(q, mid + 1, r, x << 1 | 1, v);
pu(x);
}
M qry(int ql, int qr, int l, int r, int x)
{
if(ql <= l && r <= qr) return a[x];
int mid = l + r >> 1;
if(mid < ql) return qry(ql, qr, mid + 1, r, x << 1 | 1);
if(mid >= qr) return qry(ql, qr, l, mid, x << 1);
return qry(ql, qr, mid + 1, r, x << 1 | 1) * qry(ql, qr, l, mid, x << 1);
}
}t;
void dfs1(int x, int fa)
{
sz[x] = 1;
::fa[x] = fa;
f[x] = 1;
for(int i : e[x])
{
dfs1(i, x);
f[x] = f[x] * f[i] % p;
sz[x] += sz[i];
if(sz[i] >= sz[son[x]]) son[x] = i;
}
if(e[x].empty()) f[x] = 0;
f[x] = (f[x] + a[x]) % p;
zz[x] = f[x] == 0;
}
void dfs2(int x, int tp)
{
dfn[x] = ++ts;
top[x] = tp;
dn[tp] = x;
if(son[x]) dfs2(son[x], tp);
g[x] = g2[x] = 1;
for(int i : e[x])
if(i != son[x])
{
dfs2(i, i);
c[x] += !f[i];
g[x] = g[x] * f[i] % p;
if(f[i]) g2[x] = g2[x] * f[i] % p;
}
if(e[x].empty()) g[x] = g2[x] = 0;
t.upd(dfn[x], 1, n, 1, {g[x], 0, a[x], 1});
}
int qpow(int a, int b)
{
if(!b) return 1;
return ((b & 1) ? a : 1ll) * qpow(a * a % p, b >> 1) % p;
}
void upd(int u, int v)
{
M x = t.qry(dfn[u], dfn[u], 1, n, 1);
x.a[1][0] = v;
t.upd(dfn[u], 1, n, 1, x);
while(fa[top[u]])
{
u = top[u];
M y = t.qry(dfn[u], dfn[dn[u]], 1, n, 1);
int nf = y.a[1][0];
if(nf && zz[u]) c[fa[u]] --, zz[u] = 0;
else if(nf == 0 && !zz[u]) c[fa[u]] ++, zz[u] = 1;
M z = t.qry(dfn[fa[u]], dfn[fa[u]], 1, n, 1);
if(f[u]) g2[fa[u]] = qpow(f[u], p - 2) * g2[fa[u]] % p;
if(c[fa[u]]) g[fa[u]] = 0;
else
{
g2[fa[u]] = g2[fa[u]] * nf % p;
g[fa[u]] = g2[fa[u]];
}
f[u] = nf;
z.a[0][0] = g[fa[u]];
t.upd(dfn[fa[u]], 1, n, 1, z);
u = fa[u];
}
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);
int q;
cin >> n >> q;
for(int i = 2; i <= n; i ++)
{
int x; cin >> x;
e[x].push_back(i);
}
for(int i = 1; i <= n; i ++) cin >> a[i];
dfs1(1, 0);
dfs2(1, 1);
while(q --)
{
int x, v; cin >> x >> v;
upd(x, v);
cout << t.qry(dfn[1], dfn[dn[1]], 1, n, 1).a[1][0] << "\n";
}
return 0;
}
标签:g2,int,题解,son,fa,dfn,zz,ABC351G
From: https://www.cnblogs.com/adam01/p/18162698