洛谷: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. AppStore审核--17.5

    本文转载至 http://blog.csdn.net/addychen/article/details/39672185 感谢原文作者分享 AppStore审核 为了确保用户理解应用如何使用他们的数据 ...

  2. 开源流媒体云视频平台EasyDarwin中EasyCMS服务是如何进行命令转发和消息路由的

    EasyCMS介绍 EasyCMS做为EasyDarwin开源流媒体云平台解决方案的一部分,主要进行的是设备的接入和Session(DeviceSession & ClientSession) ...

  3. CMD命令操作

    win + R 然后输入 cmd 打开命令窗口 或者开始-->运行 打开 1.进入 D盘 输 d: 回车2.进入D盘 子目录 输入 cd d:\test 回车 3. dir 回车 进入文件目录 ...

  4. port_443

    GRC | Port Authority, for Internet Port 443 https://www.grc.com/port_443.htm

  5. Win32对话框工程笔记

    Main.cpp #include <Windows.h> #include "resource.h" INT_PTR CALLBACK dialogProc(HWND ...

  6. 对ShortCut和TWMKey的研究

    TWMKey = packed record Msg: Cardinal; CharCode: Word; Unused: Word; KeyData: Longint; Result: Longin ...

  7. 杭电 2553 N皇后问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  8. java读流方式,下载网络上的图片

    本工具类支持url的list集合,具体实现如下所示: public static void download(ArrayList<String> listUrl, String downl ...

  9. Git检出远程库的分支等相关操作

    来到公司,询问同事后发现系统已经上传到Git远程仓库: 我这里先把远程仓库clone下来: $ git clone http://git.eas****tect.git 发现目录下只有一个READY. ...

  10. ZOJ - 3954 Seven-Segment Display 【状态标记】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 题意 有一块 LED 灯 然后上面有七块灯管 在显示不同数 ...