题意:给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值

裸裸的树剖

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. using namespace std;
  8.  
  9. typedef long long LL;
  10.  
  11. #define N 50010
  12.  
  13. struct Node
  14. {
  15. int to,next;
  16. }e[N<<];
  17.  
  18. int id,cnt;
  19. int head[N];
  20. int num[N],siz[N],top[N],son[N];
  21. int dep[N],pos[N],rank1[N],fa[N];
  22.  
  23. LL sum[N<<],add[N<<];
  24.  
  25. char s[];
  26.  
  27. int n,m,q;
  28. int u,v;
  29. int al,ar,ask;
  30.  
  31. inline void init()
  32. {
  33. memset(son,-,sizeof(son));
  34. memset(head,-,sizeof(head));
  35. id=;
  36. cnt=;
  37. }
  38.  
  39. void link(int x,int y)
  40. {
  41. e[++cnt]=(Node){y,head[x]};
  42. head[x]=cnt;
  43. }
  44.  
  45. void dfs(int x,int father,int d)
  46. {
  47. siz[x]=;
  48. dep[x]=d;
  49. fa[x]=father;
  50. for (int i=head[x];~i;i=e[i].next)
  51. {
  52. int t=e[i].to;
  53. if (t!=fa[x])
  54. {
  55. dfs(t,x,d+);
  56. siz[x]+=siz[t];
  57. if (son[x]==- || siz[x]>siz[son[x]])
  58. son[x]=t;
  59. }
  60. }
  61. }
  62.  
  63. void dfs2(int x,int cha)
  64. {
  65. top[x]=cha;
  66. pos[x]=++id;
  67. rank1[pos[x]]=x;
  68. if (son[x]==-)
  69. return ;
  70. dfs2(son[x],cha);
  71. for(int i=head[x];~i;i=e[i].next)
  72. {
  73. int t=e[i].to;
  74. if(t!=son[x] && t!=fa[x])
  75. dfs2(t,t);
  76. }
  77. }
  78.  
  79. void pushup(int now)
  80. {
  81. sum[now]=max(sum[now<<],sum[now<<|]);
  82. }
  83.  
  84. void pushdown(int now,int m)
  85. {
  86. if (add[now])
  87. {
  88. add[now<<]+=add[now];
  89. add[now<<|]+=add[now];
  90. sum[now<<]+=add[now]*(m-(m>>));
  91. sum[now<<|]+=add[now]*(m>>);
  92. add[now]=;
  93. }
  94. }
  95.  
  96. void build(int nowl,int nowr,int now)
  97. {
  98. add[now]=;
  99. if (nowl==nowr)
  100. {
  101. sum[now]=num[rank1[nowl]];
  102. return ;
  103. }
  104. int mid=(nowl+nowr)>>;
  105. build(nowl,mid,now<<);
  106. build(mid+,nowr,now<<|);
  107. pushup(now);
  108. }
  109.  
  110. void update(int nowl,int nowr,int now,int L,int R,int d)
  111. {
  112. if (nowl>=L && nowr<=R)
  113. {
  114. add[now]+=d;
  115. sum[now]+=d*(nowr-nowl+);
  116. return ;
  117. }
  118. pushdown(now,nowr-nowl+);
  119. int mid=nowl+nowr>>;
  120. if (L<=mid) update(nowl,mid,now<<,L,R,d);
  121. if (mid<R) update(mid+,nowr,now<<|,L,R,d);
  122. pushup(now);
  123. }
  124.  
  125. int query(int nowl,int nowr,int now,int d)
  126. {
  127. int res();
  128. if (nowl==nowr)
  129. return sum[now];
  130. pushdown(now,nowr-nowl+);
  131. int mid=nowl+nowr>>;
  132. if (d<=mid) res=query(nowl,mid,now<<,d);
  133. else res=query(mid+,nowr,now<<|,d);
  134. pushup(now);
  135. return res;
  136. }
  137.  
  138. void work(int x,int y,int val)
  139. {
  140. while (top[x]!=top[y])
  141. {
  142. if (dep[top[x]]<dep[top[y]])
  143. swap(x,y);
  144. update(,n,,pos[top[x]],pos[x],val);
  145. x=fa[top[x]];
  146. }
  147. if (dep[x]>dep[y])
  148. swap(x,y);
  149. update(,n,,pos[x],pos[y],val);
  150. }
  151. int main()
  152. {
  153. while (scanf("%d%d%d",&n,&m,&q)!=EOF && n+m+q)
  154. {
  155. init();
  156. for (int i=;i<=n;i++)
  157. scanf("%d",&num[i]);
  158. for (int i=;i<=m;i++)
  159. {
  160. scanf("%d%d",&u,&v);
  161. link(u,v);
  162. link(v,u);
  163. }
  164. dfs(,,);
  165. dfs2(,);
  166. build(,n,);
  167. while (q--)
  168. {
  169. scanf("%s",s);
  170. if (s[]=='I')
  171. {
  172. scanf("%d%d%d",&al,&ar,&ask);
  173. work(al,ar,ask);
  174. }
  175. else if (s[]=='D')
  176. {
  177. scanf("%d%d%d",&al,&ar,&ask);
  178. work(al,ar,-ask);
  179. }
  180. else
  181. {
  182. scanf("%d",&ask);
  183. printf("%d\n",query(,n,,pos[ask]));
  184. }
  185. }
  186. }
  187. return ;
  188. }

【hdu3966】Aragorn's Story的更多相关文章

  1. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  2. 【原】谈谈对Objective-C中代理模式的误解

    [原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...

  3. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  4. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  5. 【调侃】IOC前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  6. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  7. Python高手之路【一】初识python

    Python简介 1:Python的创始人 Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种解释型.面向对象.动态数据类型的高级程序设计语言,由荷兰人Guido ...

  8. 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】

    说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...

  9. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

随机推荐

  1. H5实现调用本地摄像头实现实时视频以及拍照功能

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 21反射、动态代理、枚举、Filter

    2018/10/08 1.反射 Constructor Class类的newInstance()方法是使用该类无参的构造函数创建对象, 如果一个类没有无参的构造函数, 就不能这样创建了,可以调用Cla ...

  3. 【转】C语言中access函数

    头文件:unistd.h 功 能: 确定文件或文件夹的访问权限.即,检查某个文件的存取方式,比如说是只读方式.只写方式等.如果指定的存取方式有效,则函数返回0,否则函数返回-1. 用 法: int a ...

  4. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...

  5. Volume 1. String(uva)

    10361 - Automatic Poetry #include <iostream> #include <string> #include <cstdio> # ...

  6. virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenvwrapper.........(解决办法)

    Linux(ubuntu)上python2与python3共存环境下,安装virtualenvwrapper后, 其环境变量被自动设置为VIRTUALENVWRAPPER_PYTHON=/usr/bi ...

  7. add list of symbols -- latex

    * add list of symbols -- latexinclude a *toc.tex* file in the *main.tex* in *main.tex*#+BEGIN_SRC la ...

  8. 使用js生成条形码以及二维码

    一.用js生成条形码这种业务场景不是很常见的,最近刚好又接到这种需求 Google一下,发现github还真有这方面的轮子,感谢github,省去了我们很多造轮子的过程, 好了言归正传,首先引入jsb ...

  9. Qt 编写应用支持多语言版本--一个GUI应用示例

    简介 上一篇博文已经说过如何编写支持多语言的Qt 命令行应用,这一篇说说Qt GUI 应用多语言支持的坑. 本人喜欢用代码来写布局,而不是用 Qt Designer 来设计布局,手写布局比 Qt De ...

  10. 【Codeforces 1041D】Glider

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分. 枚举每一个上升区的起始位置作为起点(这样做肯定是最优的),然后如果没有掉在地上的话就尽量往右二分(只有上升区之间的间隙会让他往下掉) ...