动态树LCT
#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的更多相关文章
- hdu 5398 动态树LCT
GCD Tree Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5002 (动态树lct)
Tree Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 动态树LCT小结
最开始看动态树不知道找了多少资料,总感觉不能完全理解.但其实理解了就是那么一回事...动态树在某种意思上来说跟树链剖分很相似,都是为了解决序列问题,树链剖分由于树的形态是不变的,所以可以通过预处理节点 ...
- bzoj2049-洞穴勘测(动态树lct模板题)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- [模板] 动态树/LCT
简介 LCT是一种数据结构, 可以维护树的动态加边, 删边, 维护链上信息(满足结合律), 单次操作时间复杂度 \(O(\log n)\).(不会证) 思想类似树链剖分, 因为splay可以换根, 用 ...
- 动态树LCT(Link-cut-tree)总结+模板题+各种题目
一.理解LCT的工作原理 先看一道例题: 让你维护一棵给定的树,需要支持下面两种操作: Change x val: 令x点的点权变为val Query x y: 计算x,y之间的唯一的最短路径的点 ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- 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 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2843 Solved: 1519[Submi ...
- 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 ...
随机推荐
- ios 打电话结束返回到应用中
在我们做打电话这个功能时,我们常常是调用这个方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel ...
- @property的特性
@property还有一些关键字,它们都是有特殊作用的,比如上述代码中的nonatomic,strong: 1 2 @property(nonatomic,strong) NSString *carN ...
- [PWA] 10. Trigger a version update
When the refersh button is clicked, we need to tell the waiting service worker to replace the curren ...
- Notice : Soft open files now is 1024, We recommend greater than 10000
在研究 workerman 时, 报了这个错误, 感觉只是个notice级别的, 就一直给忽略掉了, 今天有时间, 就查了一下. 其实本质就是 ulimit 这个命令 打开一个命令行, 输入 ulim ...
- POJ 2891 扩展欧几里德
这个题乍一看跟剩余定理似的,但是它不满足两两互素的条件,所以不能用剩余定理,也是给了一组同余方程,找出一个X满足这些方程,如果找不到的话就输出-1 因为它不满足互素的条件,所以两个两个的合并,最后合成 ...
- JS的replace()的应用
替换字符串中的空格 /\s/ig 例如: var pro="ssss ssss sss ddd ss" var protext = pro.replace(/\s/ig,&qu ...
- win7 打开方式不能添加程序
打开注册表,找到“HKEY_CLASSES_ROOT\Applications\”中,查看相应的程序的“\shell\open\command”项中的数据是否正确:如果不正确,就修改正确,之后再添加程 ...
- Mysql 中is null 和 =null 的区别
在mysql中,筛选非空的时候经常会用到is not null和!=null,这两种方法单从字面上来看感觉是差不多的,其实如 果去运行一下试试的话差别会很大! 为什么会出现这种情况呢? null 表示 ...
- IDisposable接口详解
转载:http://www.cnblogs.com/davyli/archive/2010/09/13/1825042.html 正确实现 IDisposable .NET中用于释放对象资源的接口是I ...
- Java序列化的机制和原理
Java序列化的机制和原理 本文讲解了Java序列化的机制和原理.从文中你可以了解如何序列化一个对象,什么时候需要序列化以及Java序列化的算法. 有关Java对象的序列化和反序列化也算是Java基础 ...