动态树有些类似 树链剖分+并查集 的思想,是用splay维护的

lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链

动态树的操作核心是把你要把 修改/询问/... 等等一系列的操作的树链放到一个splay里,然后用splay根据相对深度大小来维护这个树链

lct利用了splay的神奇性质,通过"认爹不认子"来达到记录多个子树的目的

lct的核心,access函数的意义是,在从这个点到它所在联通块中 相对深度最小的点 (可以理解为子树根) 的树链上,打通树链上每个点所在的splay树,并把需要的那部分树链放到一个splay树里,这样,这颗树链上的所有点就都在同一个splay里了,而且这颗splay里只存了这个树链而没有其他的点

由于根是动态的,所以有了makeroot函数,意义是把x点作为树根,先用access把需要的一部分树链取出,然后splay到根,发现x点原来是这条树链深度最大的点,现在深度变成了最小,用splay维护一下区间翻转即可

还有很多细节需要注意,洛谷上神犇的讲解很详细

 #include <cstdio>
#include <algorithm>
#include <cstring>
#define root d[0].ch[1]
#define il inline
#define nu 7777
#define inf 500000
#define N 300100
using namespace std; int n,m,tp;
int stk[N];
char str[];
struct Link_Cut_Tree{
int fa[N],ch[N][],sum[N],val[N],rev[N];
il int idf(int x){return ch[fa[x]][]==x?:;}
il int isroot(int x){return (ch[fa[x]][]!=x&&ch[fa[x]][]!=x)?:;}
il void con(int x,int ff,int p){fa[x]=ff;ch[ff][p]=x;}
il void pushup(int x){sum[x]=sum[ch[x][]]^sum[ch[x][]]^val[x];}
il void revers(int x){swap(ch[x][],ch[x][]),rev[x]^=;}
il void pushdown(int x){
if(rev[x]){
if(ch[x][]) revers(ch[x][]);
if(ch[x][]) revers(ch[x][]);
rev[x]=;}}
il void rot(int x){
int y=fa[x];int ff=fa[y];int px=idf(x);int py=idf(y);
if(!isroot(y)) ch[ff][py]=x;
fa[x]=ff,con(ch[x][px^],y,px),con(y,x,px^);
pushup(y),pushup(x);}
void splay(int x){
int y=x;stk[++tp]=x;
while(!isroot(y)){stk[++tp]=fa[y];y=fa[y];}
while(tp){pushdown(stk[tp--]);}
while(!isroot(x)){
y=fa[x];
if(isroot(y)) rot(x);
else if(idf(y)==idf(x)) rot(y),rot(x);
else rot(x),rot(x);
}}
il void access(int x){for(int y=;x;y=x,x=fa[x])splay(x),ch[x][]=y,pushup(x);}
il void mkroot(int x){access(x),splay(x),revers(x);}
il int findrt(int x){
access(x),splay(x);
while(ch[x][])pushdown(x),x=ch[x][];
return x;}
il void split(int x,int y){mkroot(x),access(y),splay(y);}
il void link(int x,int y){mkroot(x);if(findrt(y)!=x)fa[x]=y;}
il void cut(int x,int y){
mkroot(x);
if(findrt(y)==x&&fa[x]==y&&!ch[x][])
fa[x]=ch[y][]=,pushup(y);}
}lct; int gc()
{
int rett=,fh=;char c=getchar();
while(c<''||c>''){if(c=='-')fh=-;c=getchar();}
while(c>=''&&c<=''){rett=(rett<<)+(rett<<)+c-'';c=getchar();}
return rett*fh;
} int main()
{
n=gc(),m=gc();
for(int i=;i<=n;i++) lct.val[i]=gc();
int fl,x,y;
for(int i=;i<=m;i++)
{
fl=gc(),x=gc(),y=gc();
if(fl==) lct.split(x,y),printf("%d\n",lct.sum[y]);
if(fl==) lct.link(x,y);
if(fl==) lct.cut(x,y);
if(fl==) lct.splay(x),lct.val[x]=y,lct.pushup(x);
}
return ;
}

Link Cut Tree 动态树 小结的更多相关文章

  1. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  2. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  3. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  4. 洛谷P3690 Link Cut Tree (动态树)

    干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...

  5. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  6. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  9. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

随机推荐

  1. WEBGL学习【十二】鼠标操作场景

    <!DOCTYPE HTML> <html lang="en"> <head> <title>Listing 7-3 and 7-4 ...

  2. flex笔记 - 基础

    flex笔记 - 基础 文章中的所有图示代码都放在了github上: 阮一峰flex博客跟学代码 传统的布局解决方案,基于盒模型, 依赖 display, position, float属性来进行布局 ...

  3. jq实现瀑布流

    静态html代码: <!DOCTYPE html><html> <head> <meta charset="utf-8"> < ...

  4. MAVEN的结构认识篇

    1.maven的结构认识 src main com imooc calss test com imooc test resources pom.xml 以上是maven项目存在的必须结构!如下图 te ...

  5. MySQL导入到SQLServer

    Mysql是在Linux环境服务器,MSSQL在windows服务器上 1.在MSServer中安装VPN 2.为VPN配置Mysql服务器账号 3.账号中的文件 4.在MSSQL服务器上安装mysq ...

  6. NOIP2018提高组省一冲奖班模测训练(五)

    NOIP2018提高组省一冲奖班模测训练(五) http://www.51nod.com/Contest/ContestDescription.html#!#contestId=79 今天有点浪…… ...

  7. Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate(转)

    今天看了下Spring的源码——关于JDBC的"薄"封装,Spring 用一个Spring JDBC模板类来封装了繁琐的JDBC操作.下面仔细讲解一下Spring JDBC框架. ...

  8. 这个过人真是NB

  9. BA--空调系统一次泵和二次泵区别

    通常来说,空调系统是按照满负荷设计的,但实际运行中,满负荷运行的 时间不足 3% ,空调设备绝大部分时间内在远低于额定负荷的情况下运转.在 部分负荷下,虽然冷水机组可以根据实际负荷调节相应的冷量输出, ...

  10. rails undefined method error_messages

    rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...