洛谷:P3690 【模板】Link Cut Tree (动态树)

/*诸多细节,不注意就会调死去! 见注释。*/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=;
int n,m;
int fa[MAXN],rev[MAXN],val[MAXN],xr[MAXN],Q[MAXN],ch[MAXN][];
bool isroot(int x){ return ch[fa[x]][]!=x && ch[fa[x]][]!=x; }
void Update(int x){ xr[x]=xr[ch[x][]]^xr[ch[x][]]^val[x]; }
bool get(int x){ return ch[fa[x]][]==x ;}
void Down(int x){ if(rev[x]){ rev[ch[x][]]^=; rev[ch[x][]]^=; rev[x]^=; swap(ch[x][],ch[x][]); } }
void Rotate(int x)
{
int old=fa[x],oldf=fa[old],op=get(x);
if(!isroot(old)) ch[oldf][ch[oldf][]==old]=x; //这一条一定要放在改变父子关系之前!在纯Splay中是放在后面的,因为直接看oldf是否为0可知old是否为根。
ch[old][op]=ch[x][op^]; fa[ch[x][op^]]=old; //但这里使用isroot,改变之后就不能判断了!
ch[x][op^]=old; fa[old]=x; fa[x]=oldf;
Update(old); Update(x);
}
void Splay(int x)
{
int tp=; Q[]=x;
for(int i=x;!isroot(i);i=fa[i]) Q[++tp]=fa[i]; //对于LCT的判断是否是根节点,需要使用isroot,在纯Splay中使用的是fa,不要搞混!
for(int i=tp;i;i--) Down(Q[i]);
for(int FA; !isroot(x) ; Rotate(x))
{
FA=fa[x];
if(!isroot(FA))
Rotate(get(x)==get(FA)?FA:x);
}
}
void Access(int x){ int t=; while(x){ Splay(x); ch[x][]=t; Update(x); t=x; x=fa[x]; } } //记得Update
void Makeroot(int x){ Access(x); Splay(x); rev[x]^=;}
void Link(int x,int y){ Makeroot(x); fa[x]=y; }
void Cut(int x,int y){ Makeroot(x); Access(y); Splay(y); if(ch[y][]==x) fa[x]=ch[y][]=;}
int Find(int x){ Access(x); Splay(x); while(ch[x][]) x=ch[x][]; return x; }
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&val[i]) , xr[i]=val[i];
while(m--)
{
int x,y,op;
scanf("%d%d%d",&op,&x,&y);
if(op==){ Makeroot(x); Access(y); Splay(y); printf("%d\n",xr[y]); }
if(op==){ if(Find(x)!=Find(y)) Link(x,y); }
if(op==){ Cut(x,y); }
if(op==){ Makeroot(x); val[x]=y; Update(x); }
}
return ;
}

LCT 动态树 模板的更多相关文章

  1. [HNOI2010]弹飞绵羊 (平衡树,LCT动态树)

    题面 题解 因为每个点都只能向后跳到一个唯一的点,但可能不止一个点能跳到后面的某个相同的点, 所以我们把它抽象成一个森林.(思考:为什么是森林而不是树?) 子节点可以跳到父节点,根节点再跳就跳飞了. ...

  2. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

  3. LCT动态树入门

    LCT,link-cut-tree,一种基于splay的高级数据结构,常用于维护动态森林问题,但ta只能维护子树信息,无法修改子树信息. 首先,如果你不会splay,来这里看看吧. 接下来步入正题. ...

  4. Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5020  Solved: 1872[Submit][Status ...

  5. Hdu 3966-Aragorn's Story LCT,动态树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Aragorn's Story Time Limit: 10000/3000 MS (Java/Ot ...

  6. Hdu 4010-Query on The Trees LCT,动态树

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  7. 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 12266  Solved: 4945[Submit ...

  8. HDU4010 Query on The Trees (LCT动态树)

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  9. Hdu 2475-Box LCT,动态树

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Pipeline inbound(netty源码7)

    netty源码死磕7  Pipeline 入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Handler节点 ( ...

  2. openssl之BIO系列之20---缓冲(buffer)类型BIO

    缓冲(buffer)类型BIO ---依据openssl doc\crypto\bio_f_buffer.pod翻译和自己的理解写成 (作者:DragonKing, Mail: wzhah@263.n ...

  3. 关于ActiveMQ接收端停止接收的方法

    现在有一个需求: 在发送端服务器出现故障后,接收端的接收方法要停下来,关于停止接收的方法,我做了下面这些事情: // 获取 ConnectionFactory ConnectionFactory co ...

  4. swift-ios开发pod的使用(1)

    MAC安裝CocoaPods   http://www.cnblogs.com/surge/p/4436360.html 请注意我的环境,这个很重要 xcode版本7.3.2   mac 版本OS X ...

  5. CoreData使用

    1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...

  6. BZOJ 2002 Bounce 弹飞绵羊 —— 分块算法

    题目链接:https://vjudge.net/problem/HYSBZ-2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Li ...

  7. 【Selenium】IE浏览器启动问题

    DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();ieCapabilities.setCapabi ...

  8. 洛谷 P1509 找啊找啊找GF(复习二维费用背包)

    传送门 题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sq ...

  9. Android四种启动模式

    四种启动模式 standard(默认) singleTop singleTast singleInstance standard(默认) 系统默认的启动模式. Android是使用返回栈来管理活动的, ...

  10. hibernate VS mybatis

    1: 一般来说,业务逻辑比较简单,集增删改查就可以满足需求,建议使用hibernate,而复杂的业务逻辑,尤其是多表关联查询,建议使用mybatis. 2: hibernate有更好的二级缓存机制,可 ...