\(\mathcal{Description}\)

  Link.

  给定一棵 \(n\) 个点的带点权树,删除 \(u\) 点的代价是该点点权 \(a_u\)。\(m\) 次操作:

  • 修改单点点权。
  • 询问让某棵子树的根不可到达子树内任意一片叶子的代价。

  \(n,m\le2\times10^5\)。

\(\mathcal{Solution}\)

  不考虑修改,列出 DP:

\[f(u)=\begin{cases}a_u&u\text{ is leaf}\\\min\{a_u,\sum_vf(v)\}&\text{otherwise}\end{cases}
\]

  单独拿出实儿子 \(s_u\):

\[g(u)=\begin{cases}+\infty&u\text{ is leaf}\\\sum_{v\not=s_u}f(v)&\text{otherwise}\end{cases}\\\Rightarrow f(u)=\min\{a_u,f(s_u)+g(u)\}
\]

  定义矩乘的 \(+\) 为加法,\(\times\) 为取 \(\min\),有:

\[\begin{bmatrix}f(u)\\0\end{bmatrix}=\begin{bmatrix}g(u)&a_u\\+\infty&0\end{bmatrix}\begin{bmatrix}f(s_u)\\0\end{bmatrix}
\]

  用 LCT / 树剖维护。若使用 LCT,询问 \(u\) 子树时,应 \(\operatorname{access}\) 原树上 \(u\) 的父亲,再 \(\operatorname{splay}\) \(u\),就能保证当前 \(u\) 的实链全部在子树内,输出 \(u\) 维护的矩乘答案即可。

  还有一点,虽然 DP 是自下而上的,但把矩乘展开却是由 \(u\) 向实儿子走的。注意乘法顺序。

\(\mathcal{Code}\)

  询问的时候 \(\operatorname{access}\) 成了 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;
}

Solution -「洛谷 P6021」洪水的更多相关文章

  1. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  2. Note/Solution -「洛谷 P5158」「模板」多项式快速插值

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...

  3. Solution -「洛谷 P4198」楼房重建

    \(\mathcal{Description}\)   Link.   给定点集 \(\{P_n\}\),\(P_i=(i,h_i)\),\(m\) 次修改,每次修改某个 \(h_i\),在每次修改后 ...

  4. Solution -「洛谷 P6577」「模板」二分图最大权完美匹配

    \(\mathcal{Description}\)   Link.   给定二分图 \(G=(V=X\cup Y,E)\),\(|X|=|Y|=n\),边 \((u,v)\in E\) 有权 \(w( ...

  5. Solution -「洛谷 P4719」「模板」"动态 DP" & 动态树分治

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个结点的带权树,\(m\) 次单点点权修改,求出每次修改后的带权最大独立集.   \(n,m\le10^5 ...

  6. Solution -「洛谷 P5236」「模板」静态仙人掌

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的仙人掌,\(q\) 组询问两点最短路.   \(n,q\le10^4\),\(m\ ...

  7. Solution -「洛谷 P4320」道路相遇

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的连通无向图,并给出 \(q\) 个点对 \((u,v)\),询问 \(u\) 到 ...

  8. Solution -「洛谷 P5827」边双连通图计数

    \(\mathcal{Description}\)   link.   求包含 \(n\) 个点的边双连通图的个数.   \(n\le10^5\). \(\mathcal{Solution}\)    ...

  9. Solution -「洛谷 P5827」点双连通图计数

    \(\mathcal{Description}\)   link.   求有 \(n\) 个结点的点双连通图的个数,对 \(998244353\) 取模.   \(n\le10^5\). \(\mat ...

随机推荐

  1. spring security 继承 WebSecurityConfigurerAdapter 的重写方法configure() 参数 HttpSecurity 常用方法及说明

    HttpSecurity 常用方法及说明 方法 说明 openidLogin() 用于基于 OpenId 的验证 headers() 将安全标头添加到响应 cors() 配置跨域资源共享( CORS ...

  2. LINUX学习-PHP安装

    一.安装环境 操作系统CentOS6.8 关闭SeLinux和iptables防火墙 二.网络yum源 将下面的软件下载到  /etc/yum.repos.d/   的目录下 官方基础:http:// ...

  3. Git 的基本命令的使用

    1.获得Git仓库(克隆一份代码到本地仓库) git clone url 2.更新本地的代码 git pull 3.查看本地修改的文件 git status 4.将本地的修改加到stage中 git ...

  4. 我的2021年度总结-回忆录|附旅行Vlog

    今天是农历腊月初十,还有20天就是2022年了.这一年,些许遗憾,些许期盼.时间久了,很多事已经慢慢模糊了,只记得,这最后几个月的闲碎小事. 不止多久,很久没有码字了.有些事,记不清,忆不得.时至今年 ...

  5. py3nvml实现GPU相关信息读取

    技术背景 随着模型运算量的增长和硬件技术的发展,使用GPU来完成各种任务的计算已经渐渐成为算法实现的主流手段.而对于运行期间的一些GPU的占用,比如每一步的显存使用率等诸如此类的信息,就需要一些比较细 ...

  6. 【漏洞复现】CVE-2022–21661 WordPress核心框架WP_Query SQL注入漏洞原理分析与复现

    影响版本 wordpress < 5.8.3 分析 参考:https://blog.csdn.net/qq_46717339/article/details/122431779 在 5.8.3 ...

  7. Docker 与 K8S学习笔记(十 二)容器间数据共享

    数据共享是volume的关键特性,今天我们来看一下通过volume实现容器与host.容器与容器之间共享数据. 一.容器与host共享数据 在上一篇中介绍到的bind mount和docker man ...

  8. 一个小程序:Instrumentation的使用

    本来是想练习Matrix的,没想到写了一个自定义View,监听它的ASWD键后,不知道该如何按下ASWD(手机上一般都没实体按键了).于是: 一个自定义View: public class MyVie ...

  9. leetcode 1218. 最长定差子序列

    问题描述 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等差子序列的长度.   示例 ...

  10. 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...