题解:P8334 [ZJOI2022] 深搜
UKE_Automation · · 题解
[ZJOI2022] 深搜
首先最小值期望不好直接转移,所以考虑拆成最小值乘上概率。如果用传统的树形 dp 状态数必然是两维的,承受不了,考虑另一种思路。我们从大往小枚举当前最小值
接下来令权值
考虑转移,先判掉
然后我们再设
先把
显然这可以写成
最后我们再维护一下轻儿子
这样的话用全局平衡二叉树维护一下就可以做到
#include <bits/stdc++.h>
#define il inline
using namespace std;
const int Maxn = 8e5 + 5;
const int Inf = 2e9;
const int Mod = 998244353;
il int Add(int x, int y) {return x + y >= Mod ? x + y - Mod: x + y;} il void pls(int &x, int y) {x = Add(x, y);}
il int Del(int x, int y) {return x - y < 0 ? x - y + Mod : x - y;} il void sub(int &x, int y) {x = Del(x, y);}
il int qpow(int a, int b) {int res = 1; for(; b; a = 1ll * a * a % Mod, b >>= 1) if(b & 1) res = 1ll * res * a % Mod; return res;}
il int Inv(int a) {return qpow(a, Mod - 2);}
template <typename T> il void chkmax(T &x, T y) {x = (x >= y ? x : y);}
template <typename T> il void chkmin(T &x, T y) {x = (x <= y ? x : y);}
template <typename T>
il void read(T &x) {
x = 0; char ch = getchar(); bool flg = 0;
for(; ch < '0' || ch > '9'; ch = getchar()) flg = (ch == '-');
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
flg ? x = -x : 0;
}
template <typename T>
il void write(T x, bool typ = 1) {
static short Stk[50], Top = 0;
x < 0 ? putchar('-'), x = -x : 0;
do Stk[++Top] = x % 10, x /= 10; while(x);
while(Top) putchar(Stk[Top--] | 48);
typ ? putchar('\n') : putchar(' ');
}
il void IOS() {ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);}
il void File() {freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);}
bool Beg;
int T, n, rt;
int a[Maxn], b[Maxn], tot;
int head[Maxn], edgenum;
struct node {
int nxt, to;
}edge[Maxn << 1];
void add(int u, int v) {
edge[++edgenum] = {head[u], v}; head[u] = edgenum;
edge[++edgenum] = {head[v], u}; head[v] = edgenum;
}
vector <int> m1[Maxn], m2[Maxn];
int fth[Maxn], siz[Maxn], son[Maxn], mn[Maxn];
int col[Maxn], tag[Maxn], cnt[Maxn], f[Maxn], sf[Maxn][2], g[Maxn], sg[Maxn];
void dfs1(int x, int f) {
siz[x] = 1; mn[x] = a[x]; fth[x] = f;
for(int i = head[x]; i; i = edge[i].nxt) {
int to = edge[i].to;
if(to == f) continue;
cnt[x]++;
dfs1(to, x); chkmin(mn[x], mn[to]); siz[x] += siz[to];
if(siz[son[x]] < siz[to]) son[x] = to;
}
m2[mn[x]].push_back(x);
}
int inv[Maxn];
struct Mat {
int a, b, c, d;
Mat operator * (Mat y) {return {1ll * a * y.a % Mod, Add(1ll * a * y.b % Mod, b), Add(1ll * c * y.a % Mod, y.c), Add(1ll * c * y.b % Mod, Add(d, y.d))};}
}p[Maxn];
int root;
int bel2[Maxn];
namespace GBT {
struct node {
int l, r, fa;
Mat sum;
}t[Maxn];
int bel[Maxn];
#define ls(p) t[p].l
#define rs(p) t[p].r
#define fa(p) t[p].fa
int b[Maxn], bs[Maxn], tot;
void clear() {
for(int i = 1; i <= n; i++) ls(i) = rs(i) = fa(i) = t[i].sum.a = t[i].sum.b = t[i].sum.c = t[i].sum.d = 0;
}
int cbuild(int l, int r) {
int L = l, R = r, res = 0;
while(L <= R) {
int mid = (L + R) >> 1;
if(2 * (bs[mid - 1] - bs[l - 1]) <= bs[r] - bs[l - 1]) res = mid, L = mid + 1;
else R = mid - 1;
}
int rt = b[res];
if(l < res) ls(rt) = cbuild(l, res - 1), fa(ls(rt)) = rt;
if(r > res) rs(rt) = cbuild(res + 1, r), fa(rs(rt)) = rt;
return rt;
}
int build(int x) {
int y = x;
do {
for(int i = head[x]; i; i = edge[i].nxt) {
int to = edge[i].to;
if(to == son[x] || to == fth[x]) continue;
fa(build(to)) = x;
}
bel[x] = y;
}while(x = son[x]);
tot = 0;
do {
b[++tot] = y;
bs[tot] = bs[tot - 1] + siz[y] - siz[son[y]];
}while(y = son[y]);
int rt = cbuild(1, tot);
for(int i = 1; i <= tot; i++) bel2[b[i]] = rt;
return rt;
}
void pushup(int x) {
t[x].sum = p[x];
if(ls(x)) t[x].sum = t[ls(x)].sum * t[x].sum;
if(rs(x)) t[x].sum = t[x].sum * t[rs(x)].sum;
}
int isroot(int x) {return ls(fa(x)) != x && rs(fa(x)) != x;}
void reset(int x) {
int k = col[x] * inv[cnt[x] + tag[son[x]]];
int b = col[x] * Add(Add(1ll * inv[cnt[x]] * sf[x][0] % Mod, 1ll * inv[cnt[x] + 1] * sf[x][1] % Mod), 1);
int g = sg[x];
p[x] = {k, b, k, Add(b, g)}; pushup(x);
}
void mdf(int x) {
bool flg = 1;
while(x) {
if(flg) reset(x);
else pushup(x);
if(isroot(x) && fa(x)) {
sub(sf[fa(x)][tag[bel[x]]], f[x]), sub(sg[fa(x)], g[x]);
f[x] = t[x].sum.b, g[x] = t[x].sum.d;
pls(sf[fa(x)][tag[bel[x]]], f[x]), pls(sg[fa(x)], g[x]);
flg = 1;
}
else flg = 0;
x = fa(x);
}
}
}
int ans[Maxn];
void init() {
tot = edgenum = 0;
for(int i = 1; i <= n; i++) {
ans[i] = p[i].a = p[i].b = p[i].c = p[i].d = son[i] = head[i] = col[i] = tag[i] = cnt[i] = sf[i][0] = sf[i][1] = sg[i] = f[i] = g[i] = 0;
m1[i].clear(), m2[i].clear();
}
GBT::clear();
}
void solve() {
init();
read(n), read(rt);
for(int i = 1; i <= n; i++) read(a[i]), b[++tot] = a[i];
sort(b + 1, b + tot + 1); tot = unique(b + 1, b + tot + 1) - b - 1;
for(int i = 1; i <= n; i++) m1[a[i] = lower_bound(b + 1, b + tot + 1, a[i]) - b].push_back(i);
for(int i = 1, u, v; i < n; i++) {
read(u), read(v);
add(u, v);
}
inv[1] = 1;
for(int i = 2; i <= n; i++) inv[i] = 1ll * inv[Mod % i] * (Mod - Mod / i) % Mod;
dfs1(rt, 0);
root = GBT::build(rt);
for(int i = tot; i >= 1; i--) {
for(auto x : m1[i]) col[x] = 1, GBT::mdf(x);
for(auto x : m2[i]) {
tag[x] = 1;
if(!fth[x]) continue;
if(son[fth[x]] == x) cnt[fth[x]]--, GBT::mdf(fth[x]);
else {
int sum = f[bel2[x]];
sub(sf[fth[x]][0], sum), pls(sf[fth[x]][1], sum);
cnt[fth[x]]--;
GBT::mdf(fth[x]);
}
}
ans[i] = GBT::t[root].sum.d;
}
int res = 0;
for(int i = tot; i >= 1; i--) pls(res, 1ll * Del(ans[i], ans[i + 1]) * b[i] % Mod);
write(res);
}
bool End;
il void Usd() {cerr << (&Beg - &End) / 1024.0 / 1024.0 << "MB " << (double)clock() * 1000.0 / CLOCKS_PER_SEC << "ms\n"; }
int main() {
read(T);
while(T--) solve();
Usd();
return 0;
}