Solution -「洛谷 P6021」洪水
Link.
给定一棵 个点的带点权树,删除 点的代价是该点点权 。 次操作:
- 修改单点点权。
- 询问让某棵子树的根不可到达子树内任意一片叶子的代价。
。
不考虑修改,列出 DP:
单独拿出实儿子 :
定义矩乘的 为加法, 为取 ,有:
用 LCT / 树剖维护。若使用 LCT,询问 子树时,应 原树上 的父亲,再 ,就能保证当前 的实链全部在子树内,输出 维护的矩乘答案即可。
还有一点,虽然 DP 是自下而上的,但把矩乘展开却是由 向实儿子走的。注意乘法顺序。
询问的时候 成了 LCT 上的父亲调哭了 qwq。
#include <cstdio>
#define calc( x ) ( min_ ( S[x][0][0], S[x][0][1] ) )
typedef long long LL;
const int MAXN = 2e5;
const LL INF = 1e14;
int n, m, ecnt, a[MAXN + 5], head[MAXN + 5];
int srcfa[MAXN + 5], fa[MAXN + 5], ch[MAXN + 5][2];
inline LL min_ ( const LL a, const LL b ) { return a < b ? a : b; }
inline char fgc () {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread ( p = buf, 1, 1 << 17, stdin ), p == q ) ? EOF : *p ++;
}
inline int rint () {
int x = 0, f = 1; char s = fgc ();
for ( ; s < '0' || '9' < s; s = fgc () ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = fgc () ) x = x * 10 + ( s ^ '0' );
return x * f;
}
inline void wint ( LL x ) {
if ( x < 0 ) putchar ( '-' ), x = -x;
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
}
struct Edge { int to, nxt; } graph[MAXN * 2 + 5];
struct Matrix {
LL mat[2][2];
Matrix (): mat { INF, INF, INF, INF } {}
inline LL* operator [] ( const int key ) { return mat[key]; }
inline Matrix operator * ( Matrix& t ) {
Matrix ret;
for ( int i = 0; i < 2; ++ i ) {
for ( int k = 0; k < 2; ++ k ) {
for ( int j = 0; j < 2; ++ j ) {
ret[i][j] = min_ ( ret[i][j], mat[i][k] + t[k][j] );
}
}
}
return ret;
}
} G[MAXN + 5], S[MAXN + 5];
inline void link ( const int s, const int t ) {
graph[++ ecnt] = { t, head[s] };
head[s] = ecnt;
}
inline bool nroot ( const int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
inline void pushup ( const int x ) {
S[x] = G[x];
if ( ch[x][0] ) S[x] = S[ch[x][0]] * S[x];
if ( ch[x][1] ) S[x] = S[x] * S[ch[x][1]];
}
inline void rotate ( const int x ) {
int y = fa[x], z = fa[y], k = ch[y][1] == x;
fa[x] = z; if ( nroot ( y ) ) ch[z][ch[z][1] == y] = x;
ch[y][k] = ch[x][k ^ 1]; if ( ch[x][k ^ 1] ) fa[ch[x][k ^ 1]] = y;
pushup ( ch[fa[y] = x][k ^ 1] = y ), pushup ( x );
}
inline void splay ( const int x ) {
for ( int y; nroot ( x ); rotate ( x ) ) {
if ( nroot ( y = fa[x] ) ) {
rotate ( x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y );
}
}
pushup ( x );
}
inline void access ( int x ) {
for ( int y = 0; x; x = fa[y = x] ) {
splay ( x );
if ( ch[x][1] ) G[x][0][0] += calc ( ch[x][1] );
if ( y ) G[x][0][0] -= calc ( y );
ch[x][1] = y, pushup ( x );
}
}
inline LL initDP ( const int u, const int fath ) {
LL sum = INF; fa[u] = srcfa[u] = fath;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fath ) {
if ( sum == INF ) sum = 0;
sum += initDP ( v, u );
}
}
G[u][0][0] = sum, G[u][0][1] = a[u], G[u][1][1] = 0;
return S[u] = G[u], min_ ( sum, a[u] );
}
int main () {
n = rint ();
for ( int i = 1; i <= n; ++ i ) a[i] = rint ();
for ( int i = 1, u, v; i < n; ++ i ) {
u = rint (), v = rint ();
link ( u, v ), link ( v, u );
}
initDP ( 1, 0 );
m = rint ();
for ( int x, t, op; m --; ) {
for ( op = fgc (); op < 'A' || 'Z' < op; op = fgc () );
x = rint ();
if ( op == 'Q' ) {
if ( srcfa[x] ) access ( srcfa[x] );
splay ( x );
wint ( calc ( x ) ), putchar ( '\n' );
} else {
t = rint ();
access ( x ), splay ( x );
G[x][0][1] += t, pushup ( x );
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现