1. 程式人生 > >Codeforces-1076E:Vasya and a Tree(樹狀陣列)

Codeforces-1076E:Vasya and a Tree(樹狀陣列)

E. Vasya and a Tree
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output

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

Let d(i,j)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)kd(x,y)≤k.

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

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

Input
The first line contains single integer n(1n3105)n (1≤n≤3⋅10^5) — number of vertices in the tree.

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

Next line contains single integer m

(1m3105)m (1≤m≤3⋅10^5) — number of queries.

Each of next m lines contains three integers vi,di,xi(1vin,0di109,1xi109)vi, di, xi (1≤v_i≤n, 0≤d_i≤10^9, 1≤x_i≤10^9) — description of the i-th query.

Output
Print n 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.

思路:先將所有操作存下來。然後以深度為節點建立樹狀陣列。從根節點1開始進行DFS。

當遍歷到一個節點時,把當前節點的操作影響利用差分更新到樹狀數組裡,然後查詢樹狀陣列並更新當前節點答案。

如果把當前節點的所有子節點都遍歷完後,再更新樹狀陣列消除當前節點的操作,這樣的話再遍歷其它節點時,不會產生影響。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6+10;
const int MOD=1e9+7;
typedef long long ll;
vector<int>e[MAX];
vector<pair<int,int> >v[MAX];
ll A[MAX];
int n;
void add(int x,int y){while(x<=n){A[x]+=y;x+=x&(-x);}}
ll ask(int x){ll tot=0;while(x){tot+=A[x];x-=x&(-x);}return tot;}
ll ans[MAX];
void dfs(int k,int fa,int d)
{
    for(int i=0;i<v[k].size();i++)
    {
        add(d,v[k][i].second);
        if(d+v[k][i].first+1<=n)add(d+v[k][i].first+1,-v[k][i].second);
    }
    ans[k]=ask(d);
    for(int i=0;i<e[k].size();i++)
    {
        if(e[k][i]==fa)continue;
        dfs(e[k][i],k,d+1);
    }
    for(int i=0;i<v[k].size();i++)
    {
        add(d,-v[k][i].second);
        if(d+v[k][i].first+1<=n)add(d+v[k][i].first+1,v[k][i].second);
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int k,de,val;
        scanf("%d%d%d",&k,&de,&val);
        v[k].push_back({de,val});
    }
    dfs(1,0,1);
    for(int i=1;i<=n;i++)printf("%lld ",ans[i]);
    return 0;
}