Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10510    Accepted Submission(s):
2766

Problem Description
Our protagonist is the handsome human prince Aragorn
comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who
want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his
kingdom and M edges connect them. It is guaranteed that for any two camps, there
is one and only one path connect them. At first Aragorn know the number of
enemies in every camp. But the enemy is cunning , they will increase or decrease
the number of soldiers in camps. Every time the enemy change the number of
soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on
the path from C1 to C2, they will increase or decrease K soldiers to these
camps. Now Aragorn wants to know the number of soldiers in some particular camps
real-time.
 
Input
Multiple test cases, process to the end of
input.

For each case, The first line contains three integers N, M, P
which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤
100000) operations. The number of camps starts from 1.

The next line
contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has
Ai enemies.

The next M lines contains two integers u and v for each,
denotes that there is an edge connects camp-u and camp-v.

The next P
lines will start with a capital letter 'I', 'D' or 'Q' for each
line.

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which
means for camp C1, C2 and all camps on the path from C1 to C2, increase K
soldiers to these camps.

'D', followed by three integers C1, C2 and K(
0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2,
decrease K soldiers to these camps.

'Q', followed by one integer C, which
is a query and means Aragorn wants to know the number of enemies in camp C at
that time.

 
Output
For each query, you need to output the actually number
of enemies in the specified camp.
 
Sample Input
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
 
Sample Output
7
4
8

Hint

1.The number of enemies may be negative.

2.Huge input, be careful.

 
Source
 
Recommend
We have carefully selected several similar problems for
you:  3964 3965 3962 3963 3967 
 
思路:
  裸树剖;
 
来,上代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. #define maxn 50001
  7.  
  8. using namespace std;
  9.  
  10. struct TreeNodeType {
  11. int l,r,dis,mid,flag;
  12.  
  13. void clear()
  14. {
  15. l=,r=,dis=,mid=,flag=;
  16. }
  17. };
  18. struct TreeNodeType tree[maxn<<];
  19.  
  20. struct EdgeType {
  21. int to,next;
  22. };
  23. struct EdgeType edge[maxn<<];
  24.  
  25. int if_z,n,m,q,cnt,tot,Enum,deep[maxn],size[maxn],belong[maxn];
  26. int flag[maxn],head[maxn],dis[maxn],dis_[maxn],f[maxn];
  27.  
  28. char Cget;
  29.  
  30. inline void read_int(int &now)
  31. {
  32. now=,if_z=,Cget=getchar();
  33. while(Cget<''||Cget>'')
  34. {
  35. if(Cget=='-') if_z=-;
  36. Cget=getchar();
  37. }
  38. while(Cget>=''&&Cget<='')
  39. {
  40. now=now*+Cget-'';
  41. Cget=getchar();
  42. }
  43. now*=if_z;
  44. }
  45.  
  46. inline void edge_add(int from,int to)
  47. {
  48. edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum;
  49. edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum;
  50. }
  51.  
  52. void search(int now,int fa)
  53. {
  54. int pos=tot++;
  55. deep[now]=deep[fa]+,f[now]=fa;
  56. for(int i=head[now];i;i=edge[i].next)
  57. {
  58. if(edge[i].to==fa) continue;
  59. search(edge[i].to,now);
  60. }
  61. size[now]=tot-pos;
  62. }
  63.  
  64. void search_(int now,int chain)
  65. {
  66. int pos=;
  67. flag[now]=++tot,dis_[flag[now]]=dis[now];
  68. belong[now]=chain;
  69. for(int i=head[now];i;i=edge[i].next)
  70. {
  71. if(flag[edge[i].to]) continue;
  72. if(size[edge[i].to]>size[pos]) pos=edge[i].to;
  73. }
  74. if(pos!=) search_(pos,chain);
  75. else return ;
  76. for(int i=head[now];i;i=edge[i].next)
  77. {
  78. if(flag[edge[i].to]) continue;
  79. search_(edge[i].to,edge[i].to);
  80. }
  81. }
  82.  
  83. inline void tree_up(int now)
  84. {
  85. tree[now].dis=tree[now<<].dis+tree[now<<|].dis;
  86. }
  87.  
  88. inline void tree_down(int now)
  89. {
  90. if(tree[now].l==tree[now].r) return ;
  91. tree[now<<].dis+=(tree[now<<].r-tree[now<<].l+)*tree[now].flag;
  92. tree[now<<].flag+=tree[now].flag;
  93. tree[now<<|].dis+=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag;
  94. tree[now<<|].flag+=tree[now].flag;
  95. tree[now].flag=;
  96. }
  97.  
  98. void tree_build(int now,int l,int r)
  99. {
  100. tree[now].clear();
  101. tree[now].l=l,tree[now].r=r;
  102. if(l==r)
  103. {
  104. tree[now].dis=dis_[++tot];
  105. return ;
  106. }
  107. tree[now].mid=(l+r)>>;
  108. tree_build(now<<,l,tree[now].mid);
  109. tree_build(now<<|,tree[now].mid+,r);
  110. tree_up(now);
  111. }
  112.  
  113. void tree_change(int now,int l,int r,int x)
  114. {
  115. if(tree[now].l==l&&tree[now].r==r)
  116. {
  117. tree[now].dis+=(r-l+)*x;
  118. tree[now].flag+=x;
  119. return ;
  120. }
  121. if(tree[now].flag) tree_down(now);
  122. if(l>tree[now].mid) tree_change(now<<|,l,r,x);
  123. else if(r<=tree[now].mid) tree_change(now<<,l,r,x);
  124. else
  125. {
  126. tree_change(now<<,l,tree[now].mid,x);
  127. tree_change(now<<|,tree[now].mid+,r,x);
  128. }
  129. tree_up(now);
  130. }
  131.  
  132. int tree_query(int now,int to)
  133. {
  134. if(tree[now].l==tree[now].r&&tree[now].l==to)
  135. {
  136. return tree[now].dis;
  137. }
  138. if(tree[now].flag) tree_down(now);
  139. tree_up(now);
  140. if(to>tree[now].mid) return tree_query(now<<|,to);
  141. else return tree_query(now<<,to);
  142. }
  143.  
  144. inline void solve_change(int x,int y,int z)
  145. {
  146. while(belong[x]!=belong[y])
  147. {
  148. if(deep[belong[x]]<deep[belong[y]]) swap(x,y);
  149. tree_change(,flag[belong[x]],flag[x],z);
  150. x=f[belong[x]];
  151. }
  152. if(deep[x]<deep[y]) swap(x,y);
  153. tree_change(,flag[y],flag[x],z);
  154. }
  155.  
  156. int main()
  157. {
  158. while(scanf("%d%d%d",&n,&m,&q)==)
  159. {
  160. memset(head,,sizeof(head));
  161. memset(size,,sizeof(size));
  162. memset(flag,,sizeof(flag));
  163. tot=,cnt=,Enum=;
  164. for(int i=;i<=n;i++) read_int(dis[i]);
  165. int from,to,cur;
  166. for(int i=;i<=m;i++)
  167. {
  168. read_int(from),read_int(to);
  169. edge_add(from,to);
  170. }
  171. search(,),tot=,search_(,);
  172. tot=,tree_build(,,n);
  173. char type;
  174. for(int i=;i<=q;i++)
  175. {
  176. cin>>type;
  177. if(type=='I')
  178. {
  179. read_int(from),read_int(to),read_int(cur);
  180. solve_change(from,to,cur);
  181. }
  182. if(type=='D')
  183. {
  184. read_int(from),read_int(to),read_int(cur);
  185. solve_change(from,to,-cur);
  186. }
  187. if(type=='Q')
  188. {
  189. read_int(from);
  190. printf("%d\n",tree_query(,flag[from]));
  191. }
  192. }
  193. }
  194. return ;
  195. }

AC日记——Aragorn's Story HDU 3966的更多相关文章

  1. Aragorn's Story HDU - 3966 -树剖模板

    HDU - 3966 思路 :树链剖分就是可以把一个路径上的点映射成几段连续的区间上.这样对于连续的区间可以用线段树维护, 对于每一段连续的区间都可以通过top [ ]数组很快的找到这段连续区间的头. ...

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

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

  3. AC日记——Dylans loves tree hdu 5274

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

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

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

  5. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  6. HDU - 3966 Aragorn's Story(树链剖分入门+线段树)

    HDU - 3966 Aragorn's Story Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  7. HDU 3966 Aragorn's Story 动态树 树链剖分

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

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

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

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

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

随机推荐

  1. rest_framework之status HTTP状态码

    Django Rest Framework有一个status.py的文件 通常在我们Django视图(views)中,HTTP状态码使用的是纯数字,像400,404,200,304等,并不是那么很好理 ...

  2. Vim编辑器基础

    Vim编辑器基础 vi:Visual Interface vim:VI iMproved Vim模式 1.编辑模式(命令模式) 只能下达命令,不能键入字符 2.输入模式 键入字符 3.末行模式 左下角 ...

  3. TB平台搭建之一

    最近在搭建公司的testbench,主要有一下总结: 1.TB主要有两部分:部分一,软件部分主要用C写的,她的作用是写硬件的驱动(其实就是让核的外围设备可以正常工作或工作到特定的环境上)甚至有可能写整 ...

  4. 中国电信物联网平台入门学习笔记2: DOME程序分析

    "墨子号NB-IOT开发板"提供的dome: 程序只要分为延时,定时器,串口通讯…… 工程文件在:…\STM32L1xx_StdPeriph_Lib_V1.3.1\Project\ ...

  5. HDU 5510 Bazinga KMP

    题意: 给\(n(1 \leq n \leq 500)\)个字符串,求一个最大的\(i\),使得存在一个\(S_{j}\)不是\(S_i\)的子串. 分析: 维护两个指针\(l,r\) 那么有两种情况 ...

  6. loj2275 「JXOI2017」颜色

    枚举右端点,然后看左端点合法情况. 先预处理每个颜色 \(i\) 的最大出现位置 \(max_i\) 和最小出现位置 \(min_i\).对于枚举右端点在一个位置 \(i\),凡是 \(max_k & ...

  7. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  8. 【SDOJ 3741】 【poj2528】 Mayor's posters

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  9. [python学习篇] uiautomator xiaocong

    Skip to content     This repository Pull requests Issues Marketplace Gist   Sign out       Watch103 ...

  10. poj 1523 SPF 求割点以及删除该割点后联通块的数量

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7136   Accepted: 3255 Description C ...