动态树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 ...
随机推荐
- java 新建文件夹保存
String Txtname = field.getText(); File file3 =new File("D:\\MATP_robot"+&qu ...
- 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 ...
- iOS平台基于ffmpeg的视频直播技术揭秘
现在非常流行直播,相信很多人都跟我一样十分好奇这个技术是如何实现的,正好最近在做一个ffmpeg的项目,发现这个工具很容易就可以做直播,下面来给大家分享下技术要点: 首先你得编译出ffmpeg运行所需 ...
- Activity的任务栈Task以及启动模式与Intent的Flag详解
什么是任务栈(Task) 官方文档是这么解释的 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即“返回栈”)中. 其实就是以栈的 ...
- android中的文件操作详解以及内部存储和外部存储(转载)
原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...
- Linux 网络配置(固定IP)
通常linux作为服务器系统时,是不推荐安装图形界面的,因此我们需要掌握非图形界面下如何配置网络,主要两种方式,如下: 一.使用SETUP工具(redhat系列才可以,推荐此修改方式) 1.在命令行直 ...
- 查看library_cache 库缓冲区的命中率
关于library cache的命中率: SQL> desc V$librarycache NAMESPACE ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
- 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 ...
- UITouch触摸事件
UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...