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/ ...
随机推荐
- Kube-OVN v1.10.0:新增Windows节点支持,用户自定义子网ACL等10+硬核功能
在Kube-OVN社区小伙伴的共同努力下,Kube-OVN v1.10.0于五月份正式发布.Kube-OVN v1.10.0版本中,我们一如既往地对Kube-OVN 的功能.性能.稳定性和易用性进行了 ...
- PHP odbc查询SQL SERVER数据库带有中文时无返回数据
近日遇到一个小麻烦当数据库中有中文字符数据 结果odbc_fetch_array后 我用json_encode怎么也得不到数据页面一片空白 我脑子也一片空白后来才知道sqlserver 没有 UTF- ...
- 配置中心的设计-nacos vs apollo
简介 前面我们分析了携程的 apollo(见 详解apollo的设计与使用),现在再来看看阿里的 nacos. 和 apollo 一样,nacos 也是一款配置中心,同样可以实现配置的集中管理.分环境 ...
- 【python】tile函数简单介绍
转:https://blog.csdn.net/april_newnew/article/details/44176059格式:tile(A,reps)* A:array_like* 输入的array ...
- CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
前言 本文深入探讨了如何设计神经网络.如何使得训练神经网络具有更加优异的效果,以及思考网络设计的物理意义. 欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结.最新技术跟踪.经典论文解读.CV招聘 ...
- php原生PHPExcel插件导表(附表格合并,加粗居中及加边框换行操作)
PHPExcel是用来操作Office Excel文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言.可以使用它来读取.写入不同格式的电子表格,如 Excel(BIFF) .xls, E ...
- UiPath存在元素Element Exists的介绍和使用
一.Element Exists的介绍 使您能够验证UI元素是否存在,即使它不可见,输出的是一个布尔值 二.Element Exists在UiPath中的使用 1. 打开设计器,在设计库中新建一个Se ...
- 浪姐打分看不够?用几行Python代码模拟评委打分
大家好鸭~我是小熊猫比赛大家都看过吧,每次是不是都对比赛成绩充满期待.特别是浪姐的打分看的简直欲罢不能- 今天就用Python来模拟评委打分,这个案例很短也很简单,很适合新手跟小白练习. 在某次十佳歌 ...
- 使用Navicat创建存储过程(顺带插入百万级数据量)
一.建表 DROP TABLE IF EXISTS `test_user`; CREATE TABLE `test_user` ( `id` bigint(20) PRIMARY key not nu ...
- Ansible Playbook概览
Ansible playbook 执行需要三步路执行: 1.编写playbook 2.定义主机清单文件 3.设置运行环境,写入配置文件 1.编写playbook Playbook使用YAML语法格式进 ...