很好的思维

转化为对树上的深度差分

回朔的思想

对查询离线

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<ctime>
#include<ctype.h>
#include<bitset>
#include<algorithm>
#include<numeric> //accumulate
#define endl "\n"
#define fi first
#define se second
#define FOR(i,s,t) for(int i=(s);i<=(t);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=+; int n;
vector<int> g[maxn];
vector<pair<int,int> > q[maxn];
long long add[maxn];
long long ans[maxn];
void dfs(int u,int pa,int dep,long long sum)
{
for(auto i:q[u])
{
int l=dep,r=dep+i.fi+;
add[l]+=i.se;
if(r<=n)
add[r]-=i.se;
}
sum+=add[dep];
ans[u]=sum;
for(auto i:g[u])
{
if(i==pa)
continue;
dfs(i,u,dep+,sum);
}
for(auto i:q[u])
{
int l=dep,r=dep+i.fi+;
add[l]-=i.se;
if(r<=n)
add[r]+=i.se;
}
}
int main()
{ cin.tie();
cout.tie();
ios_base::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>>n;
for(int i=;i<n;i++)
{
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
int m;
cin>>m;
for(int i=;i<m;i++)
{
int v,d,x;
cin>>v>>d>>x;
q[v].push_back({d,x});
}
dfs(,,,0ll);
for(int i=;i<=n;i++)
cout<<ans[i]<<' '; } /*
void read()
{ char c = getchar();
int x = 0;
for (; (c < 48 || c>57); c = getchar());
for (; c > 47 && c < 58; c = getchar())
{
x = (x << 1) + (x << 3) + c - 48;
}
return x;
}
*/

Vasya and a Tree CodeForces - 1076E的更多相关文章

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

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

  2. 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 ...

  3. Codeforces 1076 E - Vasya and a Tree

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

  4. CodeForces-1076E Vasya and a Tree

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

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

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

  6. 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 ...

  7. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

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

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

  9. Vasya and Beautiful Arrays CodeForces - 354C (数论,枚举)

    Vasya and Beautiful Arrays CodeForces - 354C Vasya's got a birthday coming up and his mom decided to ...

随机推荐

  1. 题解 NOI2004【郁闷的出纳员】

    \[ Preface \] 之前用 treap 打,交了四遍才过. 自学了 fhq treap 后,才意识到是一道 fhq treap 板子题,直接码上,一遍就过. 本题解提供的是 fhq treap ...

  2. postman之设置token

    在做接口测试的时候,很多业务的接口是有依赖性的,下一个接口需要上一个接口的token依赖,例如余额查询接口,需要先登录,才能查询余额.那么,这种接口如何使用postman进行测试呢?今天让我来给小伙伴 ...

  3. 《Head first设计模式》之迭代器模式

    迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 爆炸性新闻:对象村餐厅和对象村煎饼屋合并了! 真是个好消息!现在我们可以在同一个地方,享用煎饼屋美味的煎饼早餐,和好吃 ...

  4. SpringBoot图文教程7—SpringBoot拦截器的使用姿势这都有

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

  5. 08.JS单词整理

    以下为按照文章顺序简单整理的JS单词, 注意:是JS单词注释,部分与英文不符 01.JS语法规范.变量与常量 console——控制台 log——日志 var——变量 variable变量,变化 co ...

  6. .net core 轻量级容器 ServiceProvider 源码分析

    首先看 ServiceCollection 的定义 //定义 public class ServiceCollection : IServiceCollection { private readonl ...

  7. css3新增边框、阴影、边框、背景、文本、字体

    css3和css有什么区别?简单来讲css3是css(层叠样式表)技术的升级版本,css3新特征有很多,例如圆角效果.图形化边界.块阴影与文字阴影.使用RGBA实现透明效果.渐变效果.使用@Font- ...

  8. 聊聊GIS中的坐标系|再版 识别各种数据的坐标系及代码中的坐标系

    本篇讲讲在GIS桌面软件和实际数据中,以及各路GIS有关API的编程中,如何寻找坐标系信息.惯例: 本文约2000字,建议阅读时间10分钟. 作者:博客园/B站/知乎/csdn/小专栏 @秋意正寒 版 ...

  9. ACP知识总结

    由于ACP是一个敏捷开发的系统性知识,下面只针对我自身学习的知识总结,若需要完整的考试学习资料,可评论区或私聊我拿.   敏捷估计与规划.png   ACP知识点锦集.png   敏捷项目软件总结.p ...

  10. BigDecimal精确计算工具类

    前言 在实际开发中,遇到例如货币,统计等商业计算的时候,一般需要采用java.math.BigDecimal类来进行精确计算.而这类操作通常都是可预知的,也就是通用的.所以,写了个工具类来方便以后的工 ...