Vasya has a tree consisting of nn vertices with root in vertex 11. At first all vertices has 00 written on it.
Let d(i,j)d(i,j) be the distance between vertices ii and jj, i.e. number of edges in the shortest path from ii to jj. Also, let's denote kk-subtree of vertex xx — set of vertices yy such that next two conditions are met:
- xx is the ancestor of yy (each vertex is the ancestor of itself);
- d(x,y)≤kd(x,y)≤k.
Vasya needs you to process mm queries. The ii-th query is a triple vivi, didi and xixi. For each query Vasya adds value xixi to each vertex from didi-subtree of vivi.
Report to Vasya all values, written on vertices of the tree after processing all queries.
Input
The first line contains single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — number of vertices in the tree.
Each of next n−1n−1 lines contains two integers xx and yy (1≤x,y≤n1≤x,y≤n) — edge between vertices xx and yy. It is guarantied that given graph is a tree.
Next line contains single integer mm (1≤m≤3⋅1051≤m≤3⋅105) — number of queries.
Each of next mm lines contains three integers vivi, didi, xixi (1≤vi≤n1≤vi≤n, 0≤di≤1090≤di≤109, 1≤xi≤1091≤xi≤109) — description of the ii-th query.
Output
Print nn integers. The ii-th integers is the value, written in the ii-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,00,0,0,0,0. After the first query values will be equal to 1,1,1,0,01,1,1,0,0. After the second query values will be equal to 1,11,1,0,01,11,1,0,0. After the third query values will be equal to 1,11,1,100,01,11,1,100,0.
传送门
题意:
首先给出一个数n,代表的是下面会有n-1个操作,对于每个操作,输入两个数a,b,代表的是两个数字相连,接下来给出一个数字m,下面会有m组输入,每组输入分为u,v,w,代表的是以u为根节点,对u的子节点都进行加上w的操作(深度为v)。
题解:
#include<iostream>
#include<vector>
#include<string.h>
#include<cstdio>
#define P pair<long long,long long>
using namespace std;
const int maxn=3e5+10;
long long n,m;
long long xx[maxn];
long long num_puts[maxn];
vector<long long>mapp[maxn];
vector<P>ma[maxn];
long long start[maxn], endss[maxn];
inline long long lowbit(long long x)
{
return x&(-x);
}
void update(long long x,long long num)
{
while(x<=maxn)
{
xx[x]+=num;
x+=lowbit(x);
}
}
long long query(long long x)
{
long long num_puts=0;
while(x>0)
{
num_puts+=xx[x];
x-=lowbit(x);
}
return num_puts;
}
void dfs(long long u,long long fa,long long da)
{
for(long long i=0;i<ma[u].size();++i)
{
long long cengshu=ma[u][i].first;
long long daxiao=ma[u][i].second;
update(da,daxiao);
update(da+cengshu+1,-daxiao);
}
num_puts[u]=query(da);
for(long long i=0;i<mapp[u].size();++i)
{
long long v=mapp[u][i];
if(fa==v) continue;
dfs(v,u,da+1);
}
for(long long i=0;i<ma[u].size();++i)
{
long long cengshu=ma[u][i].first;
long long daxiao=ma[u][i].second;
update(da,-daxiao);
update(da+cengshu+1,daxiao);
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//加速输入输出流
cin>>n;
for(long long i=1;i<n;++i)
{
long long x,y;
cin>>x>>y;
mapp[x].push_back(y);
mapp[y].push_back(x);
}
cin>>m;
for(long long i=0;i<m;++i)
{
long long v,d,x;
cin>>v>>d>>x;
ma[v].push_back({d,x});
}
dfs(1,0,1);
for(long long i=1;i<n;++i)
{
printf("%lld ",num_puts[i]);
}
printf("%lld\n",num_puts[n]);
return 0;
}