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/ ...
随机推荐
- Python3 filter()函数和map()函数
filter(function or None,iterable) 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换. 该接收两个参数,第 ...
- 7. Docker CI、CD
在上图这个新建的docker-compose.yml文件中把刚才的代码粘贴进去. 可把上述文件保存后,然后到/etc/ssh/sshd_config文件中更改下对应的端口号即可. 然后重新启动sshd ...
- 使用PowerShell安装MySQL
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年7月10日. 2022年4月16日:更新MySQL下载链接. 一.说明与准备工作 根据MySQL官网提供的安装M ...
- 2分钟实现一个Vue实时直播系统
前言 我们在不敲代码的时候可能会去看游戏直播,那么是前台怎么实现的呢?下面我们来讲一下.第一步,购买云直播服务 首先,你必须去阿里云或者腾讯云注册一个直播服务.也花不了几个钱,练手的话,几十块钱就够了 ...
- 配置nginx多域名虚拟主机
1.先做域名映射,由于我们使用的是阿里云域名. 登录阿里云控制台-->域名与网站(万网)-->域名-->选择一个域名-->域名解析-->添加记录 配置静态资源下载转发: ...
- C#中List实体类转换为object 并把参数返回到前端
用ConvertAll方法转换: List<Object> m= list.ConvertAll(s=> (object)s); 返回的结果:
- 【微服务专题之】.Net6下集成消息队列上-RabbitMQ
微信公众号:趣编程ACE关注可了解更多的.NET日常实战开发技巧,如需源码 请公众号后台留言 源码;[如果觉得本公众号对您有帮助,欢迎关注] .Net中RabbitMQ的使用 [微服务专题之].N ...
- 【主流技术】Redis 在 Spring 框架中的实践
前言 在Java Spring 项目中,数据与远程数据库的频繁交互对服务器的内存消耗比较大,而 Redis 的特性可以有效解决这样的问题. Redis 的几个特性: Redis 以内存作为数据存储介质 ...
- 软件测试—Day2
day2 Q:面试过程中,性能测试你测试什么?关注的点是什么? A:程序的响应时间,系统的吞吐量,以及并发用户数,和tps,qps,以及DB的IOPS,和服务器的系统资源(CPU和内存).通过一定的工 ...
- Python List 中的append 和 extend 的区别
方法的参数不同 append 方法是向原list的末尾添加一个对象(任意对象:如元组,字典,列表等),且只占据一个原list的索引位,添加后无返回值,直接在原列表中添加. list.append(ob ...