CF1076E:Vasya and a Tree(DFS&差分)
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
5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100
1 11 1 100 0
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
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,满足操作都是在子树里的。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
int dep[maxn],N,Laxt[maxn],Next[maxn],To[maxn],cnt;
int laxt2[maxn],next2[maxn],D[maxn],X[maxn],tot; ll ans[maxn];
void add(int u,int v){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void add2(int u,int d,int x){
next2[++tot]=laxt2[u]; laxt2[u]=tot; D[tot]=d; X[tot]=x;
}
void dfs(int u,int f,ll sum,ll *mp)
{
dep[u]=dep[f]+; sum-=mp[dep[u]];
for(int i=laxt2[u];i;i=next2[i]){
sum+=X[i];if(dep[u]+D[i]+<=N) mp[dep[u]+D[i]+]+=X[i];
}
ans[u]=sum;
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=f) dfs(To[i],u,sum,mp);
for(int i=laxt2[u];i;i=next2[i]){
sum-=X[i];if(dep[u]+D[i]+<=N) mp[dep[u]+D[i]+]-=X[i];
}
}
ll mp[maxn];
int main()
{
int u,v,x,Q; scanf("%d",&N);
rep(i,,N-) {
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
scanf("%d",&Q);
rep(i,,Q) {
scanf("%d%d%d",&u,&v,&x);
add2(u,v,x);
} dfs(,,0LL,mp);
rep(i,,N) printf("%lld ",ans[i]);
return ;
}
最新文章
- Handler Should be static or leaks Occur?
- VS下的Resharper插件报错&ldquo;Can not resolve symbol&rdquo;的解决办法
- sqlserver 中的NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX
- bug集合
- 利用ajax获取到的网页源码不能执行js代码
- asp.net select Case条件语句的使用方法
- Java Map集合按照key和value排序之法
- redis 学习笔记三(队列功能)
- CentOS 安装redis2.8.13 提醒";libc.so.6: version `GLIBC_2.14&#39; not found";系统的glibc版本太低
- OCP读书笔记(13) - 管理内存
- [git] 细说commit (git add/commit/diff/rm/reset 以及 index 的概念)
- Python pandas ERROR 2006 (HY000): MySQL server has gone away
- [HAOI2008]糖果传递
- c#判断是否有网络
- 高效的 JavaScript
- 51nod1238 最小公倍数之和 V3
- [转载]window.location.href的用法(动态输出跳转)
- ML平台_小米深度学习平台的架构与实践
- [LeetCode]138复制带随机指针的链表
- (C/C++学习笔记) 十一. 数组
热门文章
- Virtualbox中win7虚拟机中U盘不可用问题的解决
- python3 requests 进行接口测试、爬虫使用总结
- 3.5 Templates -- Binding Element Attributes(绑定元素属性)
- (6)Cocos2d-x 3.0坐标系详解
- hdu5072 容斥+枚举
- python学习之【16】网络编程
- Python3.x:os.path模块
- 详解KMP算法【转】
- HDU 6351 Beautiful Now(DFS)多校题解
- http://www.artrobot.com/北京钢铁侠