经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
Housewife Wind(边权树链剖分)
来源:cnblogs  作者:Fighting_sh  时间:2018/9/25 19:12:40  对本文有异议

Housewife Wind

http://poj.org/problem?id=2763

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 14820   Accepted: 4097

Description

After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique. 

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!' 

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road. 

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her? 

Input

The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001. 

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000. 

The following q lines each is one of the following two types: 

Message A: 0 u 
A kid in hut u calls Wind. She should go to hut u from her current position. 
Message B: 1 i w 
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid. 

Output

For each message A, print an integer X, the time required to take the next child.

Sample Input

  1. 3 3 1
  2. 1 2 1
  3. 2 3 2
  4. 0 2
  5. 1 2 3
  6. 0 3

Sample Output

  1. 1
  2. 3

Source

 
边权树链剖分模板题
比较一条边上哪个点的深度大,就把权值加在哪个点上
  1. 1 #include<iostream>
  2. 2 #include<cstring>
  3. 3 #include<string>
  4. 4 #include<cmath>
  5. 5 #include<cstdio>
  6. 6 #include<algorithm>
  7. 7 #include<vector>
  8. 8 #define maxn 200005
  9. 9 #define MAXN 200005
  10. 10 #define lson l,mid,rt<<1
  11. 11 #define rson mid+1,r,rt<<1|1
  12. 12 using namespace std;
  13. 13
  14. 14 long long tree[maxn<<3];
  15. 15 int n;
  16. 16 int v[maxn],val[maxn];
  17. 17 int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt;
  18. 18 int co,head[MAXN];
  19. 19 struct Edge {
  20. 20 int to, next;
  21. 21 }edge[MAXN];
  22. 22 struct E {
  23. 23 int u, v, c;
  24. 24 }e[MAXN];
  25. 25 void addedge(int u, int v) {
  26. 26 edge[cnt].to = v;
  27. 27 edge[cnt].next = head[u];
  28. 28 head[u] = cnt++;
  29. 29 }
  30. 30 struct sair{
  31. 31 int x,y,len;
  32. 32 }p[maxn];
  33. 33
  34. 34 void pushup(int rt){
  35. 35 tree[rt]=(tree[rt<<1]+tree[rt<<1|1]);
  36. 36 }
  37. 37
  38. 38 void build(int l,int r,int rt){
  39. 39 if(l==r){
  40. 40 tree[rt]=0;
  41. 41 return;
  42. 42 }
  43. 43 int mid=(l+r)/2;
  44. 44 build(lson);
  45. 45 build(rson);
  46. 46 pushup(rt);
  47. 47 }
  48. 48
  49. 49 void add(int L,int R,int k,int l,int r,int rt){
  50. 50 if(L<=l&&R>=r){
  51. 51 tree[rt]=k;
  52. 52 return;
  53. 53 }
  54. 54 int mid=(l+r)/2;
  55. 55 if(L<=mid) add(L,R,k,lson);
  56. 56 if(R>mid) add(L,R,k,rson);
  57. 57 pushup(rt);
  58. 58 }
  59. 59
  60. 60 long long query(int L,int R,int l,int r,int rt){
  61. 61 if(L<=l&&R>=r){
  62. 62 return tree[rt];
  63. 63 }
  64. 64 int mid=(l+r)/2;
  65. 65 long long ans=0;
  66. 66 if(L<=mid) ans+=query(L,R,lson);
  67. 67 if(R>mid) ans+=query(L,R,rson);
  68. 68 pushup(rt);
  69. 69 return ans;
  70. 70 }
  71. 71
  72. 72 void dfs1(int now,int f,int deep){
  73. 73 dep[now]=deep;
  74. 74 siz[now]=1;
  75. 75 fa[now]=f;
  76. 76 int maxson=-1;
  77. 77 for(int i=head[now];~i;i=edge[i].next){
  78. 78 if(edge[i].to != fa[now]) {
  79. 79 dfs1(edge[i].to,now,deep+1);
  80. 80 siz[now]+=siz[edge[i].to];
  81. 81 if(siz[edge[i].to]>maxson){
  82. 82 maxson=siz[edge[i].to];
  83. 83 son[now]=edge[i].to;
  84. 84 }
  85. 85 }
  86. 86 }
  87. 87 }
  88. 88
  89. 89 void dfs2(int now,int topp){
  90. 90 id[now]=++cnt;
  91. 91 val[cnt]=v[now];
  92. 92 top[now]=topp;
  93. 93 if(!son[now]) return;
  94. 94 dfs2(son[now],topp);
  95. 95 for(int i=head[now];~i;i=edge[i].next){
  96. 96 int vvv = edge[i].to;
  97. 97 if(vvv==son[now]||vvv==fa[now]) continue;
  98. 98 dfs2(vvv,vvv);
  99. 99 }
  100. 100 }
  101. 101
  102. 102 long long qRange(int x,int y){
  103. 103 int t1 = top[x], t2 = top[y];
  104. 104 long long res = 0;
  105. 105 while(t1 != t2) {
  106. 106 if(dep[t1] < dep[t2]) {
  107. 107 swap(t1, t2); swap(x, y);
  108. 108 }
  109. 109 res += query(id[t1], id[x], 1, n, 1);
  110. 110 x = fa[t1]; t1 = top[x];
  111. 111 }
  112. 112 if(x == y) return res;
  113. 113 if(dep[x] > dep[y]) swap(x, y);
  114. 114 return res + query(id[son[x]], id[y], 1, n, 1);
  115. 115 }
  116. 116
  117. 117 void addRange(int x,int y,int k){
  118. 118 while(top[x]!=top[y]){
  119. 119 if(dep[top[x]]<dep[top[y]]) swap(x,y);
  120. 120 add(id[top[x]],id[x],k,1,n,1);
  121. 121 x=fa[top[x]];
  122. 122 }
  123. 123 if(dep[x]>dep[y]) swap(x,y);
  124. 124 add(id[x],id[y],k,1,n,1);
  125. 125 }
  126. 126
  127. 127 int main(){
  128. 128 int m,r;
  129. 129 scanf("%d %d %d",&n,&m,&r);
  130. 130 memset(head, -1, sizeof head);
  131. 131 int pos,z,x,y;
  132. 132 co=0;
  133. 133 for(int i=1;i<n;i++){
  134. 134 scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].len);
  135. 135 addedge(p[i].x,p[i].y);
  136. 136 addedge(p[i].y,p[i].x);
  137. 137 }
  138. 138 cnt=0;
  139. 139 int xx;
  140. 140 dfs1(1,0,1);
  141. 141 dfs2(1,1);
  142. 142 build(1,n,1);
  143. 143 for(int i=1;i<n;i++){
  144. 144 if(dep[p[i].x]<dep[p[i].y]) xx=p[i].y;
  145. 145 else xx=p[i].x;
  146. 146 addRange(xx,xx,p[i].len);
  147. 147 }
  148. 148 for(int i=1;i<=m;i++){
  149. 149 scanf("%d %d",&pos,&x);
  150. 150 if(!pos){
  151. 151 printf("%lld\n",qRange(x,r));
  152. 152 r=x;
  153. 153 }
  154. 154 else if(pos){
  155. 155 scanf("%d",&y);
  156. 156 p[x].len=y;
  157. 157 if(dep[p[x].x]<dep[p[x].y]) xx=p[x].y;
  158. 158 else xx=p[x].x;
  159. 159 addRange(xx,xx,p[x].len);
  160. 160 }
  161. 161 }
  162. 162
  163. 163 }
View Code

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号