E - Vasya and a Tree

思路:

dfs动态维护关于深度树状数组

返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作

代码:

  1. #pragma GCC optimize(2)
  2. #pragma GCC optimize(3)
  3. #pragma GCC optimize(4)
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define fi first
  7. #define se second
  8. #define pi acos(-1.0)
  9. #define LL long long
  10. //#define mp make_pair
  11. #define pb push_back
  12. #define ls rt<<1, l, m
  13. #define rs rt<<1|1, m+1, r
  14. #define ULL unsigned LL
  15. #define pll pair<LL, LL>
  16. #define pli pair<LL, int>
  17. #define pii pair<int, int>
  18. #define piii pair<pii, int>
  19. #define mem(a, b) memset(a, b, sizeof(a))
  20. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  21. #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
  22. //head
  23.  
  24. const int N = 3e5 + ;
  25. vector<int> g[N];
  26. vector<pii> que[N];
  27. LL bit[N], ans[N];
  28. int n;
  29. void add(int x, int d) {
  30. while(x <= n) bit[x] += d, x += x&-x;
  31. }
  32. LL sum(int x) {
  33. LL ans = ;
  34. while(x) ans += bit[x], x -= x&-x;
  35. return ans;
  36. }
  37.  
  38. void dfs(int u, int o, int deep) {
  39. for (pii t : que[u]) {
  40. int d = t.fi;
  41. int x = t.se;
  42. int l = deep;
  43. int r = min(deep+d, n);
  44. add(l, x);
  45. add(r+, -x);
  46. }
  47. ans[u] = sum(deep);
  48. for (int v : g[u]) {
  49. if(v != o) dfs(v, u, deep+);
  50. }
  51. for (pii t : que[u]) {
  52. int d = t.fi;
  53. int x = t.se;
  54. int l = deep;
  55. int r = min(deep+d, n);
  56. add(l, -x);
  57. add(r+, +x);
  58. }
  59. }
  60. int main() {
  61. int u, m, v, d, x;
  62. scanf("%d", &n);
  63. for (int i = ; i < n; i++) {
  64. scanf("%d %d", &u, &v);
  65. g[u].pb(v);
  66. g[v].pb(u);
  67. }
  68. scanf("%d", &m);
  69. for (int i = ; i <= m; i++) {
  70. scanf("%d %d %d", &v, &d, &x);
  71. que[v].pb({d, x});
  72. }
  73. dfs(, , );
  74. for (int i = ; i <= n; i++) printf("%lld%c", ans[i], " \n"[i==n]);
  75. return ;
  76. }

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

  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-1076E Vasya and a Tree

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

  4. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

  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. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

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

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

  9. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

随机推荐

  1. pprof函数名未翻译、为函数地址0x00000232382788

    这几天在分析一个性能未达预期的功能,使用gperftools cpu profiler生成后,使用pprof格式化的时候,发现pprof出的结果函数名未翻译.为函数地址,如下所示: 每个节点代表一个函 ...

  2. Improving your submission -- Kaggle Competitions

    1: Improving Our Features In the last mission, we made our first submission to Titanic: Machine Lear ...

  3. 18位身份证验证(Java)加入身份证输入验证是否满足18位代码(修订稿)

    package day20181016; /** * 身份证的验证 34052419800101001X * */ import java.util.Scanner; public class Zuo ...

  4. py4CV例子1猫狗大战和Knn算法

    1.什么是猫狗大战: 数据集来源于Kaggle(一个为开发商和数据科学家提供举办机器学习竞赛.托管数据库.编写和分享代码的平台),原数据集有12500只猫和12500只狗,分为训练.测试两个部分. 2 ...

  5. tf.argmax()以及axis解析

    首先,明确一点,tf.argmax可以认为就是np.argmax.tensorflow使用numpy实现的这个API.    简单的说,tf.argmax就是返回最大的那个数值所在的下标.    这个 ...

  6. 测试浏览器对HTML5标签的支持

    使用相应的浏览器打开链接 https://html5test.com/即可在该页面查看到测试结果.

  7. 对应web的常用flutter应用

    例如,创建一个Text widget: new Text('Hello World', style: new TextStyle(fontSize: 32.0)) 创建一个 Image widget: ...

  8. jquery里面获取div区块的宽度与高度

    https://blog.csdn.net/ll641058431/article/details/52768825 获取宽度 $('div').width();     获取:区块的本身宽度 $(' ...

  9. HIHOcoder 1457 后缀自动机四·重复旋律7

    思路 后缀自动机题目,题目本质上是要求求出所有不同的子串的和,SAM每个节点中存放的子串互不相同,所以对于每个节点的sum,可以发现是可以递推的,每个点对子节点贡献是sum[x]*10+c*sz[x] ...

  10. (zhuan) Some Talks about Dual Learning

    研究|对偶学习:一种新的机器学习范式  this blog copy from: http://www.msra.cn/zh-cn/news/blogs/2016/12/dual-learning-2 ...