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

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

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

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

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

Input

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

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

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

Each of next m m lines contains three integers v i  vi , d i  di , x i  xi (1≤v i ≤n 1≤vi≤n , 0≤d i ≤10 9  0≤di≤109 , 1≤x i ≤10 9  1≤xi≤109 ) — description of the i i -th query.

Output

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

Examples

Input
  1. 5
  2. 1 2
  3. 1 3
  4. 2 4
  5. 2 5
  6. 3
  7. 1 1 1
  8. 2 0 10
  9. 4 10 100
Output
  1. 1 11 1 100 0
Input
  1. 5
  2. 2 3
  3. 2 1
  4. 5 4
  5. 3 4
  6. 5
  7. 2 0 4
  8. 3 10 1
  9. 1 2 3
  10. 2 3 10
  11. 1 1 7
Output
  1. 10 24 14 11 11

Note

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

题意:给定一棵大小为N个树,Q次操作,每次给出三元组(u,d,x)表示给u为根的子树,距离u不超过d的点加值x。

思路:对于每个操作,我们在u处加x,在dep[u+d+1]处减去x。只需要传递一个数组,代表在深度为多少的时候减去多少即可,由于是DFS,满足操作都是在子树里的。

  1. #include<bits/stdc++.h>
  2. #define rep(i,a,b) for(int i=a;i<=b;i++)
  3. #define ll long long
  4. using namespace std;
  5. const int maxn=;
  6. int dep[maxn],N,Laxt[maxn],Next[maxn],To[maxn],cnt;
  7. int laxt2[maxn],next2[maxn],D[maxn],X[maxn],tot; ll ans[maxn];
  8. void add(int u,int v){
  9. Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
  10. }
  11. void add2(int u,int d,int x){
  12. next2[++tot]=laxt2[u]; laxt2[u]=tot; D[tot]=d; X[tot]=x;
  13. }
  14. void dfs(int u,int f,ll sum,ll *mp)
  15. {
  16. dep[u]=dep[f]+; sum-=mp[dep[u]];
  17. for(int i=laxt2[u];i;i=next2[i]){
  18. sum+=X[i];if(dep[u]+D[i]+<=N) mp[dep[u]+D[i]+]+=X[i];
  19. }
  20. ans[u]=sum;
  21. for(int i=Laxt[u];i;i=Next[i])
  22. if(To[i]!=f) dfs(To[i],u,sum,mp);
  23. for(int i=laxt2[u];i;i=next2[i]){
  24. sum-=X[i];if(dep[u]+D[i]+<=N) mp[dep[u]+D[i]+]-=X[i];
  25. }
  26. }
  27. ll mp[maxn];
  28. int main()
  29. {
  30. int u,v,x,Q; scanf("%d",&N);
  31. rep(i,,N-) {
  32. scanf("%d%d",&u,&v);
  33. add(u,v); add(v,u);
  34. }
  35. scanf("%d",&Q);
  36. rep(i,,Q) {
  37. scanf("%d%d%d",&u,&v,&x);
  38. add2(u,v,x);
  39. }
  40.  
  41. dfs(,,0LL,mp);
  42. rep(i,,N) printf("%lld ",ans[i]);
  43. return ;
  44. }

CF1076E:Vasya and a Tree(DFS&差分)的更多相关文章

  1. CF Edu54 E. Vasya and a Tree DFS+树状数组

    Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...

  2. Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)

    https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...

  3. cf1076E Vasya and a Tree (线段树)

    我的做法: 给询问按$deep[v]+d$排序,每次做到某一深度的时候,先给这个深度所有点的值清0,然后直接改v的子树 官方做法比较妙妙: dfs,进入v的时候给$[deep[v],deep[v]+d ...

  4. [CF1076E]Vasya and a Tree

    题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$.最后输出每个节点的值 题解:可以把操作离线,每次开始遍历到一个 ...

  5. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  6. Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)

    题目链接:传送门 题目: E. Vasya and a Tree time limit per test seconds memory limit per test megabytes input s ...

  7. Vasya and a Tree CodeForces - 1076E (线段树 + dfs)

    题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...

  8. CodeForces-1076E Vasya and a Tree

    CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...

  9. Codeforces 1076 E - Vasya and a Tree

    E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...

随机推荐

  1. HttpClient发送get,post接口请求

    HttpClient发送get post接口请求/*  * post  * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...

  2. Cannot find entry file index.android.js in any of the roots:[ Android ]

    Changed the version of react project to a lower one from here npm install -g rninit rninit init [Pro ...

  3. http之post和get请求的区别

    GET请求 GET /books/?sex=man&name=Professional HTTP/1.1 Host: www.wrox.com User-Agent: Mozilla/5.0 ...

  4. WebService-WSDL简单介绍

    一.什么是WSDL 网络服务描述语言(Web Services Description Language)简称WSDL.作用是通过接口之间的调用实现数据的传输.由于WSDL是基于XML格式的,所以它可 ...

  5. C++ 把文件路径中的单斜杠“\”换成双斜杠“\\”

    <pre name="code" class="cpp"> #include <iostream> #include <strin ...

  6. SQL学习笔记六之MySQL数据备份和pymysql模块

    mysql六:数据备份.pymysql模块   阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测 ...

  7. 《Java程序设计》第四章-认识对象

    20145221<Java程序设计>第四章-认识对象 总结 教材学习内容总结 类与对象 定义:对象是Java语言中重要的组成部分,之前学过的C语言是面向过程的,而Java主要是面向对象的. ...

  8. 学习Zookeeper之第1章Zookeeper入门

    第 1 章 Zookeeper入门 1.1 概述 1.2 特点 1.3 数据结构 1.4 应用场景 统一命名服务 统一配置管理 统一集群管理 服务器动态上下线 软负载均衡 1.5 下载地址 第 1 章 ...

  9. HDU 6342 Expression in Memories(模拟)多校题解

    题意:给你一个规则,问你写的对不对. 思路:规则大概概括为:不能出现前导零,符号两边必须是合法数字.我们先把所有问号改好,再去判断现在是否合法,这样判断比一边改一边判断容易想. 下面的讲解问号只改为+ ...

  10. Bzoj 4371: [IOI2015]sorting排序 二分

    题目 似乎很久没写题解了... 这题是校里胡策的时候的题,比赛因为评测机有点慢+自己代码常数大没快读...被卡t了,但是bzoj上还是A了的...,因为bzoj时限比较宽可以不卡常. 题解: 首先可以 ...