Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966

这题注意要手动扩栈。

这题我交g++无限RE,即使手动扩栈了,但交C++就过了。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9. using namespace std;
  10. const int maxn = 5e4+;
  11. #ifdef WIN32
  12. #define LL __int64
  13. #else
  14. #define LL long long
  15. #endif
  16.  
  17. int A[maxn],totw;
  18. struct node
  19. {
  20. int fa,son,top,w,size,deep;
  21. }tree[maxn];
  22. vector<int> vt[maxn];
  23. int tr[maxn];
  24.  
  25. void dfs_1(int p,int last,int de)
  26. {
  27. tree[p].son = -;
  28. tree[p].fa = last;
  29. tree[p].deep = de;
  30. tree[p].size = ;
  31. int num = vt[p].size();
  32. for(int i = ;i < num;++i)
  33. if(vt[p][i] != last)
  34. {
  35. dfs_1(vt[p][i],p,de+);
  36. tree[p].size += tree[vt[p][i]].size;
  37. if(- == tree[p].son || tree[vt[p][i]].size > tree[tree[p].son].size)
  38. tree[p].son = vt[p][i];
  39. }
  40. }
  41. void dfs_2(int p,int top)
  42. {
  43. tree[p].w = ++totw;
  44. tree[p].top = top;
  45. if(tree[p].son != -)
  46. dfs_2(tree[p].son,top);
  47. else return ;
  48. int num = vt[p].size();
  49. for(int i = ;i < num;++i)
  50. {
  51. if(vt[p][i] != tree[p].fa && vt[p][i] != tree[p].son)
  52. dfs_2(vt[p][i],vt[p][i]);
  53. }
  54. }
  55.  
  56. void sol()
  57. {
  58. totw = ;
  59. memset(tr,,sizeof(tr));
  60. memset(tree,,sizeof(tree));
  61. dfs_1(,,);
  62. dfs_2(,);
  63. }
  64. int lowbit(int x)
  65. {
  66. return x & (-x);
  67. }
  68. int query(int p)
  69. {
  70. p = tree[p].w;
  71. int tot = ;
  72. while(p >= )
  73. {
  74. tot += tr[p];
  75. p -= lowbit(p);
  76. }
  77. return tot;
  78. }
  79. void add(int n,int p,int d)
  80. {
  81. while(p <= n)
  82. {
  83. tr[p] += d;
  84. p += lowbit(p);
  85. }
  86. }
  87. void update_sec(int n,int u,int v,int d)
  88. {
  89. if(tree[u].w > tree[v].w) swap(u,v);
  90. add(n,tree[u].w,d);
  91. add(n,tree[v].w+,-d);
  92. }
  93. void update(int n,int u,int v,int d)
  94. {
  95. /*
  96. u,v不断靠近根结点
  97. */
  98. while(tree[u].top != tree[v].top)
  99. {
  100. if(tree[tree[u].top].deep < tree[tree[v].top].deep) swap(u,v);
  101. update_sec(n,tree[u].top,u,d);
  102. u = tree[tree[u].top].fa;
  103. }
  104. update_sec(n,u,v,d);
  105. }
  106.  
  107. int main()
  108. {
  109. // freopen("in.txt","r",stdin);
  110. int n,m,p;
  111. while(scanf("%d%d%d",&n,&m,&p)!=EOF)
  112. {
  113. for(int i = ;i <= n;++i)
  114. scanf("%d",&A[i]);
  115. for(int i = ;i <= n;++i)
  116. vt[i].clear();
  117. int x,y;
  118. for(int i = ;i < m;++i)
  119. {
  120. scanf("%d%d",&x,&y);
  121. vt[x].push_back(y);
  122. vt[y].push_back(x);
  123. }
  124. sol();
  125. char oper[];
  126. while(p--)
  127. {
  128. scanf("%s",oper);
  129. int x,y,z;
  130. if('Q' == oper[])
  131. {
  132. scanf("%d",&x);
  133. printf("%d\n",A[x] + query(x));
  134. }
  135. else if('I' == oper[])
  136. {
  137. scanf("%d%d%d",&x,&y,&z);
  138. update(n,x,y,z);
  139. }
  140. else if('D' == oper[])
  141. {
  142. scanf("%d%d%d",&x,&y,&z);
  143. update(n,x,y,-z);
  144. }
  145. }
  146. }
  147. return ;
  148. }

HDU 3966 Aragorn's Story 树链剖分的更多相关文章

  1. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  2. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  3. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

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

  4. HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...

  5. hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...

  6. hdu 3966 Aragorn's Story 树链剖分 按点

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

  7. HDU 3966 Aragorn's Story (树链剖分入门题)

    树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...

  8. HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...

  9. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

随机推荐

  1. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  2. PHP代码重用与函数编写

    代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法.require()和include()几 ...

  3. 2016中国·西安“华山杯”WriteUp- SeeSea

    题目打包下载:https://yunpan.cn/ckyrKxHJDPAIN (提取码:bbaf) Web 1.签到(10) 扫码回复 hs_ctf拿flag, 套路题. flag_Xd{hSh_ct ...

  4. AFNetWorking3.0源码分析

    分析: AFNetWorking(3.0)源码分析(一)——基本框架 AFNetworking源码解析 AFNetworking2.0源码解析<一> end

  5. 基于xml的Spring多数据源配置和使用

    上一篇讲了<基于注解的Spring多数据源配置和使用>,通过在类或者方法上添加@DataSource注解就可以指定某个数据源.这种方式的优点是控制粒度细,也更灵活. 但是当有些时候项目分模 ...

  6. 一个五年 Android 开发者百度、阿里、聚美、映客的面试心经

    花絮 也许会有人感叹某些人的运气比较好,但是他们不曾知道对方吃过多少苦,受过多少委屈.某些时候就是需要我们用心去发现突破点,然后顺势而上,抓住机遇,那么你将会走向另外一条大道,成就另外一个全新的自我. ...

  7. ios9适配系列教程——ios9新变化

    Demo1_iOS9网络适配_改用更安全的HTTPS iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一使用TLS 1.2 SSL.采用TLS 1.2 协议,目的是 强制 ...

  8. C#软件设计——小话设计模式原则之:单一职责原则SRP

    前言:上篇C#软件设计——小话设计模式原则之:依赖倒置原则DIP简单介绍了下依赖倒置的由来以及使用,中间插了两篇WebApi的文章,这篇还是回归正题,继续来写写设计模式另一个重要的原则:单一职责原则. ...

  9. 解决win8 下 eclipse 中文字体太小的问题

    一.把字体设置为Courier New  操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“Genneral”——点击“Appearance”— ...

  10. Nhibernate Query By Criteria 条件查询

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not equal > Restrictio ...