E. Vasya and a Tree--Educational Codeforces Round 54 (Rated for Div. 2) (DFS&&差分思想)

3 篇文章 0 订阅
2 篇文章 0 订阅

                                                       E. Vasya and a Tree

                                                                          time limit per test   2 seconds

                                                                   memory limit per test  256 megabytes

                                                                              input   standard input

                                                                             output  standard output

Vasya has a tree consisting of nn vertices with root in vertex 1. At first all vertices has 0 written on it.

Let d(i,j) be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y such that next two conditions are met:

  • x is the ancestor of y (each vertex is the ancestor of itself);
  • d(x,y)≤k.

Vasya needs you to process m queries. The i-th query is a triple vi, di and xi. For each query Vasya adds value xi to each vertex from di-subtree of vi.

Report to Vasya all values, written on vertices of the tree after processing all queries.

Input

The first line contains single integer n (1≤n≤3⋅105) — number of vertices in the tree.

Each of next n−1 lines contains two integers x and y (1≤x,y≤n) — edge between vertices x and y. It is guarantied that given graph is a tree.

Next line contains single integer mm (1≤m≤3⋅105) — number of queries.

Each of next mm lines contains three integers vi, di, xi (1≤vi≤n, 0≤di≤109, 1≤xi≤109) — description of the i-th query.

Output

Print nn integers. The i-th integers is the value, written in the i-th vertex after processing all queries.

 

Examples

input

5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100

output

1 11 1 100 0 

 

input

5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7

output

10 24 14 11 11 

Note

In the first exapmle initial values in vertices are 0,0,0,0,0. After the first query values will be equal to 1,1,1,0,0. After the second query values will be equal to 1,11,1,0,0. After the third query values will be equal to 1,11,1,100,0.

 

题意:给定一棵n个节点的树,给定m个操作,每个操作将v的所有深度小于等于d的子节点加上x,问左后树上每个节点的权值;

 

解法:差分思想:大概是这样的,给定n个数,q个询问,每次给你一个区间 [le,ri]以及值value,然后每次让你把区间上每个点加上值value,问q次操作后数组内每个点的值。

如果用线段树或者树状数组,复杂度为O(q*log n) ,若运用差分思想,即对于每个查询将memor[le]+value,memor[ri+1]-value;然后o(n)遍历memor,使memor[i]+=memor[i-1],然后再加上初始数的大小就行了,道理应该不难懂吧,操作相当于把从le到ri上的数都加上value(i),ri+1的位置是-value(i) 所以后面的就抵消了,相当于没加,复杂度O(q+n);

 

对于这题,开一个sum数组(相当于上面的memor数组),记录的是深度depth的临时值,跑DFS,然后考虑所有以当前节点作为父节点的操作,sum[depth]+=value,sum[depth+di+1]-=value;用变量total记录向下传递的值,然后当前节点最终的值就是total+sum[depth]喽。

 

但是注意递归回来时(rollback)需要将对sum数组原来在该节点的操作撤回,防止对后来的相同depth的其他子树询问产生影响。

 

附code:

#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
typedef long long ll;

template <class T> inline void read(T &x) {
	x = 0;int f = 1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x=x*f;
}
template<typename E> E gcd(E a,E b){return b==0?a:gcd(b,a%b);}
template<typename E> E ex_gcd(E a,E b,E &x,E&y) {if(b==0){x=1;y=0;}else {ex_gcd(b,a%b,y,x);y-=(a/b)*x;}}
template<typename E> E quick_pow(E a,E b,E c){E ans=1;while(b){if(b&1) ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}
template<typename E> E inv1(E a,E b){return quick_pow(a,b-2,b);}
template<typename E> E inv2(E a,E b){E x,y;ex_gcd(a,b,x,y);return (x%b+b)%b;}

/*================Header Template==============*/

const double eps=1.0e-5;
const int maxn=200000+10;
const ll mod=10007;

vector<int> vec[300005];
vector<pair<int,int> > query[300005];
int n,m,a,b,dep;
ll sum[300005],ans[300005];

void dfs(int cur,int last,int depth,ll total)
{
	for(int i=0;i<query[cur].size();i++){
		sum[depth]+=query[cur][i].second;
		if(depth+query[cur][i].first+1<=300000) sum[depth+query[cur][i].first+1]-=query[cur][i].second;
	}
	total+=sum[depth];
	ans[cur]=total;
	for(int i=0;i<vec[cur].size();i++){
		if(vec[cur][i]!=last){
			dfs(vec[cur][i],cur,depth+1,total);
		}
	}
	for(int i=0;i<query[cur].size();i++){
		sum[depth]-=query[cur][i].second;
		if(depth+query[cur][i].first+1<=300000) sum[depth+query[cur][i].first+1]+=query[cur][i].second;
	}
}

int main()
{
	scanf("%d",&n);
	per(i,1,n-1){
		scanf("%d%d",&a,&b);
		vec[a].push_back(b);vec[b].push_back(a);
	}
	scanf("%d",&m);
	per(i,1,m){
		scanf("%d%d%d",&a,&dep,&b);
		query[a].push_back(make_pair(dep,b));
	}
	dfs(1,1,1,0);
	per(i,1,n) printf("%lld ",ans[i]);
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值