【BZOJ3282】Tree (Link-Cut Tree)

【BZOJ3282】Tree (Link-Cut Tree)

题面

BZOJ权限题呀,良心luogu上有

题解

Link-Cut Tree班子提
最近因为NOIP考炸了
学科也炸了
时间显然没有
以后再来填LCT的坑
这种题目直接上代码了。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 333333
#define ls (t[x].ch[0])
#define rs (t[x].ch[1])
inline int read()
{
    RG int x=0,t=1;RG char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
struct Node
{
    int ch[2],ff;
    int rev,v,s;
}t[MAX];
int n,m;
bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
void pushup(int x){t[x].s=t[ls].s^t[rs].s^t[x].v;}
void rotate(int x)
{
    int y=t[x].ff,z=t[y].ff;
    int k=t[y].ch[1]==x;
    if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
    t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
    t[x].ch[k^1]=y;t[y].ff=x;
    pushup(y);pushup(x);
}
void pushdown(int x)
{
    if(!t[x].rev)return;
    t[x].rev^=1;swap(ls,rs);
    if(ls)t[ls].rev^=1;if(rs)t[rs].rev^=1;
}
int S[MAX],top;
void Splay(int x)
{
    S[top=1]=x;
    for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
    while(top)pushdown(S[top--]);
    while(!isroot(x))
    {
        int y=t[x].ff,z=t[y].ff;
        if(!isroot(y))
            (t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);
        rotate(x);
    }
}
void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),rs=y,pushup(x);}
void makeroot(int x){access(x);Splay(x);t[x].rev^=1;}
void split(int x,int y){makeroot(x);access(y);Splay(y);}
void link(int x,int y){makeroot(x);t[x].ff=y;}
void cut(int x,int y){split(x,y);if(t[y].ch[0]==x)t[y].ch[0]=t[x].ff=0;}
int findroot(int x){access(x);Splay(x);while(ls)x=ls;return x;}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;++i)t[i].v=read();
    while(m--)
    {
        int opt=read(),x=read(),y=read();
        if(opt==0){split(x,y);printf("%d
",t[y].s);}
        if(opt==1)if(findroot(x)!=findroot(y))link(x,y);
        if(opt==2)cut(x,y);
        if(opt==3)makeroot(x),t[x].v=y,pushup(x);	
    }
}
原文地址:https://www.cnblogs.com/cjyyb/p/7847240.html