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 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:
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
Copy
5 1 2 1 3 2 4 2 5 3 1 1 1 2 0 10 4 10 100
output
Copy
1 11 1 100 0
input
Copy
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
Copy
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.
点我传送
给一个以1为根的树,现在有m个操作,每个操作读入u,dep,w 表示给以u为根节点且距离u小于dep范围上的点权值加w.
询问最后操作完之后每个点的权值。
先将所有的操作记录在每个节点上,以1为起点跑DFS。每当到达一个点的时候就将该点上的所有操作加入我们需要的统计范围,这里用线段树来更新范围内需要累加的值。意思就是对于每个 u 在深度为 u 到u+dep的范围内我们需要累加上w。在DFS的回溯过程中再把这些值取消掉。复杂度应该是O( (n+m)longn )吧?
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn=3e5+5;
int n,m;
LL ans[maxn];
struct OP
{
int u,dep;
LL w;
OP(int _u,int _dep,LL _w)
{
u=_u;dep=_dep;w=_w;
}
};
vectore[maxn];
vectorop[maxn];
//-------------线段树-----------
const int N = 3e5+5;
LL lazy[N << 2]; //lazy用来记录该节点的每个数值应该加多少
struct Tree
{
int l, r;
LL sum;
int mid()
{
return (l + r) >> 1;
}
}tree[N<<2]; //tree[].l, tree[].r分别表示某个节点的左右区间,这里的区间是闭区间
void PushUp(int rt)
{
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
void PushDown(int rt,int m)
{
if (lazy[rt])
{
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1].sum += lazy[rt] * (m - (m >> 1));
tree[rt << 1 | 1].sum += lazy[rt] * (m >> 1);
lazy[rt] = 0;
}
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
lazy[rt] = 0;
if (l == r)
{
tree[rt].sum = 0;
return;
}
int m = tree[rt].mid();
build(l, m, (rt << 1));
build(m + 1, r, (rt << 1 | 1));
PushUp(rt);
}
void update(LL c, int l, int r, int rt)//表示对区间[l,r]内的每个数均加c,rt是根节点
{
if (tree[rt].l == l&&tree[rt].r==r)
{
lazy[rt] += c;
tree[rt].sum += c*(r - l + 1);
return;
}
if (tree[rt].l == tree[rt].r)return;
int m = tree[rt].mid();
PushDown(rt, tree[rt].r - tree[rt].l + 1);
if (r <= m)update(c, l, r, rt << 1);
else if (l > m)update(c, l, r, rt << 1 | 1);
else
{
update(c, l, m, rt << 1);
update(c, m + 1, r, rt << 1 | 1);
}
PushUp(rt);
}
LL Query(int l, int r, int rt)
{
if (l == tree[rt].l&&r == tree[rt].r)
{
return tree[rt].sum;
}
int m = tree[rt].mid();
PushDown(rt, tree[rt].r - tree[rt].l + 1);
LL res = 0;
if (r <= m)res += Query(l, r, rt << 1);
else if (l > m)res += Query(l, r, rt << 1 | 1);
else
{
res += Query(l, m, rt << 1);
res += Query(m + 1, r, rt << 1 | 1);
}
return res;
}
void DFS(int pre,int nx,int deep)
{
ans[nx]=Query(deep,deep,1);
int Size=e[nx].size();
for(int i=0;i