LuoguP4719 【模板】动态 DP(动态DP,LCT)
\(n \times m\)的算法谁都会吧,注意到每次修改影响的仅是一部分的信息,因此可思考优化。
将每个节点对应一个矩阵\(\begin{bmatrix} g[v][0] & g[v][0] \\ g[v][1] & -\infty \end{bmatrix}\) ,从而 \(\begin{bmatrix} g[v][0] & g[v][0] \\ g[v][1] & -\infty \end{bmatrix} \times \begin{bmatrix} f[son[u]][0] \\ f[son[u]][1] \end{bmatrix} = \begin{bmatrix} f[u][0] \\ f[u][1] \end{bmatrix}\),\(LCT\)虚实相生,维护子树信息
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a,b,sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e(x) cout << (#x) << " : " << x << "\n"
#define D_e_Line printf("\n----------------\n")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt","w", stdout)
#define Pause() system("pause")
#define TIME() fprintf(stderr, "TIME : %.3lfms\n", clock() / CLOCKS_PER_SEC)
#endif
struct FastIO {
template<typename ATP> inline FastIO& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x =x * 10 + (c ^ '0'), c = getchar();
x = f == 1 ? x : -x;
return *this;
}
} io;
using namespace std;
template<typename ATP> inline ATP Max(ATP x, ATP y) {
return x > y ? x : y;
}
template<typename ATP> inline ATP Min(ATP x, ATP y) {
return x < y ? x : y;
}
const int N = 1e6 + 7;
struct Matrix {
int mat[2][2];
Matrix() {
Fill(mat, 0);
}
inline void New(const int &A, const int &B) {
mat[0][0] = mat[0][1] = A;
mat[1][0] = B, mat[1][1] = -0x3f3f3f3f;
/*
[ g_u0, g_u0
g_u1, -inf ]
*/
}
inline int Max(){
return ::Max(mat[0][0], mat[1][0]);
}
inline Matrix operator * (const Matrix &b) const {
Matrix c;
R(i,0,1){
R(j,0,1){
c.mat[i][j] = ::Max(mat[i][0] + b.mat[0][j], mat[i][1] + b.mat[1][j]);
}
}
return c;
}
};
struct Edge {
int nxt, pre;
} e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v) {
e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
struct nod {
int ch[2], fa, f[2];
Matrix x;
} t[N];
#define ls t[u].ch[0]
#define rs t[u].ch[1]
inline int Ident(int u) {
return t[t[u].fa].ch[1] == u;
}
inline bool Isroot(int u) {
return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
}
inline void Pushup(int u) {
t[u].x.New(t[u].f[0], t[u].f[1]);
if(ls) t[u].x = t[ls].x * t[u].x;
if(rs) t[u].x = t[u].x * t[rs].x;
}
inline void Rotate(int x) {
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[x].fa = z; if(!Isroot(y)) t[z].ch[Ident(y)] = x;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x) {
while(!Isroot(x)){
int y = t[x].fa;
if(!Isroot(y))
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
Rotate(x);
}
Pushup(x);
}
inline void Access(int u) {
for(register int v = 0; u; v = u, u = t[u].fa){
Splay(u);
// the past comes to be my power, the future just lies
if(rs){
t[u].f[0] += t[rs].x.Max();
t[u].f[1] += t[rs].x.mat[0][0];
}
if(v){
t[u].f[0] -= t[v].x.Max();
t[u].f[1] -= t[v].x.mat[0][0];
}
rs = v;
Pushup(u);
}
}
int val[N];
inline void DFS(int u, int father) {
// a simple DP on tree, for the first blood
t[u].f[1] = val[u];
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == father) continue;
t[v].fa = u;
DFS(v, u);
t[u].f[0] += Max(t[v].f[0], t[v].f[1]);
t[u].f[1] += t[v].f[0];
}
t[u].x.New(t[u].f[0], t[u].f[1]); // so the legend of mine is built
}
int main() {
//FileOpen();
//FileSave();
int n, m;
io >> n >> m;
R(i,1,n){
io >> val[i];
}
R(i,2,n){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
}
DFS(1, 0); // 1, our king
while(m--){
int x, newVal;
io >> x >> newVal;
Access(x); // you are my family now, every one should know you
Splay(x); // be my king
t[x].f[1] += newVal - val[x]; // the new up, the old down
val[x] = newVal; // and so the new be the old
Pushup(x); // you should use out your power, for out family
Splay(1); // but when your power is out, when the old king returns, you are just a simple man
printf("%d\n", t[1].x.Max()); // as only him is the man who can tell the maximum value of us
}
return 0;
}

LuoguP4719 【模板】动态 DP(动态DP,LCT)的更多相关文章
- luogu P4719 【模板】动态 DP 矩阵乘法 + LCT
方法二:LCT+矩阵乘法 上文中,我们用线段树来维护重链上的各种矩阵转移. 第二种方法是将树链剖分替换为动态树. 我们知道,矩阵乘法 $\begin{bmatrix} F_{u,0} & F_ ...
- 洛谷P4719 【模板】"动态 DP"&动态树分治
[模板]"动态 DP"&动态树分治 第一道动态\(DP\)的题,只会用树剖来做,全局平衡二叉树什么的就以后再学吧 所谓动态\(DP\),就是在原本的\(DP\)求解的问题上 ...
- [NOI2007]货币兑换Cash(DP+动态凸包)
第一次打动态凸包维护dp,感觉学到了超级多的东西. 首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k 不过就是重载运算符和一些细节方面有些恶心,90行解决 后 ...
- 数位dp模板 [dp][数位dp]
现在才想到要学数位dp,我是不是很弱 答案是肯定的 以一道自己瞎掰的题为模板 //题: //输入数字n //从0枚举到n,计算这n+1个数中含有两位数a的数的个数 //如12930含有两位数93 #i ...
- Vue 组件&组件之间的通信 之 template模板引用与动态组件的使用
template模板引用 在component的template中书写大量的HTML元素很麻烦. Vue提供了<template>标签,可以在里边书写HTML,然后通过ID指定到组建内的t ...
- 【模板整合计划】DP动态规划
[模板整合计划]DP动态规划 一:[背包] 1.[01背包] 采药 \([P1048]\) #include<algorithm> #include<cstdio> int T ...
- [模板] dp套dp && bzoj5336: [TJOI2018]party
Description Problem 5336. -- [TJOI2018]party Solution 神奇的dp套dp... 考虑lcs的转移方程: \[ lcs[i][j]=\begin{ca ...
- POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)
题意:有以个 有 N 个节点的树形地图,问在这些顶点上最少建多少个电话杆,可以使得所有顶点被覆盖到,一个节点如果建立了电话杆,那么和它直接相连的顶点也会被覆盖到. 分析:用最少的点覆盖所有的点,即为求 ...
- 算法竞赛模板 动态规划之背包DP
① 01背包 有n件物品和一个容量为v的背包.第i件物品的价值是c[i],体积是w[i].求解将哪些物品装入背包可使价值总和最大. 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
随机推荐
- 一起看 I/O | Flutter 3 更新详解
作者 / Kevin Jamaul Chisholm, Technical Program Manager for Dart and Flutter at Google 又到了 Flutter 稳定版 ...
- Java命令行传递参数
目录 命令行传参 代码运行 视频 命令行传参 有时候你希望运行一个程序的时候再传递给它消息. 这要靠传递命令行参数给main()函数实现 package com.broky.base; public ...
- 《HALCON数字图像处理》第四章笔记
目录 第四章 HALCON数据结构 HALCON Image图像 图像通道 HALCON Region区域 Region的初步介绍 Region的点与线 Region的行程 Region的区域特征 H ...
- 支付宝开放平台--网页&移动应用(一)
前提是先在支付宝上签约自己需要的支付宝功能,然后支付宝开放平台才能设置你需要的功能 一:支付宝开放平台登录 登录进入支付宝开放平台 二:根据自己的需求创建应用(我是用的网页&移动应用) 三:点 ...
- camunda BPM支持的开发和运行环境
以Camunda7.13版本为例,介绍Camunda支持的开发运行环境. 一.支持的Java开发环境 • Java版本: 8 / 9 / 10 / 11 / 12 / 13 / 14• Springb ...
- 入坑KeePass(三)安全设置完后后留存
1.文件> 数据库设置 > 安全 迭代次数改成500000 2.工具 > 选项 2.1.安全 2.2.策略 2.3.集成 2.4高级
- flink-执行模式
flink的执行模式 flink既能处理离线数据,也能处理实时数据,在1.12.0版本以前,批数据返回的数据集合是dataSet,对应一套dataSet的api,从1.12.0版本以后,flink实现 ...
- 想写个小说,关于C#的,名字就叫《原Csharp》吧 (第一回 买书未成炁自生 惶惶回屋遇老翁)
以前也有写过一些小说,但是总是写写停停的,因为忙于项目和其他事情,总是耽搁很久(真的是很久)才会继续动两笔,所以我想先在这里以随笔的方式写个关于C#异世界的小故事吧,更新随缘,也稍微能让自己轻松些. ...
- ASP.NET MVC的核心-Controller(控制器)
"每一个请求都必须通过Controller处理,然而其中有些请求是不需要模型和视图的" MVC框架规定带Controller后缀的类称为所谓的"控制器",在xx ...
- Halcon 条形码识别
read_image (Image, 'C:/Users/HJ/Desktop/test_image/image.png') create_bar_code_model([], [], BarCode ...