#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=+,INF=-1u>>;
int v[maxn],maxv[maxn],minv[maxn],sumv[maxn],ch[maxn][],pre[maxn],top[maxn],flip[maxn],n,Q;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') sig=-;ch=getchar();}
while(isdigit(ch)) x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<) putchar('-'),x=-x;
int len=,buf[];while(x) buf[len++]=x%,x/=;
for(int i=len-;i>=;i--) putchar(buf[i]+'');return;
}
void maintain(int o){
int lc=ch[o][],rc=ch[o][];
maxv[o]=max(maxv[lc],maxv[rc]);
minv[o]=min(minv[lc],minv[rc]);
sumv[o]=sumv[lc]+sumv[rc];
if(v[o]){
sumv[o]+=v[o];
maxv[o]=max(maxv[o],v[o]);
minv[o]=min(minv[o],v[o]);
} return;
}
void pushdown(int o){
if(flip[o]){
flip[ch[o][]]^=;
flip[ch[o][]]^=;
swap(ch[o][],ch[o][]);
flip[o]=;
} return;
}
void rotate(int x,int d){
pushdown(x);
int y=pre[x],z=pre[y];
ch[y][d^]=ch[x][d];pre[ch[x][d]]=y;
ch[z][ch[z][]==y]=x;pre[x]=z;
ch[x][d]=y;pre[y]=x;
maintain(y);return;
}
void splay(int x){
int rt=x;
while(pre[rt]) rt=pre[rt];
if(x!=rt){
top[x]=top[rt];top[rt]=;
while(pre[x]){
pushdown(pre[x]);
rotate(x,ch[pre[x]][]==x);
} maintain(x);
} else pushdown(x);
return;
}
void access(int x){
int y=;
while(x){
splay(x);
top[ch[x][]]=x;pre[ch[x][]]=;
ch[x][]=y;
top[y]=;pre[y]=x;
maintain(x);
y=x;x=top[x];
} return;
}
void makeroot(int x){
access(x);splay(x);flip[x]^=;return;
}
void link(int u,int v){
makeroot(u);top[u]=v;return;
}
void query(int x,int y){
if(x==y){
puts("error");return;
}
makeroot(x);access(y);splay(y);
write(maxv[y]);putchar(' ');write(minv[y]);putchar(' ');write(sumv[y]);putchar('\n');
return;
}
void update(int pos,int cv){
splay(pos);v[pos]=cv;
maintain(pos);return;
}
void init(){
maxv[]=-INF;minv[]=INF;sumv[]=;
n=read();
for(int i=;i<n;i++){
int a=read(),b=read(),c=read();
v[i+n]=c;
link(a,i+n);link(i+n,b);
}
Q=read();
while(Q--)
{
int tp=read(),a=read(),b=read();
if(tp) query(a,b);
else update(a+n,b);
}
return ;
}
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. 山东省赛A题:Rescue The Princess

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3230 Description Several days ago, a beast caught ...

  2. 辛星浅析跨域传输的CORS解决方式

    首先我们有一个概念.那就是"同源准则",也就是same-origin  policy,它要求一个站点(协议+主机+port号)来确定的脚本.XMLHttpRequest和Webso ...

  3. 浅析mysql 共享表空间与独享表空间以及他们之间的转化

        innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等.对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间.什么是共享表空间和独占表空间共 ...

  4. Swift的闭包(二):捕获值

    闭包可以从定义它的上下文中捕获常量和变量. 在Swift中,捕获值最简单的例子是嵌套函数,举个例子: func makeIncrementer(forIncrement amount: Int) -& ...

  5. 7-http1.1和2.0的区别?

    1.多路复用:减少tcp请求 合并成一个2.首部压缩:会把多个首部压缩3.服务器推送:不用request也可以response

  6. MD5加密相关

    demo效果

  7. C#多线程lock解决数据同步

    1.代码实例: public class ThreadTest4 { public static void Init() { //多个线程修改同一个值,使用lock锁解决并发 ; i < ; i ...

  8. 分页技术之PageDataSource类

    之前给大家介绍了分页技术之Gridview控件,今天给大家介绍另外一种分页技术,采用PageDataSource类 + Repeater控件来实现. 前台只需要拖出一个Repeater控件来绑定要显示 ...

  9. js字符串比较

    1,大写字母小于小写字母 a='ang',b='Zh' 那么a>b 2,可以使用字符串的toUpperCase()/toLowerCase()方法不区分字母的大小写. a.toUpperCase ...

  10. struts.xml中的intercepter

    1. http://weizhilizhiwei.iteye.com/blog/1005210 Struts2 的核心——拦截器[Interceptor] 2. http://blog.csdn.ne ...