HDU - 3966

思路 :树链剖分就是可以把一个路径上的点映射成几段连续的区间上。这样对于连续的区间可以用线段树维护,

对于每一段连续的区间都可以通过top [ ]数组很快的找到这段连续区间的头。跳的过程类似于 lca ,但这里 要注意的是

每一个点只属于一条链。 (重载运算符时要注意   node 一个 新的结点,不要 乱用*this 指针)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MID int m = (l+r)/2
  4. #define maxn 56789
  5. #define inf 0x3f3f3f3f
  6. struct node
  7. {
  8. int sum,lazy,cnt;
  9. node()
  10. {
  11. sum=lazy=cnt=0;
  12. }
  13. node operator+(const node &a)const
  14. {
  15. node ret;
  16. ret.sum=a.sum+sum;
  17. ret.cnt=a.cnt+cnt;
  18. return ret;
  19. }
  20. } tree[maxn*4];
  21. char str[12];
  22. vector<int>edge[maxn];
  23. int data[maxn],n,m,id[maxn],fa[maxn],u,ans;
  24. int son[maxn],top[maxn],tid[maxn],cnt,v,ad;
  25. int deep[maxn],siz[maxn],id_data[maxn],q;
  26. void pushdown(int root)
  27. {
  28. if(tree[root].lazy==0)return ;
  29. tree[root*2].lazy+=tree[root].lazy;
  30. tree[root*2+1].lazy+=tree[root].lazy;
  31. tree[root*2].sum+=tree[root*2].cnt*tree[root].lazy;
  32. tree[root*2+1].sum+=tree[root*2+1].cnt*tree[root].lazy;
  33. tree[root].lazy=0;
  34. }
  35. void bulid(int root,int l,int r)
  36. {
  37. tree[root].lazy=0;
  38. if(l==r)
  39. {
  40. tree[root].sum=id_data[l];
  41. tree[root].cnt=1;
  42. return ;
  43. }
  44. MID;
  45. bulid(root*2,l,m);
  46. bulid(root*2+1,m+1,r);
  47. tree[root]=tree[root*2]+tree[root*2+1];
  48. }
  49. void updata(int root,int l,int r,int L,int R,int ad)
  50. {
  51. if(r<L||l>R)return;
  52. if(L<=l&&r<=R)
  53. {
  54. tree[root].sum+=tree[root].cnt*ad;
  55. tree[root].lazy+=ad;
  56. return ;
  57. }
  58. pushdown(root);
  59. MID;
  60. updata(root*2,l,m,L,R,ad);
  61. updata(root*2+1,m+1,r,L,R,ad);
  62. tree[root]=tree[root*2]+tree[root*2+1];
  63. }
  64. void query(int root,int l,int r,int L,int R)
  65. {
  66. if(r<L||l>R)return ;
  67. if(L<=l&&r<=R)
  68. {
  69. ans+=tree[root].sum;
  70. return;
  71. }
  72. pushdown(root);
  73. MID;
  74. query(root*2,l,m,L,R);
  75. query(root*2+1,m+1,r,L,R);
  76. }
  77. void dfs1(int u,int pre,int ide)
  78. {
  79. son[u]=-1,siz[u]=1;
  80. deep[u]=ide,fa[u]=pre;
  81. for(int i=0; i<edge[u].size(); i++)
  82. {
  83. int v=edge[u][i];
  84. if(v==pre)continue;
  85. dfs1(v,u,ide+1);
  86. if(son[u]==-1||siz[son[u]]<siz[v])
  87. son[u]=v;
  88. }
  89. }
  90. void dfs2(int u,int tp)
  91. {
  92. top[u]=tp, tid[u]=++cnt;
  93. id_data[cnt]=data[u];
  94. if(son[u]!=-1)dfs2(son[u],tp);
  95. for(int i=0; i<edge[u].size(); i++)
  96. {
  97. int v=edge[u][i];
  98. if(v==fa[u]||v==son[u])continue;
  99. dfs2(v,v);
  100. }
  101. }
  102. void solve(int x,int y,int ad)
  103. {
  104. int tx=top[x],ty=top[y];
  105. while(tx!=ty)
  106. {
  107. if(deep[tx]<deep[ty])swap(x,y),swap(tx,ty);
  108. updata(1,1,n,tid[tx],tid[x],ad);
  109. x=fa[tx],tx=top[x];
  110. }
  111. if(deep[x]<deep[y])swap(x,y);
  112. updata(1,1,n,tid[y],tid[x],ad);
  113. }
  114. int main()
  115. {
  116. while(~scanf("%d%d%d",&n,&m,&q))
  117. {
  118. cnt=0;
  119. for(int i=1; i<=n; i++)
  120. {
  121. scanf("%d",&data[i]);
  122. edge[i].clear();
  123. }
  124. while(m--)
  125. {
  126. scanf("%d%d",&u,&v);
  127. edge[u].push_back(v);
  128. edge[v].push_back(u);
  129. }
  130. dfs1(1,0,1);
  131. dfs2(1,1);
  132. bulid(1,1,n);
  133. while(q--)
  134. {
  135. scanf("%s",str);
  136. if(str[0]=='I')
  137. {
  138. scanf("%d%d%d",&u,&v,&ad);
  139. solve(u,v,ad);
  140. }
  141. else if(str[0]=='D')
  142. {
  143. scanf("%d%d%d",&u,&v,&ad);
  144. solve(u,v,-ad);
  145. }
  146. else
  147. {
  148. scanf("%d",&u);
  149. ans=0;
  150. query(1,1,n,tid[u],tid[u]);
  151. printf("%d\n",ans);
  152. }
  153. }
  154. }
  155. return 0;
  156. }

  

Aragorn's Story HDU - 3966 -树剖模板的更多相关文章

  1. A - Aragorn's Story HDU - 3966 树剖裸题

    这个题目是一个比较裸的树剖题,很好写. http://acm.hdu.edu.cn/showproblem.php?pid=3966 #include <cstdio> #include ...

  2. HDU 3966 (树链剖分+线段树)

    Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...

  3. AC日记——Aragorn's Story HDU 3966

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【树链剖分】洛谷P3384树剖模板

    题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操作2: 格式 ...

  5. 洛谷树剖模板题 P3384 | 树链剖分

    原题链接 对于以u为根的子树,后代节点的dfn显然比他的dfn大,我们可以记录一下回溯到u的dfn,显然这两个dfn构成了一个连续区间,代表u及u的子树 剩下的就和树剖一样了 #include< ...

  6. HDU 3966 树链剖分+树状数组 模板

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  8. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  9. hdu 3966 树链剖分

    思路:树链剖分入门题,我这门入得好苦啊,程序很快写出来了,可是在LCA过程中把update函数里的左右边界位置写反了,一直RE到死. #pragma comment(linker, "/ST ...

随机推荐

  1. 【洛谷P2822 组合数问题】

    题目连接 #include<iostream> #include<cstring> #include<cstdio> #include<cctype> ...

  2. [APIO2007] 风铃

    题目链接 可能是个树上 DP?指针真好玩 23333. 首先对于所有玩具如果有深度差超过 1 的就是无解(在这里贡献 WA * 3),所以 dfs 一遍记录深度是有必要的…… 然后如果有一个点的两颗子 ...

  3. Python统计词频的几种方式

    语料 text = """My fellow citizens: I stand here today humbled by the task before us, gr ...

  4. Docker下安装Jenkins

    Docker安装参见:https://www.cnblogs.com/hackyo/p/9280042.html 安装Jenkins: docker run \ -u root \ --rm \ -d ...

  5. JN_0004:轻松解码类似eval(function(p,a,c,k,e,d){}))的JavaScript代码

    百度访问统计代码JavaScript源码:红色加粗部分将是要修改的地方.eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"&qu ...

  6. MySQL关于日志配置安全整改及处理方法

    [环境介绍] 系统环境:Linux + mysql 5.7.18 + 主从复制架构 [背景描述] 需求:MySQL数据库都有每年的集团安全整改,常常要求弱口令扫描,基线扫描,漏洞扫描等等.对于MySQ ...

  7. 液晶流在齐次 Besov 空间中的正则性准则

    在 [Zhang, Zujin. Regularity criteria for the three dimensional Ericksen–Leslie system in homogeneous ...

  8. 使用sessionStorage、localStorage存储数组与对象

    先介绍一下localStorage localStorage对象是HTML5的客户端存储持久化数据的方案.为了能访问到同一个localStorage对象,页面必须来自同一个域名(子域名无效),使用同一 ...

  9. Django 反向解析

    #1,定义: #随着功能的增加会出现更多的视图,可能之前配置的正则表达式不够准确,于是就要修改正则表达式,但是正则表达式一旦修改了,之前所有对应的超链接都要修改,真是一件麻烦的事情,而且可能还会漏掉一 ...

  10. spring事务源码分析结合mybatis源码(三)

    下面将结合mybatis源码来分析下,这种持久化框架是如何对connection使用,来达到spring事务的控制. 想要在把mybatis跟spring整合都需要这样一个jar包:mybatis-s ...