【题目分析】

又一道LCT的题目,LCT只能维护链上的信息。

【代码】

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <iostream>
  12. #include <queue>
  13.  
  14. using namespace std;
  15.  
  16. #define maxn 1000005
  17. #define inf (0x3f3f3f3f)
  18. #define mod 51061
  19.  
  20. unsigned int read()
  21. {
  22. unsigned int x=0,f=1; char ch=getchar();
  23. while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
  24. while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
  25. return x*f;
  26. }
  27.  
  28. unsigned int ch[maxn][2],fa[maxn],add[maxn],mul[maxn],num[maxn],sum[maxn];
  29. unsigned int n,m,siz[maxn],rev[maxn],sta[maxn],top=0;
  30.  
  31. void update(unsigned int x)
  32. {
  33. siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
  34. sum[x]=(sum[ch[x][0]]+sum[ch[x][1]]+num[x])%mod;
  35. }
  36.  
  37. void solve(unsigned int x,unsigned int ad,unsigned int ml)
  38. {
  39. if (!x) return ;
  40. add[x]=(add[x]*ml+ad)%mod; mul[x]=(mul[x]*ml)%mod;
  41. sum[x]=(sum[x]*ml+siz[x]*ad)%mod; num[x]=(num[x]*ml+ad)%mod;
  42. }
  43.  
  44. void pushdown(unsigned int x)
  45. {
  46. if (rev[x])
  47. {
  48. rev[x]^=1;
  49. rev[ch[x][0]]^=1;
  50. rev[ch[x][1]]^=1;
  51. swap(ch[x][0],ch[x][1]);
  52. }
  53. solve(ch[x][0],add[x],mul[x]);
  54. solve(ch[x][1],add[x],mul[x]);
  55. mul[x]=1; add[x]=0;
  56. }
  57.  
  58. bool isroot(unsigned int x)
  59. {
  60. return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
  61. }
  62.  
  63. void rot(unsigned int x)
  64. {
  65. unsigned int y=fa[x],z=fa[y],l,r;
  66. if (ch[y][0]==x) l=0; else l=1;
  67. r=l^1;
  68. if (!isroot(y))
  69. {
  70. if (ch[z][0]==y) ch[z][0]=x;
  71. else ch[z][1]=x;
  72. }
  73. fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
  74. ch[y][l]=ch[x][r]; ch[x][r]=y;
  75. update(y); update(x);
  76. }
  77.  
  78. void splay(unsigned int x)
  79. {
  80. unsigned int top=0; sta[top++]=x;
  81. for (int i=x;!isroot(i);i=fa[i]) sta[top++]=fa[i];
  82. while (top--) pushdown(sta[top]);
  83.  
  84. while (!isroot(x))
  85. {
  86. unsigned int y=fa[x],z=fa[y];
  87. if (!isroot(y))
  88. {
  89. if (ch[z][0]==y^ch[y][0]==x) rot(x);
  90. else rot(y);
  91. }
  92. rot(x);
  93. }
  94. }
  95.  
  96. void access(unsigned int x)
  97. {
  98. for (int t=0;x;t=x,x=fa[x])
  99. splay(x),ch[x][1]=t,update(x);
  100. }
  101.  
  102. void makeroot(unsigned int x)
  103. {
  104. access(x);
  105. splay(x);
  106. rev[x]^=1;
  107. }
  108.  
  109. unsigned int find(unsigned int x)
  110. {
  111. access(x);
  112. splay(x);
  113. while (ch[x][0]) x=ch[x][0];
  114. return x;
  115. }
  116.  
  117. void link(unsigned int x,unsigned int y)
  118. {
  119. makeroot(x);
  120. fa[x]=y;
  121. }
  122.  
  123. void cut(unsigned int x,unsigned int y)
  124. {
  125. makeroot(x);
  126. access(y);
  127. splay(y);
  128. ch[y][0]=fa[x]=0;
  129. }
  130.  
  131. int main()
  132. {
  133. n=read();m=read();
  134. for (unsigned int i=1;i<=n;++i) num[i]=1,update(i);
  135. char opt[10];
  136. for (unsigned int i=1;i<n;++i) link(read(),read());
  137. while (m--)
  138. {
  139. scanf("%s",opt);
  140. unsigned int x,y,z;
  141. switch (opt[0])
  142. {
  143. case '+':
  144. x=read(); y=read(); z=read();
  145. makeroot(x);
  146. access(y);
  147. splay(y);
  148. solve(y,z,1);
  149. break;
  150. case '-':
  151. cut(read(),read()); link(read(),read());
  152. break;
  153. case '*':
  154. x=read();y=read();z=read();
  155. makeroot(x);
  156. access(y);
  157. splay(y);
  158. solve(y,0,z);
  159. break;
  160. case '/':
  161. x=read();y=read();
  162. makeroot(x);
  163. access(y);
  164. splay(y);
  165. printf("%u\n",sum[y]);
  166. break;
  167. }
  168. }
  169. }

  

BZOJ 2631 Tree ——Link-Cut Tree的更多相关文章

  1. bzoj 3282: Tree (Link Cut Tree)

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

  2. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  3. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  4. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  7. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  8. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  9. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  10. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

随机推荐

  1. MyBatis之多表关联查询

    1使用resultType.ResultMap处理返回结果 处理返回结果 resultType:指定返回值结果的完全限定名,处理多表查询的结果. 多表查询需要定义vo封装查询的结果. 需求:查询部门和 ...

  2. XML 数据请求与JSON 数据请求

    (1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...

  3. spring集成activeMQ

    1.安装activehttp://activemq.apache.org/activemq-5140-release.html2.运行D:\apache-activemq-5.14.0\bin\win ...

  4. Android高性能ORM数据库DBFlow入门

    DBFlow,综合了 ActiveAndroid, Schematic, Ollie,Sprinkles 等库的优点.同时不是基于反射,所以性能也是非常高,效率紧跟greenDAO其后.基于注解,使用 ...

  5. JavaScript基础——处理字符串

    String对象是迄今为止在JavaScript中最常用的对象.在你定义一个字符串数据类型的变量的任何时候,JavaScript就自定为你创建一个String对象.例如: var myStr = &q ...

  6. mysql入门语句10条

    1,连接数据库服务器 mysql  -h host   -u root   -p  xxx(密码) 2,查看所有库 show databases; 3,选库 use 库名 4,查看库下面的表 show ...

  7. JAVA作业02

    一,      课堂练习 (一)构造方法 1,源代码 public class Test{ public static void main(String[] args){ Foo obj1=new F ...

  8. JQ 练习题

    1.留言板 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  9. js 动态时钟

    js 动态时钟 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  10. hdu 4041 2011北京赛区网络赛B 搜索 ***

    直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...