ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

lct模板

2019-02-21 13:44:49  阅读:196  来源: 互联网

标签:lct ch int void tr wh splay 模板


  lct模板

网上有很多lct的bolgs了,这里就不详细论述。

几个函数:

1.access(x)

把x到原树的根的路径打通。

分成3步:

1.x转到当前的根

2.x的右儿子改成上一次的根(改成重边),维护信息

3.x往上跳轻边(x=tr[x].f)

Access后x到根在一颗splay里,但根不一定是splay的根,所以一般要splay(x)一下

 

2.makeroot(x)

使x成为原树的根。

先Access(x);splay(x);

然后此时x只有左儿子,swap一下x就变成深度最小的点了

 

3.findroot(x)

找到x所在原树的根

Access(x);splay(x);

一直向左找到深度最小的就行

注意要pushdown

 

4.split(x,y)

把x到y的链拉出来成为一颗splay

makeroot(x); access(y); splay(y); 

 

5.link(x,y)

makeroot(x)
if(findroot(y)!=x)tr[x].f=y;

6.cut

split(x,y);

if(findroot(y)==x&&tr[x].f==y&&tr[y].ch[0]==x&&tr[y].ch[1]==0)
{ 
  tr[x].f=0;tr[y].ch[0]=0; wh(y);
}

 

 

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 300005
using namespace std;
int n,m,v[maxn];
struct node{
    int f,ch[2],s;
    bool re;
}tr[maxn];
void wh(int x){
    tr[x].s=tr[tr[x].ch[0]].s^tr[tr[x].ch[1]].s^v[x];
}
int get(int x){
    return tr[tr[x].f].ch[1]==x;
}
bool isroot(int x){
    return tr[tr[x].f].ch[0]!=x&&tr[tr[x].f].ch[1]!=x;
}
void upr(int k){
    tr[k].re^=1;
    swap(tr[k].ch[0],tr[k].ch[1]);
}
void down(int k){
    if(tr[k].re){
        upr(tr[k].ch[0]);upr(tr[k].ch[1]);
        tr[k].re=0;
    }
}
void rotate(int x){
    int y=tr[x].f,z=tr[y].f;
    int wx=get(x),wy=get(y);
    if(!isroot(y))tr[z].ch[wy]=x;tr[x].f=z;//attation 
    tr[y].ch[wx]=tr[x].ch[wx^1];tr[tr[x].ch[wx^1]].f=y;
    tr[x].ch[wx^1]=y;tr[y].f=x;
    wh(y),wh(x);
}
int st[maxn];
void splay(int x){
    int y=x,top=0;
    st[++top]=y;
    while(!isroot(y))y=tr[y].f,st[++top]=y;
    while(top)down(st[top--]);//at
    while(!isroot(x)){
        int y=tr[x].f;
        if(!isroot(y))rotate(get(x)==get(y)?y:x);//at
        rotate(x);
    }
}
void access(int x){
    for(int y=0;x;y=x,x=tr[x].f)
        splay(x),tr[x].ch[1]=y,wh(x);
}
void makeroot(int x){//原树的根 
    access(x);splay(x);
    upr(x);
    wh(x);
}
int findroot(int x){
    access(x);splay(x);
    int f;
    while(x)down(x),f=x,x=tr[x].ch[0];
    return f;
}
void split(int x,int y){
    makeroot(x);
    access(y);splay(y);
}
void link(int x,int y){
    makeroot(x);
    if(findroot(y)!=x)tr[x].f=y;    
}
void cut(int x,int y){
    split(x,y);
    if(findroot(y)==x&&tr[x].f==y&&tr[y].ch[0]==x&&tr[y].ch[1]==0){
        tr[x].f=0;tr[y].ch[0]=0;
        wh(y);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)scanf("%d",&v[i]);
    for(int i=1,op,x,y;i<=m;i++){
        scanf("%d%d%d",&op,&x,&y);
        if(op==0)split(x,y),printf("%d\n",tr[y].s);
        if(op==1)link(x,y);
        if(op==2)cut(x,y);
        if(op==3)splay(x),v[x]=y;
    }
    return 0;
}
View Code

 

标签:lct,ch,int,void,tr,wh,splay,模板
来源: https://www.cnblogs.com/liankewei/p/10411873.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有