分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时

只会影响的子树的和,最优值也是子树最大的值

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. typedef long long LL;
  7. const int N=1e5+;
  8. const LL INF=1ll*1e11;
  9. struct Edge{
  10. int v,next;
  11. }edge[N<<];
  12. int head[N],tot;
  13. void add(int u,int v){
  14. edge[tot].v=v;
  15. edge[tot].next=head[u];
  16. head[u]=tot++;
  17. }
  18. int s[N],t[N],clk,match[N],n,m,a[N];
  19. LL sum[N],c[N<<],lz[N<<];
  20. void dfs(int u,int f){
  21. s[u]=++clk;match[s[u]]=u;
  22. sum[u]+=sum[f];
  23. for(int i=head[u];~i;i=edge[i].next){
  24. int v=edge[i].v;
  25. if(v==f)continue;
  26. dfs(v,u);
  27. }
  28. t[u]=clk;
  29. }
  30. void up(int rt){
  31. c[rt]=max(c[rt<<],c[rt<<|]);
  32. }
  33. void down(int rt){
  34. if(lz[rt]){
  35. c[rt<<]+=lz[rt];
  36. c[rt<<|]+=lz[rt];
  37. lz[rt<<]+=lz[rt];
  38. lz[rt<<|]+=lz[rt];
  39. lz[rt]=;
  40. }
  41. }
  42. void build(int rt,int l,int r){
  43. lz[rt]=;
  44. if(l==r){c[rt]=sum[match[l]];return;}
  45. int m=(l+r)>>;
  46. build(rt<<,l,m);
  47. build(rt<<|,m+,r);
  48. up(rt);
  49. }
  50. int tmp;
  51. void modify(int rt,int l,int r,int x,int y){
  52. if(x<=l&&r<=y){
  53. lz[rt]+=1ll*tmp;
  54. c[rt]+=1ll*tmp;
  55. return;
  56. }
  57. int m=(l+r)>>;
  58. down(rt);
  59. if(x<=m)modify(rt<<,l,m,x,y);
  60. if(y>m)modify(rt<<|,m+,r,x,y);
  61. up(rt);
  62. }
  63. LL ask(int rt,int l,int r,int x,int y){
  64. if(x<=l&&r<=y)return c[rt];
  65. LL ans=-INF;
  66. int m=(l+r)>>;
  67. down(rt);
  68. if(x<=m)ans=max(ans,ask(rt<<,l,m,x,y));
  69. if(y>m)ans=max(ans,ask(rt<<|,m+,r,x,y));
  70. return ans;
  71. }
  72. int main()
  73. {
  74. int T,cas=;
  75. scanf("%d",&T);
  76. while(T--){
  77. printf("Case #%d:\n",++cas);
  78. scanf("%d%d",&n,&m);
  79. for(int i=;i<=n;++i)head[i]=-;
  80. clk=tot=;
  81. for(int i=;i<n;++i){
  82. int u,v;
  83. scanf("%d%d",&u,&v);
  84. ++u,++v;
  85. add(u,v),add(v,u);
  86. }
  87. for(int i=;i<=n;++i)
  88. scanf("%I64d",&sum[i]),a[i]=(int)sum[i];
  89. dfs(,);
  90. build(,,n);
  91. for(int i=;i<m;++i){
  92. int op,x,y;
  93. scanf("%d%d",&op,&x);
  94. ++x;
  95. if(!op){
  96. scanf("%d",&y);
  97. tmp=y-a[x];
  98. a[x]=y;
  99. if(tmp)modify(,,n,s[x],t[x]);
  100. }
  101. else{
  102. printf("%I64d\n",ask(,,n,s[x],t[x]));
  103. }
  104. }
  105. }
  106. return ;
  107. }

HDU5692 Snacks DFS+线段树的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  3. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  4. HDU5692 Snacks DFS序 线段树

    去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...

  5. hdu-5692 Snacks(dfs序+线段树)

    题目链接: Snacks Problem Description   百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的 ...

  6. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

  7. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  8. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  9. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

随机推荐

  1. swift基础--数组、字典

    (1)初始化 (2)新增.修改.删除 (3)清空 (4)遍历 var array1 = ["x","y","z"] var array2:[ ...

  2. c++ 异常处理 assert | try

    #include <iostream> #include <cassert> using namespace std; int main() { ; assert(i == ) ...

  3. AForm

    相信大部分程序员都接触过表单,表单是收集用户输入的不二之选,但是表单的开发又是最繁琐.最复杂的,简单地说,开发表单你需要涉及到很多知识: 布局,表单如何布局排版,看起来最清晰整洁,且符合用户体验 控件 ...

  4. The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemE:Easy Task

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2969 全场第一水题.我不知道怎么解释,看代码就好了... #include ...

  5. spoj 416

    又臭又长的烂代码 ...... #include <iostream> #include <cstdio> #include <cstring> #include ...

  6. Akka Stream文档翻译:Quick Start Guide: Reactive Tweets

    Quick Start Guide: Reactive Tweets 快速入门指南: Reactive Tweets (reactive tweets 大概可以理解为“响应式推文”,在此可以测试下GF ...

  7. Unity寻路的功能总结

    源地址:http://blog.csdn.net/sgnyyy/article/details/21878163 1. 利用Unity本身自带的NavMesh 这篇文章已经比较详细,可能对于很多需要a ...

  8. zoj 3329 One Person Game 概率DP

    思路:这题的递推方程有点麻烦!! dp[i]表示分数为i的期望步数,p[k]表示得分为k的概率,p0表示回到0的概率: dp[i]=Σ(p[k]*dp[i+k])+dp[0]*p0+1 设dp[i]= ...

  9. linux samba.tar.gz安装和配置

    安装步骤: 1. tar -xzvf samba-3.5.10.tar.gz2. cd samba-3.5.103. cd source34. ./autogen.sh  如果出现:./autogen ...

  10. Amzon MWS API开发之订单接口

    Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据. 在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息.如下: 在此我将所有信息定义在一个类中 ...