题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$。最后输出每个节点的值

题解:可以把操作离线,每次开始遍历到一个节点,把以它为根的操作加上,结束时把这个点的操作删去。

因为是$dfs$,所以一个点对同一深度的贡献是一定的,可以用树状数组区间加减,求前缀和。但是因为是$dfs$,完全可以把前缀和传入,就不需要树状数组了。

卡点:

C++ Code:

#include <cstdio>
#include <vector>
#include <cctype>
namespace __IO {namespace R {int x, ch;inline int read() {ch = getchar();while (isspace(ch)) ch = getchar();for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);return x;}}
}
using __IO::R::read;#define maxn 300010int head[maxn], cnt;
struct Edge {int to, nxt;
} e[maxn << 1];
inline void add(int a, int b) {e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
}int n, m;
struct Modify {int d, x;inline Modify() {}inline Modify(int __d, int __x) :d(__d), x(__x){}
};
std::vector<Modify> S[maxn];long long pre[maxn], ans[maxn];
void dfs(int u, int fa = 0, int dep = 0, long long sum = 0) {for (std::vector<Modify>::iterator it = S[u].begin(); it != S[u].end(); it++) {pre[dep] += it -> x;if (dep + it -> d + 1 <= n) pre[dep + it -> d + 1] -= it -> x;}sum += pre[dep];ans[u] = sum;for (int i = head[u]; i; i = e[i].nxt) {int v = e[i].to;if (v != fa) dfs(v, u, dep + 1, sum);}for (std::vector<Modify>::iterator it = S[u].begin(); it != S[u].end(); it++) {pre[dep] -= it -> x;if (dep + it -> d + 1 <= n) pre[dep + it -> d + 1] += it -> x;}
}
int main() {n = read();for (int i = 1, a, b; i < n; i++) {a = read(), b = read();add(a, b);add(b, a);}m = read();for (int i = 1, v, d, x; i <= m; i++) {v = read(), d = read(), x = read();S[v].push_back(Modify(d, x));}dfs(1);for (int i = 1; i <= n; i++) {printf("%lld", ans[i]);putchar(i == n ? '\n' : ' ');}return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9995403.html

[CF1076E]Vasya and a Tree相关推荐

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

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

  2. Educational Codeforces Round 54 (Rated for Div. 2): E. Vasya and a Tree(DFS+差分)

    题意: 给你一棵n个节点的树,每个点有一个权值,初始全为0,m次操作,每次三个数(v, d, x)表示只考虑以v为根的子树,将所有与v点距离小于等于d的点权值全部加上x,求所有操作完毕后,所有节点的值 ...

  3. CodeForces - 1076E Vasya and a Tree 树剖?nono dfs+树状数组

    题目链接:https://cn.vjudge.net/problem/CodeForces-1076E 题解:树状数组维护下深度,到达每个节点,对于每次更新容斥一下,对于直接查询即可 #include ...

  4. CoderForces Round54 (A~E)

    ProblemA Minimizing the String 题目链接 题解:这一题读完题就写了吧.就是让你删除一个字母,使得剩下的字符组成的字符串的字典序最小:我们只要第一个当前位置的字符比下一个字 ...

  5. Educational Codeforces Round 54 (Rated for Div.2)

    Educational Codeforces Round 54 (Rated for Div.2) D. Edge Deletion 题意:一张n个点的无向图,保留其中k条边,使得有尽可能多的点与1的 ...

  6. 【CodeForces - 260D】Black and White Tree (思维构造,猜结论,细节,构造一棵树)

    题干: The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-dire ...

  7. 【CodeForces - 1051A】Vasya And Password (构造,水题)

    题干: Vasya came up with a password to register for EatForces - a string ss. The password in EatForces ...

  8. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

最新文章

  1. SQL Server插入中文出现乱码??的解决办法
  2. linux常用高级命令,Linux常用高级文件操作命令
  3. vue解构赋值_前端开发es6知识 模块化、解构赋值、字符串模板
  4. IBM云计算带我们进入新服务经济时代
  5. 获得Open Images冠军,商汤TSD目标检测算法入选CVPR 2020 ​
  6. C语言原码、反码、补码
  7. windows dos 命令
  8. python上网行为分析_用 Python 一键分析你的上网行为, 看是在认真工作还是摸鱼...
  9. 分享超好用的截动图工具ScreenToGif
  10. python矩阵转置_矩阵转置python
  11. 用git下载github项目失败werning : Clone succeeded, but checkout failed.
  12. 网站根目录文件权限设置推荐
  13. java输入无名粉_05·无名粉店(上)
  14. Hadoop in action 第45678章
  15. 2023年中职网络安全竞赛服务远程控制任务解析
  16. 锂离子电池是由什么组成的
  17. (二)编译PVE内核5.10.6-1-pve及安装内核补丁fullconeNat
  18. Google Deepmind的自杀开关研究或缓解人工智能恐慌
  19. Linux文件属主和属组 概念
  20. 【C语言】如何将函数内部申请的内存,放到函数外部也能引用到

热门文章

  1. Apache Kafka-通过concurrency实现并发消费
  2. 白话Elasticsearch20-深度探秘搜索技术之使用rescoring机制优化近似匹配搜索的性能
  3. tablepc是什么平板电脑_54位平板电脑充电柜长什么样?安和力
  4. 自定义本地Maven 仓库 配置
  5. 如何基于多线程队列简单实现mq
  6. Android 出现“此用户无法使用开发者选项”问题
  7. html app的登陆、注册,登陆_注册.html
  8. standard python venv module_python 在venv中报错 ModuleNotFoundError: No module named 'MySQLdb'
  9. ios 添加浮动效果_iOS实现拖拽View跟随手指浮动效果
  10. ZieglerNicholas PID GUI控制设计