【NOIP 2016 提高组】天天爱跑步

传送门


problem

在这里插入图片描述
数据范围: n , m ≤ 3 × 1 0 5 n,m\le 3\times 10^5 n,m3×105 1 ≤ S i , T i ≤ n 1≤S_i,T_i≤n 1Si,Tin 0 ≤ W j ≤ n 0≤W_j≤n 0Wjn


solution

每个人的起点为 s s s,终点为 t t t,设 l c a ( s , t ) = g lca(s,t)=g lca(s,t)=g。我们把路径拆成两段,即 s → g s\rightarrow g sg g → t g\rightarrow t gt 的路径。

1 1 1、在 s → g s\rightarrow g sg 路径上的观测点 u u u

如果 u u u 能观测到这个点,那么要满足 w u = d e p s − d e p u w_u=dep_s-dep_u wu=depsdepu,移项得 w u + d e p u = d e p s w_u+dep_u=dep_s wu+depu=deps

对于一个观测点 u u u,其 w u + d e p u w_u+dep_u wu+depu 是确定的,换句话说,我们需要统计起点 s i s_i si u u u 的子树中,且满足 d e p s i = w u + d e p u dep_{s_i}=w_u+dep_u depsi=wu+depu 的链的数量。

那么对于每个观测点 u u u,用 G G G 来维护 u u u 子树内起点的 d e p s i dep_{s_i} depsi 的个数,那么 u u u 点的答案就是 G [ w u + d e p u ] G[w_u+dep_u] G[wu+depu]

但是对每个观测点都单独开个桶显然是不现实的,我们可以开全局桶。那么就要多一些细节:

  • 用树上差分,在 s s s 点把 d e p s dep_s deps 加进桶,在 g / f a g g/fa_g g/fag 点把 d e p s dep_s deps 删掉。因为要满足大前提: u u u s → g s\rightarrow g sg 的路径上。
  • u u u 有多颗子树,要注意不要把其它子树的贡献算到当前子树来(具体看代码)。
2 2 2、在 g → t g\rightarrow t gt 路径上的观测点 u u u

这部分是一样的道理。

如果 u u u 能观测到这个点,要满足 w u = ( d e p s − d e p g ) + ( d e p u − d e p g ) w_u=(dep_s-dep_g)+(dep_u-dep_g) wu=(depsdepg)+(depudepg),移项得 w u − d e p u = d e p s − 2 d e p g w_u-dep_u=dep_s-2dep_g wudepu=deps2depg

那这个和上面所说的是同一个东西,一样做就可以了。

时间复杂度 O ( n log ⁡ n ) O(n\log n) O(nlogn),主要花在求 l c a lca lca 上,如果用欧拉序 + + + ST 表可以优化到 O ( n ) O(n) O(n)


code

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define N 300005
using namespace std;
int n,m,t,Zero=N<<1;
int w[N],dep[N],fa[N][21],ans[N],G1[N<<2],G2[N<<2];
int first[N],v[N<<1],nxt[N<<1];
vector<int>add[N],dec[N],Add[N],Dec[N];
void edge(int x,int y){
	nxt[++t]=first[x],first[x]=t,v[t]=y;
}
void Dfs(int x){
	for(int i=1;i<=20;++i)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=first[x];i;i=nxt[i]){
		int to=v[i];
		if(to==fa[x][0])  continue;
		fa[to][0]=x,dep[to]=dep[x]+1;
		Dfs(to);
	}
}
int LCA(int x,int y){
	if(dep[x]<dep[y])  swap(x,y);
	for(int i=20;~i;--i)  if(dep[fa[x][i]]>=dep[y])  x=fa[x][i];
	if(x==y)  return x;
	for(int i=20;~i;--i)  if(fa[x][i]!=fa[y][i])  x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
void dfs(int x){
	int tmp1=w[x]+dep[x],tmp2=w[x]-dep[x],val1=G1[tmp1],val2=G2[Zero+tmp2];
	for(int i=first[x];i;i=nxt[i]){
		int to=v[i];
		if(to==fa[x][0])  continue;
		dfs(to);
	}
	for(int i=0;i<add[x].size();++i)  ++G1[add[x][i]];
	for(int i=0;i<dec[x].size();++i)  --G1[dec[x][i]];
	ans[x]=G1[tmp1]-val1;
	for(int i=0;i<Add[x].size();++i)  ++G2[Zero+Add[x][i]];
	for(int i=0;i<Dec[x].size();++i)  --G2[Zero+Dec[x][i]];
	ans[x]+=G2[Zero+tmp2]-val2;
}
int main(){
	int x,y;
	scanf("%d%d",&n,&m);
	for(int i=1;i<n;++i){
		scanf("%d%d",&x,&y),edge(x,y),edge(y,x);
	}
	dep[1]=1,Dfs(1);
	for(int i=1;i<=n;++i)  scanf("%d",&w[i]);
	for(int i=1;i<=m;++i){
		scanf("%d%d",&x,&y);
		int lca=LCA(x,y);
		add[x].push_back(dep[x]);
		dec[lca].push_back(dep[x]);
		Add[y].push_back(dep[x]-2*dep[lca]);
		Dec[fa[lca][0]].push_back(dep[x]-2*dep[lca]);
	}
	dfs(1);
	for(int i=1;i<=n;++i)  printf("%d ",ans[i]);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值