1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=+,INF=-1u>>;
  7. int v[maxn],maxv[maxn],minv[maxn],sumv[maxn],ch[maxn][],pre[maxn],top[maxn],flip[maxn],n,Q;
  8. inline int read(){
  9. int x=,sig=;char ch=getchar();
  10. while(!isdigit(ch)){if(ch=='-') sig=-;ch=getchar();}
  11. while(isdigit(ch)) x=*x+ch-'',ch=getchar();
  12. return x*=sig;
  13. }
  14. inline void write(int x){
  15. if(x==){putchar('');return;}if(x<) putchar('-'),x=-x;
  16. int len=,buf[];while(x) buf[len++]=x%,x/=;
  17. for(int i=len-;i>=;i--) putchar(buf[i]+'');return;
  18. }
  19. void maintain(int o){
  20. int lc=ch[o][],rc=ch[o][];
  21. maxv[o]=max(maxv[lc],maxv[rc]);
  22. minv[o]=min(minv[lc],minv[rc]);
  23. sumv[o]=sumv[lc]+sumv[rc];
  24. if(v[o]){
  25. sumv[o]+=v[o];
  26. maxv[o]=max(maxv[o],v[o]);
  27. minv[o]=min(minv[o],v[o]);
  28. } return;
  29. }
  30. void pushdown(int o){
  31. if(flip[o]){
  32. flip[ch[o][]]^=;
  33. flip[ch[o][]]^=;
  34. swap(ch[o][],ch[o][]);
  35. flip[o]=;
  36. } return;
  37. }
  38. void rotate(int x,int d){
  39. pushdown(x);
  40. int y=pre[x],z=pre[y];
  41. ch[y][d^]=ch[x][d];pre[ch[x][d]]=y;
  42. ch[z][ch[z][]==y]=x;pre[x]=z;
  43. ch[x][d]=y;pre[y]=x;
  44. maintain(y);return;
  45. }
  46. void splay(int x){
  47. int rt=x;
  48. while(pre[rt]) rt=pre[rt];
  49. if(x!=rt){
  50. top[x]=top[rt];top[rt]=;
  51. while(pre[x]){
  52. pushdown(pre[x]);
  53. rotate(x,ch[pre[x]][]==x);
  54. } maintain(x);
  55. } else pushdown(x);
  56. return;
  57. }
  58. void access(int x){
  59. int y=;
  60. while(x){
  61. splay(x);
  62. top[ch[x][]]=x;pre[ch[x][]]=;
  63. ch[x][]=y;
  64. top[y]=;pre[y]=x;
  65. maintain(x);
  66. y=x;x=top[x];
  67. } return;
  68. }
  69. void makeroot(int x){
  70. access(x);splay(x);flip[x]^=;return;
  71. }
  72. void link(int u,int v){
  73. makeroot(u);top[u]=v;return;
  74. }
  75. void query(int x,int y){
  76. if(x==y){
  77. puts("error");return;
  78. }
  79. makeroot(x);access(y);splay(y);
  80. write(maxv[y]);putchar(' ');write(minv[y]);putchar(' ');write(sumv[y]);putchar('\n');
  81. return;
  82. }
  83. void update(int pos,int cv){
  84. splay(pos);v[pos]=cv;
  85. maintain(pos);return;
  86. }
  87. void init(){
  88. maxv[]=-INF;minv[]=INF;sumv[]=;
  89. n=read();
  90. for(int i=;i<n;i++){
  91. int a=read(),b=read(),c=read();
  92. v[i+n]=c;
  93. link(a,i+n);link(i+n,b);
  94. }
  95. Q=read();
  96. while(Q--)
  97. {
  98. int tp=read(),a=read(),b=read();
  99. if(tp) query(a,b);
  100. else update(a+n,b);
  101. }
  102. return ;
  103. }
  104. int main(){init();return ;}

动态树LCT的更多相关文章

  1. hdu 5398 动态树LCT

    GCD Tree Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. hdu 5002 (动态树lct)

    Tree Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. 动态树LCT小结

    最开始看动态树不知道找了多少资料,总感觉不能完全理解.但其实理解了就是那么一回事...动态树在某种意思上来说跟树链剖分很相似,都是为了解决序列问题,树链剖分由于树的形态是不变的,所以可以通过预处理节点 ...

  4. bzoj2049-洞穴勘测(动态树lct模板题)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  5. [模板] 动态树/LCT

    简介 LCT是一种数据结构, 可以维护树的动态加边, 删边, 维护链上信息(满足结合律), 单次操作时间复杂度 \(O(\log n)\).(不会证) 思想类似树链剖分, 因为splay可以换根, 用 ...

  6. 动态树LCT(Link-cut-tree)总结+模板题+各种题目

    一.理解LCT的工作原理 先看一道例题: 让你维护一棵给定的树,需要支持下面两种操作: Change x val:  令x点的点权变为val Query x y:  计算x,y之间的唯一的最短路径的点 ...

  7. SPOJ OTOCI 动态树 LCT

    SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...

  8. HDU 4718 The LCIS on the Tree (动态树LCT)

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  9. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

  10. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

随机推荐

  1. java 新建文件夹保存

    String Txtname = field.getText();                File file3 =new File("D:\\MATP_robot"+&qu ...

  2. Slow Server? This is the Flow Chart You're Looking For--reference

    Your high-powered server is suddenly running dog slow, and you need to remember the troubleshooting ...

  3. iOS平台基于ffmpeg的视频直播技术揭秘

    现在非常流行直播,相信很多人都跟我一样十分好奇这个技术是如何实现的,正好最近在做一个ffmpeg的项目,发现这个工具很容易就可以做直播,下面来给大家分享下技术要点: 首先你得编译出ffmpeg运行所需 ...

  4. Activity的任务栈Task以及启动模式与Intent的Flag详解

    什么是任务栈(Task) 官方文档是这么解释的 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即“返回栈”)中. 其实就是以栈的 ...

  5. android中的文件操作详解以及内部存储和外部存储(转载)

    原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...

  6. Linux 网络配置(固定IP)

    通常linux作为服务器系统时,是不推荐安装图形界面的,因此我们需要掌握非图形界面下如何配置网络,主要两种方式,如下: 一.使用SETUP工具(redhat系列才可以,推荐此修改方式) 1.在命令行直 ...

  7. 查看library_cache 库缓冲区的命中率

    关于library cache的命中率:    SQL> desc V$librarycache    NAMESPACE                                     ...

  8. Data Types

    原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...

  9. How do I size a UITextView to its content?

    UITextView 自适应高度,搬来一篇stack上的:   Is there a good way to adjust the size of a UITextView to conform to ...

  10. UITouch触摸事件

    UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...