LuoguP4719 【模板】动态 DP(动态DP,LCT)

(n imes m)的算法谁都会吧,注意到每次修改影响的仅是一部分的信息,因此可思考优化。
将每个节点对应一个矩阵(egin{bmatrix} g[v][0] & g[v][0] \ g[v][1] & -infty end{bmatrix}) ,从而 (egin{bmatrix} g[v][0] & g[v][0] \ g[v][1] & -infty end{bmatrix} imes egin{bmatrix} f[son[u]][0] \ f[son[u]][1] end{bmatrix} = egin{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 << "
"
#define D_e_Line printf("
----------------
")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt","w", stdout)
#define Pause() system("pause")
#define TIME() fprintf(stderr, "TIME : %.3lfms
", 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
", t[1].x.Max()); // as only him is the man who can tell the maximum value of us
	}
	
	return 0;
}

原文地址:https://www.cnblogs.com/bingoyes/p/11755278.html