\(\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。

  1. #include <cstdio>
  2. #define calc( x ) ( min_ ( S[x][0][0], S[x][0][1] ) )
  3. typedef long long LL;
  4. const int MAXN = 2e5;
  5. const LL INF = 1e14;
  6. int n, m, ecnt, a[MAXN + 5], head[MAXN + 5];
  7. int srcfa[MAXN + 5], fa[MAXN + 5], ch[MAXN + 5][2];
  8. inline LL min_ ( const LL a, const LL b ) { return a < b ? a : b; }
  9. inline char fgc () {
  10. static char buf[1 << 17], *p = buf, *q = buf;
  11. return p == q && ( q = buf + fread ( p = buf, 1, 1 << 17, stdin ), p == q ) ? EOF : *p ++;
  12. }
  13. inline int rint () {
  14. int x = 0, f = 1; char s = fgc ();
  15. for ( ; s < '0' || '9' < s; s = fgc () ) f = s == '-' ? -f : f;
  16. for ( ; '0' <= s && s <= '9'; s = fgc () ) x = x * 10 + ( s ^ '0' );
  17. return x * f;
  18. }
  19. inline void wint ( LL x ) {
  20. if ( x < 0 ) putchar ( '-' ), x = -x;
  21. if ( 9 < x ) wint ( x / 10 );
  22. putchar ( x % 10 ^ '0' );
  23. }
  24. struct Edge { int to, nxt; } graph[MAXN * 2 + 5];
  25. struct Matrix {
  26. LL mat[2][2];
  27. Matrix (): mat { INF, INF, INF, INF } {}
  28. inline LL* operator [] ( const int key ) { return mat[key]; }
  29. inline Matrix operator * ( Matrix& t ) {
  30. Matrix ret;
  31. for ( int i = 0; i < 2; ++ i ) {
  32. for ( int k = 0; k < 2; ++ k ) {
  33. for ( int j = 0; j < 2; ++ j ) {
  34. ret[i][j] = min_ ( ret[i][j], mat[i][k] + t[k][j] );
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. } G[MAXN + 5], S[MAXN + 5];
  41. inline void link ( const int s, const int t ) {
  42. graph[++ ecnt] = { t, head[s] };
  43. head[s] = ecnt;
  44. }
  45. inline bool nroot ( const int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; }
  46. inline void pushup ( const int x ) {
  47. S[x] = G[x];
  48. if ( ch[x][0] ) S[x] = S[ch[x][0]] * S[x];
  49. if ( ch[x][1] ) S[x] = S[x] * S[ch[x][1]];
  50. }
  51. inline void rotate ( const int x ) {
  52. int y = fa[x], z = fa[y], k = ch[y][1] == x;
  53. fa[x] = z; if ( nroot ( y ) ) ch[z][ch[z][1] == y] = x;
  54. ch[y][k] = ch[x][k ^ 1]; if ( ch[x][k ^ 1] ) fa[ch[x][k ^ 1]] = y;
  55. pushup ( ch[fa[y] = x][k ^ 1] = y ), pushup ( x );
  56. }
  57. inline void splay ( const int x ) {
  58. for ( int y; nroot ( x ); rotate ( x ) ) {
  59. if ( nroot ( y = fa[x] ) ) {
  60. rotate ( x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y );
  61. }
  62. }
  63. pushup ( x );
  64. }
  65. inline void access ( int x ) {
  66. for ( int y = 0; x; x = fa[y = x] ) {
  67. splay ( x );
  68. if ( ch[x][1] ) G[x][0][0] += calc ( ch[x][1] );
  69. if ( y ) G[x][0][0] -= calc ( y );
  70. ch[x][1] = y, pushup ( x );
  71. }
  72. }
  73. inline LL initDP ( const int u, const int fath ) {
  74. LL sum = INF; fa[u] = srcfa[u] = fath;
  75. for ( int i = head[u], v; i; i = graph[i].nxt ) {
  76. if ( ( v = graph[i].to ) ^ fath ) {
  77. if ( sum == INF ) sum = 0;
  78. sum += initDP ( v, u );
  79. }
  80. }
  81. G[u][0][0] = sum, G[u][0][1] = a[u], G[u][1][1] = 0;
  82. return S[u] = G[u], min_ ( sum, a[u] );
  83. }
  84. int main () {
  85. n = rint ();
  86. for ( int i = 1; i <= n; ++ i ) a[i] = rint ();
  87. for ( int i = 1, u, v; i < n; ++ i ) {
  88. u = rint (), v = rint ();
  89. link ( u, v ), link ( v, u );
  90. }
  91. initDP ( 1, 0 );
  92. m = rint ();
  93. for ( int x, t, op; m --; ) {
  94. for ( op = fgc (); op < 'A' || 'Z' < op; op = fgc () );
  95. x = rint ();
  96. if ( op == 'Q' ) {
  97. if ( srcfa[x] ) access ( srcfa[x] );
  98. splay ( x );
  99. wint ( calc ( x ) ), putchar ( '\n' );
  100. } else {
  101. t = rint ();
  102. access ( x ), splay ( x );
  103. G[x][0][1] += t, pushup ( x );
  104. }
  105. }
  106. return 0;
  107. }

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. Java 递归 常见24道题目 总结

    1.N个台阶的走法递归[这里设为10个台阶] /** * N个台阶的走法递归 * <p> * 有个楼梯,台阶有10个,每次可以跳上1阶 或者 2阶 ,那么台阶的走法一共有多少种 */ @T ...

  2. [vscode] os.getcwd(),调试和命令行运行的结果不一致

    问题描述: 调试和命令行运行的时候工作目录不一致 这会导致一个问题,我想从上级目录导入模块的话,F5调试就会找不到模块,而命令行则没问题 那么我该如何调试呢? 目录结构: top  └ folder_ ...

  3. 使用.NET 6开发TodoList应用(24)——实现基于JWT的Identity功能

    系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 在.NET Web API开发中还有一个很重要的需求是关于身份认证和授权的,这个主题非常大,所以本文不打算面面俱到地介绍整个主 ...

  4. 蓝桥杯ALGO-1003

    问题描述 JiaoShou在爱琳大陆的旅行完毕,即将回家,为了纪念这次旅行,他决定带回一些礼物给好朋友. 在走出了怪物森林以后,JiaoShou看到了排成一排的N个石子. 这些石子很漂亮,JiaoSh ...

  5. [BJDCTF2020]EzPHP-POP链

    那次某信内部比赛中有道pop链问题的题目,我当时没有做出来,所以在此总结一下,本次以buu上复现的[MRCTF2020]Ezpop为例. 题目 1 Welcome to index.php 2 < ...

  6. Docker入门篇(一)安装docker

    Docker入门篇(一)安装docker Docker的来源 由dotCloud公司首创及正式命名,但是企业规模小,影响力不够,所以在快要坚持不住的时候,开始吃百家饭--开源了.不开则已,一开惊人.越 ...

  7. (转)Make 命令

    Make 命令 代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目 ...

  8. 【刷题-PAT】A1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  9. unity3d百度语音+图灵机器人

    using NAudio.Wave; using System; using System.Collections; using System.Collections.Generic; using S ...

  10. MongoDB常用运维命令

    # 查看Mongodb版本信息 mongos> db.version() # 关闭mongodb服务 mongos> use admin mongos> shutdownServer ...