跳到正文

题解

题解:P4751 【模板】动态 DP(加强版)

ren_lei_zhi_guang约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年9月28日
收录
2026年9月30日
更新
2026年9月30日
篇幅
约 7 分钟
获赞
1
题目
P4751

版本

  1. v1当前

注意:文中的卡常技巧打勾的为作者认为优化显著的,杂项由于众所周知所以不进行评价,文章提到的部分函数、数组及变量与代码中的完全对应。

树剖常数小!跑不满!

shadowice1984 为了向你证明他能卡树剖并且会卡树剖从而出了这道毒瘤题。

既然如此,那就让树剖的常数更小。

对代码从上往下进行卡常。

  • 对于矩阵:

    • 矩阵是 2×22\times 2 的,将乘法考虑循环展开。
    • 将矩阵中二维数组改为一维数组。
  • 对于线段树:

    • 将递归线段树改为 zkw 线段树。
  • 对于树链剖分:

    • 对每个重链单独开一颗线段树,这样子在 update 函数中的两次 query1^{\color{red}{1}} 可以省掉,常数至少缩小为原来的三分之一。
  • 杂项:

    1. 快读快写,其实并不会有很大的提升,只关闭同步流也可以,但是如果对于读入不进行任何优化,似乎至多获得九十分。
    2. 非递归函数强制内联。
    3. if 等改为三目运算符。
    4. 将 f 等二维数组压为一维数组。

为阅读方便,使用关闭同步流而非快读快写。

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000005],st[1000005],base[1000005],f[2000005],dp[2000005],fa[1000005],siz[1000005],son[1000005],top[1000005],dfn[1000005],rk[1000005],bottom[1000005],cnt,lastans;
vector<int>g[1000005];
struct Matrix{
	int a[4];
	inline Matrix operator*(const Matrix&other){
		return {max(a[0]+other.a[0],a[1]+other.a[2]),max(a[0]+other.a[1],a[1]+other.a[3]),max(a[2]+other.a[0],a[3]+other.a[2]),max(a[2]+other.a[1],a[3]+other.a[3])};
	}
}tree[4000005];
static inline void build(){
	int sum=0;
	for(int i=1;i<=n;i++){
		if(top[i]==i){
			st[i]=sum;
			base[i]=(1<<(((1<<__lg(dfn[bottom[i]]-dfn[i]+1))==dfn[bottom[i]]-dfn[i]+1)?__lg(dfn[bottom[i]]-dfn[i]+1):__lg(dfn[bottom[i]]-dfn[i]+1)+1));
			sum+=(base[i]<<1);
			for(int j=1;j<=base[i];j++)j<=dfn[bottom[i]]-dfn[i]+1?(tree[st[i]+base[i]+j-1].a[0]=tree[st[i]+base[i]+j-1].a[1]=f[rk[dfn[i]+j-1]<<1],tree[st[i]+base[i]+j-1].a[2]=f[rk[dfn[i]+j-1]<<1|1],tree[st[i]+base[i]+j-1].a[3]=-1e9):(tree[st[i]+base[i]+j-1].a[0]=tree[st[i]+base[i]+j-1].a[3]=0,tree[st[i]+base[i]+j-1].a[1]=tree[st[i]+base[i]+j-1].a[2]=-1e9);
			for(int j=base[i]-1;j>=1;j--)tree[st[i]+j]=tree[st[i]+(j<<1)]*tree[st[i]+(j<<1|1)];
		}
	}
}
static inline void change(const int u){
	tree[st[top[u]]+base[top[u]]+dfn[u]-dfn[top[u]]].a[0]=tree[st[top[u]]+base[top[u]]+dfn[u]-dfn[top[u]]].a[1]=f[u<<1];
	tree[st[top[u]]+base[top[u]]+dfn[u]-dfn[top[u]]].a[2]=f[u<<1|1];
	for(int i=(base[top[u]]+dfn[u]-dfn[top[u]])>>1;i;i>>=1)tree[st[top[u]]+i]=tree[st[top[u]]+(i<<1)]*tree[st[top[u]]+(i<<1|1)];
}
static void dfs(const int u,const int f){
	fa[u]=f;
	siz[u]=1;
	dp[u<<1|1]=a[u];
	int s=0;
	for(auto v:g[u])(v^f)&&(dfs(v,u),dp[u<<1]+=max(dp[v<<1|1],dp[v<<1]),dp[u<<1|1]+=dp[v<<1],siz[u]+=siz[v],(s<siz[v])&&(s=siz[v],son[u]=v));
}
static void dfs2(const int u,const int t){
	dfn[u]=++cnt;
	rk[cnt]=u;
	top[u]=t;
	f[u<<1|1]=a[u];
	if(siz[u]==1){
		bottom[u]=u;
		return;
	}
	dfs2(son[u],t);
	bottom[u]=bottom[son[u]];
	for(auto v:g[u])((v^fa[u])&&(v^son[u]))&&(dfs2(v,v),f[u<<1]+=max(dp[v<<1|1],dp[v<<1]),f[u<<1|1]+=dp[v<<1]);
}
static inline void update(int u,const int k){
	f[u<<1|1]+=k-a[u];
	a[u]=k;
	while(u){
		Matrix old=tree[st[top[u]]+1];
		change(u);
		Matrix New=tree[st[top[u]]+1];
		u=fa[top[u]];
		f[u<<1]+=max(New.a[0],New.a[2])-max(old.a[0],old.a[2]);
		f[u<<1|1]+=New.a[0]-old.a[0];
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int u,v,i=1;i<n;i++){
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0);
	dfs2(1,1);
	build();
	while(m--){
		int x,y;
		cin>>x>>y;
		x^=lastans;
		update(x,y);
		cout<<(lastans=max(tree[1].a[0],tree[1].a[2]))<<'\n';
	}
	return 0;
}

代码很短,仅 2.65KB⁡2.65\operatorname{KB}。

使用 C++14(GCC 9)\texttt{C++14(GCC 9)} 进行评测,用时 4.68s⁡4.68\operatorname{s},若使用某些快读快写,用时 3.93s⁡3.93\operatorname{s}。


1.\color{red}{1}. query 函数指不对每个重链单独开一颗线段树时对于整颗线段树的查询函数,具体可见 此提交记录。

原文
存档评论1 条抓取于