跳到正文

题解

题解:P4719 【模板】动态 DP

cheng2010约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年7月11日
收录
2026年4月1日
更新
2026年10月27日
篇幅
约 12 分钟
题目
P4719

版本

  1. v2当前
  2. v1

翻了一下题解区,发现几乎没有 LCT 的题解,有的也十分简略,那就让我这个蒟蒻讲一个稍微详细一点的吧。

先速通一下式子,定义 fx,1/0f_{x,1/0} 为是否选 xx 的最大值,gx,0g_{x,0} 为每个虚儿子中可选可不选的和最大值,gx,1g_{x,1} 为不选虚儿子,加上选自己的最大值,则有:

[fv,0fv,1][gx,0gx,0gx,1−∞]=[fx,0fx,1]\left[ \begin{array}{cc} f_{v,0} & f_{v,1} \end{array} \right] \left[ \begin{array}{cc} g_{x,0} & g_{x,0} \\ g_{x,1} & -\infty \\ \end{array} \right]=\left[ \begin{array}{cc} f_{x,0} & f_{x,1} \end{array} \right]

其中 vv 为 xx 的实儿子,因为叶子结点为 [00]\left[ \begin{array}{cc} 0 & 0 \end{array} \right] 于是答案就是:

[gx,0gx,0gx,1−∞]l\left[ \begin{array}{cc} g_{x,0} & g_{x,0} \\ g_{x,1} & -\infty \\ \end{array} \right]^l

ll 为链长。

树剖 log⁡2\log ^2 不优秀,全局平衡二叉树不会,考虑 LCT,但这里的 LCT 不太一样。

首先,这里的 LCT 是“静态的”,也就是说没有 link、cut、make_root 之类的操作,因为有的话父子关系变来变去的很难维护,所以,初始时设每个儿子都是虚的,跑一遍 dp,再来解决其他问题。

以下约定:treex.valtree_x.val 为 xx 的矩阵,treex.multree_x.mul 为此时以 xx 为根的子树中还原的链(也就是先序遍历,LCT 的性质)的矩阵的积。

  • pushup

按序合并即可。

C++
inline void pushup(int p)
{
    tree[p].mul=
    (ls(p)?tree[ls(p)].mul:one)//注意判断儿子是否为空,one 为单位矩阵,对角线为负无穷,其余为 0。
    *tree[p].val*
    (rs(p)?tree[rs(p)].mul:one);
}
  • rotate

无影响,正常打。

C++
inline void rotate(int x)
{
    int y=fa(x),z=fa(y);
    bool pd=get(x);
    if(not_root(y)) tree[z].son[get(y)]=x;
    tree[y].son[pd]=tree[x].son[pd^1];
    if(tree[x].son[pd^1]) fa(tree[x].son[pd^1])=y;
    fa(y)=x;
    tree[x].son[pd^1]=y;
    fa(x)=z;
    pushup(y);
}
  • splay

我们注意到,由于这棵 LCT 是“静态的”,所以没有 make_root 操作,也就是说,没有懒标记!我们不用下放标记了!可节约很多常数。

C++
inline void splay(int x)
{
  while(not_root(x))
  {
    int y=fa(x);
    if(not_root(y)) rotate(get(x)==get(y)?y:x);
    rotate(x);
  }
  pushup(x);
}
  • access

在这里,我们要更新实、虚儿子,要重点注意。

首先,原本的右儿子虚了,加入的点(记作 yy)实了,那么我们就要加上右儿子的 dp 值,减去 yy 的 dp 值,那么该怎么办?其实很简单,因为右儿子一定是它所在子树的根,yy 也是它所在联通块的根,所以它们的 dp 值就在它们身上,直接做即可。

C++
inline void access(int x)
{
    for(int y=0;x;x=fa(y=x))
    {
        splay(x);
        matix res=tree[rs(x)].mul;
        int r1=res.a[0][0];
        int r2=res.a[1][0];
        int y0=tree[y].mul.a[0][0];
        int y1=tree[y].mul.a[1][0];
        //以上代表着 dp 值,不清楚的可以回看一下前面。
        int _0=max(r1,r2)-max(y0,y1);
        //_0 用来更新 g0
        tree[x].val.a[0][0]+=_0;
        tree[x].val.a[0][1]+=_0;
        tree[x].val.a[1][0]+=r1-y0;
        rs(x)=y;
        pushup(x);//记得上传
    }
}
  • update

这个也要注意,更新 xx 的权值时不可以这样,否则就炸了:

C++
splay(x);
update x
pushup(x)

为什么?我们来看,直接把 xx 转到这棵 splay 的根,那它还有祖先啊,它更新了但它祖先怎么办?所以我们加一个 access 让它没有祖先,无所畏惧。

C++
inline void update(int x,int y)
{
    access(x);
    splay(x);
    tree[x].val.a[1][0]+=y-a[x];//更新 g1 的值
    a[x]=y;
    pushup(x);
}
  • query

我们 update 后,xx 成了它这棵 splay 的根,注意到这棵 splay 实质上是一个从原树的根到某个叶子的链,所以 xx 直接就是答案了。

现在,所有操作都讲完了若有问题,欢迎指正,现给出完整代码:(注:这个代码是加强版的代码,稍微卡了一下常)

C++
#include<bits/stdc++.h>
#define ls(x) tree[x].son[0]
#define rs(x) tree[x].son[1]
#define fa(x) tree[x].fa
using namespace std;
const int N=1e6+7;
struct FastIO
{
	inline char gc()
	{
	    static char buf[1000001],*p1=buf,*p2=buf;
	    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000001,stdin),p1==p2)?EOF:*p1++;
	}
	template<typename T> inline void in(T &x)
	{
		x=0;
		int f=1;
		char c=gc();
		while(c<'0'||c>'9')
		{
			if(c=='-') f=-f;
			c=gc();
		}
		while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
		x*=f;
	}
	template<typename T> inline void out(T x)
	{

		if(x<0){x=-x;putchar('-');}
		if(x>9)out(x/10);
		putchar(x%10+'0');
	}
	template<typename T,typename...Args> inline void in(T&x,Args&...Arg){in(x),in(Arg...);}
	template<typename T,typename...Args> inline void out(T x,Args...Arg){out(x),putchar('\n'),out(Arg...);}
}io;
int a[N];
struct matix
{
	int a[2][2];
	matix(){memset(a,0,sizeof(a));}
};
inline matix operator * (matix A,matix B)
{
	matix res;
	res.a[0][0]=max(A.a[0][0]+B.a[0][0],A.a[0][1]+B.a[1][0]);
	res.a[1][0]=max(A.a[1][0]+B.a[0][0],A.a[1][1]+B.a[1][0]);
	res.a[0][1]=max(A.a[0][0]+B.a[0][1],A.a[0][1]+B.a[1][1]);
	res.a[1][1]=max(A.a[1][0]+B.a[0][1],A.a[1][1]+B.a[1][1]);
	return res;
}
matix one;
struct Tree
{
	int fa,son[2];
	matix val,mul;
}tree[N];
struct link_cut_tree
{
	inline void pushup(int p){tree[p].mul=(ls(p)?tree[ls(p)].mul:one)*tree[p].val*(rs(p)?tree[rs(p)].mul:one);}
	inline bool not_root(int p){return ls(fa(p))==p||rs(fa(p))==p;}
	inline bool get(int p){return rs(fa(p))==p;}
	inline void rotate(int x)
	{
		int y=fa(x),z=fa(y);
		bool pd=get(x);
		if(not_root(y)) tree[z].son[get(y)]=x;
		tree[y].son[pd]=tree[x].son[pd^1];
		if(tree[x].son[pd^1]) fa(tree[x].son[pd^1])=y;
		fa(y)=x;
		tree[x].son[pd^1]=y;
		fa(x)=z;
		pushup(y);
	}
	inline void splay(int x)
	{
		while(not_root(x))
		{
			int y=fa(x);
			if(not_root(y)) rotate(get(x)==get(y)?y:x);
			rotate(x);
		}
		pushup(x);
	}
	inline void access(int x)
	{
		for(int y=0;x;x=fa(y=x))
		{
			splay(x);
			matix res=tree[rs(x)].mul;
			int r1=res.a[0][0],r2=res.a[1][0],y0=tree[y].mul.a[0][0],y1=tree[y].mul.a[1][0],_0=max(r1,r2)-max(y0,y1);
			tree[x].val.a[0][0]+=_0;
			tree[x].val.a[0][1]+=_0;
			tree[x].val.a[1][0]+=r1-y0;
			rs(x)=y;
			pushup(x);
		}
	}
	inline void update(int x,int y)
	{
		access(x);
		splay(x);
		tree[x].val.a[1][0]+=y-a[x];
		a[x]=y;
		pushup(x);
	}
}LCT;
int n,m;
int leaf,in[N];
int lstans;
vector<int> edge[N];
inline void dfs(int x,int fa)
{
	int w1=a[x],w2=0;
	for(int v:edge[x])
	{
		if(v==fa) continue;
		dfs(v,x);
		tree[v].fa=x;
		w1+=tree[v].val.a[0][0];
		w2+=max(tree[v].val.a[0][0],tree[v].val.a[1][0]);
	}
	tree[x].val.a[0][0]=tree[x].val.a[0][1]=w2;
	tree[x].val.a[1][0]=w1;
	tree[x].val.a[1][1]=-1145;
	tree[x].mul=tree[x].val;
}
int main()
{
	one.a[0][1]=one.a[1][0]=-1145;
	io.in(n,m);
	for(int i=1;i<=n;i++)
		io.in(a[i]);
	for(int i=1;i<n;i++)
	{
		int x,y;
		io.in(x,y);
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	dfs(1,0);
	while(m--)
	{
		int x,y;
		io.in(x,y);
		x^=lstans;
		LCT.update(x,y);
		io.out(lstans=max(tree[x].mul.a[0][0],tree[x].mul.a[1][0]));
		putchar('\n');
	}
}

其实可以看到,LCT 虽然常数大,但在大的数据下单 log⁡\log 的优势就很明显了,而且十分好码,LCT 万岁!

原文