题目大意:

给一棵树root=1的树:

给一些操作:u  v 的路径所有节点的node + val;

最后m个询问:u 节点(包括u) sum%mod 是多少。

LCA + RMQ;

我们每次mark ,u , v, +=val;

fa=lca[u,v])-=val;

fa[fa]-=val

你会发现下次DFS一边的时候可以把所以答案更新出来。DFS 从下往上更新。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. using namespace std;
  14. #define N 100001
  15. #define LN 20
  16. #define mod 1000000007
  17.  
  18. vector<int>mp[N];
  19. int mark[N];
  20. int dp[LN][N];
  21. int dep[N];
  22. int ans[N],tmp[N];
  23.  
  24. int lca(int l,int r)
  25. {
  26. if (dep[l]<dep[r]) swap(l,r);
  27. int dif=dep[l]-dep[r];
  28. for (int i=;i<LN;i++)
  29. if ((dif>>i)&) l=dp[i][l];
  30.  
  31. if (l==r) return l;
  32.  
  33. for (int i=LN-;i>=;i--)
  34. if (dp[i][l]!=dp[i][r]) l=dp[i][l],r=dp[i][r];
  35. return dp[][l];
  36. }
  37.  
  38. void dfs(int u,int fa,int level)
  39. {
  40. dp[][u]=fa;
  41. dep[u]=level;
  42. for (int i=;i<mp[u].size();i++)
  43. {
  44. int v=mp[u][i];
  45. if (v==fa) continue;
  46. dfs(v,u,level+);
  47. }
  48. }
  49.  
  50. int dfs2(int u,int fa=)
  51. {
  52. int sum=mark[u];
  53. for (int i=;i<mp[u].size();i++)
  54. {
  55. int v=mp[u][i];
  56. if (v==fa) continue;
  57. dfs2(v,u);
  58. sum+=tmp[v];
  59. sum%=mod;
  60. ans[u]+=ans[v];
  61. ans[u]%=mod;
  62. }
  63. tmp[u]=sum;
  64. ans[u]+=tmp[u];
  65. ans[u]%=mod;
  66. }
  67.  
  68. int main()
  69. {
  70. int n,u,q;
  71. scanf("%d",&n);
  72. scanf("%d%d",&u,&q);
  73. // for (int i=0;i<=n;i++) mp[i].clear();
  74. for (int i=;i<n;i++)
  75. {
  76. int a,b;
  77. scanf("%d%d",&a,&b);
  78. mp[a].push_back(b);
  79. mp[b].push_back(a);
  80. }
  81. dfs(,,);
  82. for (int j=;j<LN;j++)
  83. for (int i=;i<=n;i++)
  84. dp[j][i]=dp[j-][dp[j-][i]];
  85. while (u--)
  86. {
  87. int a,b,c;
  88. scanf("%d%d%d",&a,&b,&c);
  89. mark[a]+=c;
  90. mark[b]+=c;
  91. mark[a]%=mod;
  92. mark[b]%=mod;
  93. int l=lca(a,b);
  94. mark[l]-=c;
  95. mark[l]%=mod;
  96. mark[dp[][l]]-=c;
  97. mark[dp[][l]]%=mod;
  98. }
  99.  
  100. dfs2();
  101. while (q--)
  102. {
  103. int x;
  104. scanf("%d",&x);
  105. printf("%d\n",(ans[x]+mod)%mod);
  106. }
  107.  
  108. return ;
  109. }

codechef FUN WITH TREES的更多相关文章

  1. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  2. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  3. codechef : TREDEG , Trees and Degrees

    其实有原题,生成树计数 然鹅这题里面是两道题, 50pts 可以用上面那题的做法直接过掉,另外 50pts 要推推式子,搞出 O n 的做法才行(毕竟多项式常数之大您是知道的) 虽说这道题里面是没有 ...

  4. 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)

    点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...

  5. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  6. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. linux下查看nginx配置文件地址

    which nginx/usr/sbin/nginx -t

  2. 洛谷 P2788 数学1(math1)- 加减算式

    题目背景 蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白. 题目描述 好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式.显然成功就在眼前了. ...

  3. codevs 2046 孪生素数 3 (水题日常)

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 在质数的大家庭中,大小之差不超过2的两个质数称它俩为一对孪生素数,如2和3.3和5 ...

  4. vue 自定义组件 v-model双向绑定、 父子组件同步通信【转】

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  5. CF1179D Fedor Runs for President [DP,斜率优化]

    Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...

  6. nginx 获取真实ip

    使用阿里云SLB,无法获取真实ip问题 官方给出的是如下用法,需要安装模块,大体上是没有错的,但是比较模糊,实际操作中可能会踩坑,所以参考学习即可,不必照搬.(那个http_realip_module ...

  7. qt c++对象头文件如何相互包含

    今天在写qt时,遇到了两个类相互包含的问题,类A要用到类B,类B要用到类A. 类A:a.h #ifndef A_H #define A_H #include <b.h> class A { ...

  8. K8S部署

    k8S部署 柯穴上网 安装openvpn来获取docker镜像(不是本文重点不做详述) 软件包安装 1 关闭iptables,禁用firewalld,关闭selinux 2 配置yum仓库(使用阿里云 ...

  9. POJ 3122 pie (二分法)

    Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have ...

  10. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...