这两题都是link cut tree的裸题
之前看Qtree的论文,只会在确定父子关系的情况下连边和删边
如果在任意两个点连边删边怎么做呢?
这时候我们不能随意的将一个点的父节点设为另一个点,因为其中某个点的父节点可能已经被设为另外某点了
其实很简单,连边的时候,我们只要把x变成其所在原树的根,这样x是没有父节点了
然后把x的父节点设为y即可,删边、询问路径的道理类似,具体见程序
给出的是bzoj2631的程序

  1. const mo=;
  2.  
  3. var fa,q,a,mul,add,size,sum:array[..] of longint;
  4. rev:array[..] of boolean;
  5. son:array[..,..] of longint;
  6. i,x0,y0,x,y,z,n,m,t:longint;
  7. ch:char;
  8.  
  9. function root(x:longint):boolean; //判断是Auxiliary tree(splay)上的父节点还是path parent
  10. begin
  11. exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
  12. end;
  13.  
  14. procedure swap(var a,b:longint);
  15. var c:longint;
  16. begin
  17. c:=a;
  18. a:=b;
  19. b:=c;
  20. end;
  21.  
  22. procedure update(x:longint);
  23. var l,r:longint;
  24. begin
  25. l:=son[x,];
  26. r:=son[x,];
  27. size[x]:=(size[l]+size[r]+) mod mo;
  28. sum[x]:=(a[x]+sum[l]+sum[r]) mod mo;
  29. end;
  30.  
  31. procedure calc(x,c,j:longint);
  32. begin
  33. if (c<=) then //常数优化,少用int64
  34. begin
  35. mul[x]:=mul[x]*c mod mo;
  36. add[x]:=(add[x]*c+j) mod mo;
  37. sum[x]:=(sum[x]*c+int64(j)*int64(size[x])) mod mo;
  38. a[x]:=(a[x]*c+j) mod mo;
  39. end
  40. else begin
  41. mul[x]:=int64(mul[x])*int64(c) mod mo;
  42. add[x]:=(int64(add[x])*int64(c)+j) mod mo;
  43. sum[x]:=(int64(sum[x])*int64(c)+int64(j)*int64(size[x])) mod mo;
  44. a[x]:=(int64(a[x])*int64(c)+j) mod mo;
  45. end;
  46. end;
  47.  
  48. procedure push(x:longint);
  49. begin
  50. if rev[x] then
  51. begin
  52. rev[son[x,]]:=not rev[son[x,]];
  53. rev[son[x,]]:=not rev[son[x,]];
  54. swap(son[x,],son[x,]);
  55. rev[x]:=false;
  56. end;
  57. if (mul[x]<>) or (add[x]<>) then
  58. begin
  59. if son[x,]<> then calc(son[x,],mul[x],add[x]);
  60. if son[x,]<> then calc(son[x,],mul[x],add[x]);
  61. mul[x]:=;
  62. add[x]:=;
  63. end;
  64. end;
  65.  
  66. procedure rotate(x,w:longint);
  67. var y:longint;
  68. begin
  69. y:=fa[x];
  70. if not root(y) then
  71. begin
  72. if son[fa[y],]=y then son[fa[y],]:=x
  73. else son[fa[y],]:=x;
  74. end;
  75. fa[x]:=fa[y];
  76. son[y,-w]:=son[x,w];
  77. if son[x,w]<> then fa[son[x,w]]:=y;
  78. son[x,w]:=y;
  79. fa[y]:=x;
  80. update(y);
  81. end;
  82.  
  83. procedure splay(x:longint);
  84. var y,t,i:longint;
  85. ff:boolean;
  86. begin
  87. t:=;
  88. i:=x;
  89. while not root(i) do
  90. begin
  91. inc(t);
  92. q[t]:=i;
  93. i:=fa[i];
  94. end;
  95. inc(t);
  96. q[t]:=i;
  97. for i:=t downto do
  98. push(q[i]);
  99. ff:=true;
  100. if t= then exit;
  101. while ff do
  102. begin
  103. y:=fa[x];
  104. if y=q[t] then
  105. begin
  106. if son[y,]=x then rotate(x,)
  107. else rotate(x,);
  108. ff:=false;
  109. end
  110. else begin
  111. if fa[y]=q[t] then ff:=false;
  112. if son[fa[y],]=y then
  113. begin
  114. if son[y,]=x then rotate(y,)
  115. else rotate(x,);
  116. rotate(x,);
  117. end
  118. else begin
  119. if son[y,]=x then rotate(x,)
  120. else rotate(y,);
  121. rotate(x,);
  122. end;
  123. end;
  124. end;
  125. update(x);
  126. end;
  127.  
  128. procedure access(x:longint);
  129. var y:longint;
  130. begin
  131. y:=;
  132. repeat
  133. splay(x);
  134. son[x,]:=y;
  135. update(x); //这里要记得维护
  136. y:=x;
  137. x:=fa[x];
  138. until x=;
  139. end;
  140.  
  141. procedure makeroot(x:longint);
  142. begin
  143. access(x); //执行access之后,x和当前树的根之间的路径构成Auxiliary tree
  144. splay(x); //将x旋到Auxiliary tree的根,这时候x没有右孩子,左子树的点都是x的祖辈
  145. rev[x]:=not rev[x]; //执行翻转操作,这时候x是Auxiliary tree上最小的点,也就变成了原树的根
  146. end;
  147.  
  148. procedure link(x,y:longint);
  149. begin
  150. makeroot(x);
  151. fa[x]:=y;
  152. end;
  153.  
  154. procedure cut(x,y:longint);
  155. begin
  156. makeroot(x);
  157. access(y);
  158. splay(y);
  159. son[y,]:=;
  160. fa[x]:=;
  161. end;
  162.  
  163. procedure path(x,y:longint); //构成x到y的路径
  164. begin
  165. makeroot(x);
  166. access(y); //x到y路径上的点就在一棵Auxiliary tree中
  167. splay(y);
  168. end;
  169.  
  170. begin
  171. readln(n,m);
  172. for i:= to n do
  173. begin
  174. a[i]:=;
  175. size[i]:=;
  176. sum[i]:=;
  177. mul[i]:=;
  178. end;
  179. for i:= to n- do
  180. begin
  181. readln(x,y);
  182. link(x,y);
  183. end;
  184. for i:= to m do
  185. begin
  186. read(ch);
  187. if ch='+' then
  188. begin
  189. readln(x,y,z);
  190. z:=z mod mo;
  191. path(x,y);
  192. calc(y,,z);
  193. end
  194. else if ch='-' then
  195. begin
  196. readln(x0,y0,x,y);
  197. cut(x0,y0);
  198. link(x,y);
  199. end
  200. else if ch='*' then
  201. begin
  202. readln(x,y,z);
  203. z:=z mod mo;
  204. path(x,y);
  205. calc(y,z,);
  206. end
  207. else begin
  208. readln(x,y);
  209. path(x,y);
  210. writeln(sum[y]);
  211. end;
  212. end;
  213. end.

bzoj2631 3282的更多相关文章

  1. BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习

    #include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...

  2. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

  3. 【BZOJ】【3282】Tree

    LCT 喜闻乐见的Link-Cut-Tree…… srO zyf http://www.cnblogs.com/zyfzyf/p/4149109.html 目测我是第222个?………………不要在意这些 ...

  4. [BZOJ 3282] Tree 【LCT】

    题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...

  5. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  6. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  7. 【BZOJ】3282: Tree(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3282 复习了下lct,发现两个问题.. 1:一开始我以为splay那里直接全部rot(x)就好了,然 ...

  8. BZOJ 3282: Tree

    3282: Tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1714  Solved: 765[Submit][Status][Discuss ...

  9. bzoj 3282

    回顾一下LCT,容易写错的地方: 1.每次断掉Splay中的边,必须update一下父亲节点,再根据具体情况是否splay父亲节点. 2.养成没有用的值(比如当pre[u]不为0时的pnt[u])不去 ...

随机推荐

  1. VIEW层AJAX提交表单到Controller的实体

    在MVC环境中,AJAX方式添加一个对象,这个对象在Models中是一个视图模型,在前台显示时是这样的代码: <%using (Html.BeginForm())      { %>    ...

  2. EasyUI中combotree允许多选的时候onSelect事件会重复触发onCheck事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAADkCAIAAACOkmAuAAAgAElEQVR4nO2dW2wc15nnO0EQBJsdzA

  3. Unity中使用RequireComponent,没有添加上组件

    using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter), typeof(MeshRender ...

  4. 理解Java中的字符串类型

    1.Java内置对字符串的支持: 所谓的内置支持,即不用像C语言通过char指针实现字符串类型,并且Java的字符串编码是符合Unicode编码标准,这也意味着不用像C++那样通过使用string和w ...

  5. C# 时间与时间戳互转

    /// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="t ...

  6. ci 多个文件同时上传

    // 单个文件请手册,这里多个文件中,参数设置可参考手册 view 视图 <form...> <input type="file" name="user ...

  7. linux根目录详解

    ubuntu 文件说明:http://tech.ccidnet.com/art/302/20080118/1347213_1.html/ 根目录  |  |-boot/ 启动文件.所有与系统启动有关的 ...

  8. 解决IE6下不支持 png24的透明图片问题

    常用的两种解决方案: 第一:使用IE滤镜解决 关键代码: css代码  _background:none;_filter:progid:DXImageTransform.Microsoft.Alpha ...

  9. shell脚本修复MySQL主从同步

    发布:thebaby   来源:net     [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...

  10. python【第十八篇】Django基础

    1.什么是Django? Django是一个Python写成的开源Web应用框架.python流行的web框架还有很多,如tornado.flask.web.py等.django采用了MVC的框架模式 ...