动态树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 ...
随机推荐
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- common-httpclient 用户名密码认证示例
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.com ...
- const中的一些tricky的地方
1. 为了逻辑上的优化需要,const成员函数可能想修改某些成员变量,把这些成员变量定义为mutable可以绕过const的检查 2. 调用const和non-const的参数的函数可以重载 3. s ...
- iOS开发系列之运动事件
前面我们主要介绍了触摸事件以及由触摸事件引出的手势识别,下面我们简单介绍一下运动事件.在iOS中和运动相关的有三个事件:开始运动.结束运动.取消运动. 监听运动事件对于UI控件有个前提就是监听对象必须 ...
- MWEB+七牛 上传图片
MWEB+七牛 上传图片 博客之前的图片也都用的七牛,但编辑和上传分离还是很麻烦,所以一直很心水meb, 上周mweb降到¥50,感觉短期内应该不会再降了,于是果断入手,今天在和使用图床功能遇到了一些 ...
- BeanFactory学习
关于BeanFactory,一个相对易懂的博客,关于深入的理解,后续继续学习 http://www.cnblogs.com/liuling/archive/2013/04/14/BeanFactory ...
- asp.net web.config的学习笔记
原文地址:http://www.cnblogs.com/Bulid-For-NET/archive/2013/01/11/2856632.html 一直都对web.config不太清楚.这几天趁着项目 ...
- How JSP work.
A JSP page exists in three forms: JSP source code: consists of a mix of HTML template code. Java lan ...
- Swift进阶
概述 访问控制 Swift命名空间 Swift和ObjC互相调用 Swift和ObjC映射关系 Swift调用ObjC ObjC调用Swift 扩展—Swift调用C 反射 扩展—KVO 内存管理 循 ...
- 虚拟器运行iOS8地图提示错误
/SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-225/Runtime/PBRequester.m:799 server (https://gsp13-c ...