CodeForces1076E、Vasya and a Tree(线段树+离线)

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:

  • 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

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 <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
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;
    }
};
vector<int>e[maxn];
vector<OP>op[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<Size;i++)
    {
        int to =e[nx][i];
        if(to!=pre)
        {
            int sizeoop=op[to].size();
            for(int j=0;j<sizeoop;j++)
            {
                OP p=op[to][j];
                int l=deep+1,r=deep+1+p.dep;
                update(p.w,l,r,1);
            }
            DFS(nx,to,deep+1);

            for(int j=0;j<sizeoop;j++)
            {
                OP p=op[to][j];
                int l=deep+1,r=deep+1+p.dep;
                update(-p.w,l,r,1);
            }
        }
    }
    return ;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int u,dep;
        LL w;
        scanf("%d %d %lld",&u,&dep,&w);
        op[u].push_back({u,dep,w});
    }
    build(0,maxn-1,1);
    int sizeoop=op[1].size();
    for(int i=0;i<sizeoop;i++)
    {
        OP p=op[1][i];
        int l=1,r=1+p.dep;
        update(p.w,l,r,1);
    }
    DFS(-1,1,1);
    for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值