一、前言

  这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了。。于是,还是低级错误背锅啊。。。。代码能力有待进一步提升2333333

二、题意

  某家庭主妇住在一棵树上,他的若干个孩子在树的若干个节点上有分布,主妇同学需要从某给定节点出发,飞到树上的制定节点,在过程中,边权可能会发生改变,问从当前节点到指定节点的边权和。

三、解法

  树链拋分,点更新查区间。

  1. // #include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<math.h>
  4. #include<string.h>
  5. #include<stdio.h>
  6. #include<algorithm>
  7. #include<vector>
  8. using namespace std;
  9.  
  10. #define ll long long
  11. #define pp pair<int ,int>
  12.  
  13. const ll MAXN=1e5+;
  14.  
  15. class Node
  16. {
  17. public:
  18. int next,to,cost;
  19. void init(int a,int b,int c)
  20. {
  21. this->next=a;
  22. this->to=b;
  23. this->cost=c;
  24. }
  25. };
  26. Node G[MAXN*];
  27. int tree[MAXN],child[MAXN],deep[MAXN],number[MAXN],top[MAXN],points[MAXN],father[MAXN];
  28. int size,summ,n,q,s;
  29. void insert(int pos,int key)
  30. {
  31. while(pos<MAXN)
  32. {
  33. tree[pos]+=key;
  34. pos+=pos&(-pos);
  35. }
  36. }
  37. int getSum(int pos)
  38. {
  39. int ans=;
  40. while(pos)
  41. {
  42. ans+=tree[pos];
  43. pos-=pos&(-pos);
  44. }return ans;
  45. }
  46. void add(int from,int to,int cost)
  47. {
  48. G[summ].init(points[from],to,cost);
  49. points[from]=summ++;
  50. }
  51. void dfs_1(int now,int last,int dep)
  52. {
  53. deep[now]=dep;
  54. father[now]=last;
  55. child[now]=;
  56. for(int i=points[now];i!=-;i=G[i].next)
  57. {
  58. int tar=G[i].to;
  59. int cost=G[i].cost;
  60. if(tar==last)continue;
  61. dfs_1(tar,now,dep+);
  62. child[now]+=child[tar];
  63. }
  64. }
  65. void dfs(int now,int last,int first,int cc=)
  66. {
  67. top[now] = first ? first : now;
  68. insert(size,cc);
  69. // cout<<"check_number: "<<now<<ends<<size<<endl;
  70. number[now]=size++;
  71. int maxx,pos,coS;
  72. maxx=pos=-;
  73. for(int i=points[now];i!=-;i=G[i].next)
  74. {
  75. int tar=G[i].to;
  76. int cost=G[i].cost;
  77. if(tar==last)continue;
  78. if(maxx<child[tar])
  79. {
  80. maxx=child[tar];
  81. pos=tar;
  82. coS=cost;
  83. }
  84. }if(pos!=-)dfs(pos,now,top[now],coS);
  85. for(int i=points[now];i!=-;i=G[i].next)
  86. {
  87. int tar=G[i].to;
  88. int cost=G[i].cost;
  89. if(tar==last||tar==pos)continue;
  90.  
  91. dfs(tar,now,,cost);
  92. }
  93. }
  94. void update(int num,int key)
  95. {
  96. int a=G[num*].to;
  97. int b=G[num*+].to;
  98. if(father[a]==b)
  99. {
  100. key=key-(getSum(number[a])-getSum(number[a]-));
  101. insert(number[a],key);
  102. }else
  103. {
  104. key=key-(getSum(number[b])-getSum(number[b]-));
  105. insert(number[b],key);
  106. }
  107. }
  108. int query(int from,int to)
  109. {
  110. int ans=;
  111. int t1=top[from];
  112. int t2=top[to];
  113. // cout<<"check_top: "<<t1<<ends<<t2<<endl;
  114. // cout<<"check_point: "<<from<<ends<<to<<endl;
  115. while(t1!=t2)
  116. {
  117. if(deep[t1]<deep[t2])
  118. {
  119. swap(t1,t2);
  120. swap(from,to);
  121. }
  122. ans+=getSum(number[from])-getSum(number[t1]-);
  123. from=father[t1];
  124. t1=top[from];
  125. }
  126. int star=min(number[from],number[to]);
  127. int endd=max(number[from],number[to]);
  128. // cout<<ans<<"check_Number: "<<star<<ends<<endd<<endl;
  129. // cout<<"checkSum: "<<getSum(endd)<<ends<<getSum(star)<<endl;
  130. ans+=getSum(endd)-getSum(star);
  131. return ans;
  132. }
  133. void init()
  134. {
  135. size=;summ=;
  136. memset(points,-,sizeof(points));
  137. for(int i=;i<n;++i)
  138. {
  139. int a,b,c;
  140. scanf("%d%d%d",&a,&b,&c);
  141. // cin>>a>>b>>c;
  142. add(a,b,c);
  143. add(b,a,c);
  144. }dfs_1(,,);
  145. dfs(,,);
  146. for(int i=;i<q;++i)
  147. {
  148. int a,b,c;
  149. // cin>>c;
  150. scanf("%d",&c);
  151. if(c)
  152. {
  153. // cin>>a>>b;
  154. scanf("%d%d",&a,&b);
  155. a--;
  156. update(a,b);
  157. }else{
  158. // cin>>a;
  159. scanf("%d",&a);
  160. cout<<query(s,a)<<"\n";
  161. s=a;
  162. }
  163. }
  164. }
  165.  
  166. int main()
  167. {
  168. // cin.sync_with_stdio(false);
  169. cin>>n>>q>>s;init();
  170.  
  171. return ;
  172. }

POJ 2763 Housewife Wind 树链拋分的更多相关文章

  1. poj 2763 Housewife Wind(树链拆分)

    id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...

  2. POJ 2763 Housewife Wind (树链剖分 有修改单边权)

    题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...

  3. POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)

    题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...

  4. poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...

  5. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  6. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

  7. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

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

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

  9. POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10378   Accepted: 2886 D ...

随机推荐

  1. electron 开发记录

    判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...

  2. ashx是什么文件

    ashx是什么文件 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程 ...

  3. ace admin 左侧菜单定位

    后台模版来自:Ace Admin http://ace.jeka.by/form-elements.html 左侧菜单,通过js根据url来判断显示哪块 window.location.pathnam ...

  4. 阿里云部署安装redis无法访问

    ps:我在linux上安装redis后发现一直端口不通连接不上,折腾一晚上.后来在阿里云官方回复(机器人)中看到下面的回复:       2019/02/28 22:50 自己一试,过完是端口监听是本 ...

  5. python中函数的定义与调用

    1.为什么要用函数? (1)代码重复太多(2)可读性差 使用函数的好处: (1)代码重用 (2)保持一致性,易维护 (2)可扩展性 2.初始函数定义与调用     函数的定义 def test(x): ...

  6. form表单上传域(type="file")的使用----上传文件

    一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contentType="text/html; charset=UT ...

  7. FMCW 雷达原理(转)

    FMCW(Frequency Modulated Continuous Wave),即调频连续波.FMCW技术和脉冲雷达技术是两种在高精度雷达测距中使用的技术.其基本原理为,发射波为高频连续波,其频率 ...

  8. Ubuntu 安装boost 库

    使用 apt-get进行安装 sudo apt-get install libboost-dev

  9. 在window下, Java调用执行bat脚本

    参考博客: https://www.cnblogs.com/jing1617/p/6430141.html 最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下: 我这里是先 ...

  10. Android商城开发系列(十)—— 首页活动广告布局实现

    在上一篇博客当中,我们讲了频道布局的实现,接下来我们讲解一下活动广告布局的实现,效果如下图: 这个是用viewpager去实现的,新建一个act_item.xml,代码如下所示: <?xml v ...