[Template] Link Cut Tree (dynamic tree) LCT Summary - Los concept papers + Valley P3690 [template] Link Cut Tree (dynamic tree) (LCT, Splay)

Amway bigwigs to explain:

LCT Summary - Los concept papers + Valley P3690 [template] Link Cut Tree (dynamic tree) (LCT, Splay)

Topic transfer: [template] Link Cut Tree (dynamic tree) (luogu)

Description

Write a data structure that supports the right to change the value of the node, even the edges, border erase, the nodes on the right path value between the query and two XOR

Solution

Gangster has made it very clear explanation of the

Here add that easy to believe konjac details jump pit (to his code as an example)

  • FA [] to store the information of the original tree is the father, but is in a splay disrupted, can only be determined in each splay tree traversal sequence of the original point is the father of the first FA [fundamental splay trees tree]
  • link (x, y) must be written f [x] = y, but not writing f [y] = x, y may have stored as information on the father splay, splay and x is the root of the tree
  • Note that splay and rotate functions functions and traditional writing different, LCT is the treatment of the more severe 0

Code I template (forced to rely on their traditional writing ...)

 

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int fa[N],ch[N][2];
int s[N],d[N],n,m,opt,x,y,rev[N];
bool nroot(int x)
{
    return x==ch[fa[x]][0] || x==ch[fa[x]][1];
}
int get(int x)
{
    return x==ch[fa[x]][1];
}
void push_up(int x)
{
    s[x]=s[ch[x][0]]^s[ch[x][1]]^d[x];
}
void change(int x)
{
    swap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}
void push_down(int x)
{
    if(rev[x])
    {
        change(ch[x][0]);
        change(ch[x][1]);
        rev[x]=0;
    }
}
void dfs(int x)
{
    if(nroot(x)) dfs(fa[x]);
    push_down(x);
}
void rotate(int x)
{
    int y=fa[x],z=fa[y],wh=get(x);
    if(nroot(y)) ch[z][get(y)]=x;
    if(ch[x][wh^1]) fa[ch[x][wh^1]]=y;
    ch[y][wh]=ch[x][wh^1];
    ch[x][wh^1]=y;
    fa[y]=x,fa[x]=z;
    push_up(y),push_up(x);
}
void splay(int x)
{
    dfs(x);
    for(int fx;fx=fa[x],nroot(x);rotate(x))
        if(nroot(fx)) rotate(get(x)==get(fx)?fx:x);
}
void access(int x)
{
    for(int y=0;x;x=fa[y=x])
        splay(x),ch[x][1]=y,push_up(x);
}
void makeroot(int x)
{
    access(x),splay(x);
    change(x);
}
int findroot(int x)
{
    access(x),splay(x);
    while(ch[x][0]) push_down(x),x=ch[x][0];
    splay(x);
    return x;
}
void link(int x,int y)
{
    makeroot(x);
    if(findroot(y)!=x) fa[x]=y;
}
void cut(int x,int y)
{
    makeroot(x);
    if(findroot(y)==x && fa[y]==x && !ch[y][0])
        fa[y]=ch[x][1]=0;
}
void split(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&d[i]);
    while(m--)
    {
        scanf("%d%d%d",&opt,&x,&y);
        if(opt==0) split(x,y),printf("%d\n",s[y]);
        else if(opt==1) link(x,y);
        else if(opt==2) cut(x,y);
        else splay(x),d[x]=y,push_up(x);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hsez-cyx/p/12287331.html