Muh-Yih.github.io

[ABC187E] Through Path 题解

当前位置:首页 > 信竞相关 > [ABC187E] Through Path 题解

发布时间:2023年8月2日 11:06

题意

给定一棵 N 节点的树,边形如 (ui, vi)。维护以下操作 Q 次:

输出最终每个点的点权。初始点权为 0。

分析

刚开始看到这道题的时候,我想的是树链剖分——然而很快就因码力不足将其否决,于是开始考虑树上差分。

让我们来看看样例:

7
2 1
2 3
4 2
4 5
6 1
3 7
7
2 2 1
1 3 2
2 2 4
1 6 8
1 3 16
2 4 32
2 1 64

规定 1 号节点为根节点,把这棵树画出来,大概是这样:

考虑第 i 次操作为 opi,ei,ki,无论 opi=1 还是 opi=2,都会选中一条边,其中一个顶点作为出发点。

记出发点为 st,另一个顶点为 at,节点 x 深度为 depthx(depth1=1)。

若 depthst < depthat,则修改的节点为树上除 at 的子树(包括 at)外的所有节点;若 depthst > depthat,则修改的节点为 st 的子树(包括 st)。

比如,对于操作 2 2 1 ,有 st=v2=3,at=u2=2;depthst=depth3=3,depthat=depth2=2。此时 depthst > depthat 。修改节点如下:

对于操作 1 6 8 ,有 st=u6=3,at=v6=7;depthst=depth3=3,depthat=depth7=4。此时 depthst < depthat 。修改节点如下:

于是我们可以建一个差分数组 d[N] :对于操作 opi,ei,ki,若 depthst < depthat,则 d[1]+=ki,d[at]-=ki;若 depthst > depthat,则 d[st]+=ki 。

最后从根往下统计答案即可。

总体思路:

我的代码中使用 DFS 处理深度,时间复杂度 O(N);每次询问修改差分数组,时间复杂度 O(Q);使用 DFS 统计答案,时间复杂度 O(N);总时间复杂度 O(N+Q)。

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=410000;
int u[N],v[N],depth[N],chafen[N],ans[N];
int n,q;
vector <int> mp[N];
void depdfs (int x,int fa,int dep)
{
	depth[x]=dep;
	for (int i=0;i<mp[x].size();i++)
	{
		int pt=mp[x][i];
		if (pt!=fa) depdfs (pt,x,dep+1);
	}
}
void chafendfs (int x,int fa,int v)
{
	v+=chafen[x];
	ans[x]=v;
	for (int i=0;i<mp[x].size();i++)
	{
		int pt=mp[x][i];
		if (pt!=fa) chafendfs (pt,x,v);
	}
}
signed main()
{
	scanf ("%lld",&n);
	for (int i=1;i<n;i++)
	{
		scanf ("%lld%lld",u+i,v+i);
		mp[u[i]].push_back(v[i]);mp[v[i]].push_back(u[i]);
	}
	depdfs (1,0,1);
    scanf ("%lld",&q);
	for (int i=1;i<=q;i++)
	{
		int op,e,val;
		scanf ("%lld%lld%lld",&op,&e,&val);
		int ch,nch,chd,nchd;
		if (op==1)
		{
			ch=u[e],nch=v[e];
			chd=depth[ch],nchd=depth[nch];
			if (chd<nchd) chafen[1]+=val,chafen[nch]-=val;
			else chafen[ch]+=val;
		}
		if (op==2)
		{
			ch=v[e],nch=u[e];
			chd=depth[ch],nchd=depth[nch];
			if (chd<nchd) chafen[1]+=val,chafen[nch]-=val;
			else chafen[ch]+=val;
		}
	}
	chafendfs (1,0,0);
	for (int i=1;i<=n;i++) printf ("%lld\n",ans[i]);
	return 0;
}

外部链接
我的洛谷博客
我的 LOFTER 博客