lct

洛谷lct模板

详细网址:http://www.cnblogs.com/flashhu/p/8324551.html

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 200000
ll n,m,num,root,data[maxn],fa[maxn],leftson[maxn],rightson[maxn];
ll count2[maxn],v[maxn];
bool rev[maxn];
void down(ll x)
{
  if (!rev[x]) return;
  swap(leftson[x],rightson[x]); rev[x]=0;
  rev[leftson[x]]^=1; rev[rightson[x]]^=1; 
}
void updata(ll x)
{
    down(x);
    count2[x]=count2[leftson[x]]+count2[rightson[x]]+1;
    data[x]=data[leftson[x]]^data[rightson[x]]^v[x];
}
void pushr(ll x)
{
  rev[x]^=1;
}
bool pd(ll x)
{
  ll y=fa[x];
  if (leftson[y]!=x&&rightson[y]!=x) return(false);
  else return(true);
}
void rotate(ll x,ll y)
{
  ll father=fa[x];
  if (y==1)
  {
    rightson[father]=leftson[x];
    if (leftson[x]) fa[leftson[x]]=father;
  } else
  {
    leftson[father]=rightson[x];
    if (rightson[x]) fa[rightson[x]]=father;
  }
  fa[x]=fa[father];
  if (pd(father))
  {
    if (leftson[fa[father]]==father) 
      leftson[fa[father]]=x; else
      rightson[fa[father]]=x;
  }
  fa[father]=x;
  if (y==1) leftson[x]=father; else rightson[x]=father;
  updata(father); updata(x);
}
void dfs(ll x)
{
  if (pd(x)) dfs(fa[x]);
  down(x);
}
void splay(ll x)
{
  dfs(x);
  ll father=fa[x];
  while (pd(x))
  {
    if (!pd(father))
    {
      if (x==leftson[father]) rotate(x,2);
      else rotate(x,1);
    } else
    {
      if (father==leftson[fa[father]])
      {
        if (x==leftson[father]) 
          rotate(father,2),rotate(x,2);
        else rotate(x,1),rotate(x,2);
      } else
      {
        if (x==rightson[father])
          rotate(father,1),rotate(x,1);
        else rotate(x,2),rotate(x,1);
      }
    }
    father=fa[x];
  }
}
void access(ll x)
{
  for (ll y=0;x;y=x,x=fa[x])
    splay(x),rightson[x]=y,updata(x);
}
void makeroot(ll x)
{
  access(x);
  splay(x);
  pushr(x);
}
ll findroot(ll x)
{
  access(x);
  splay(x);
  while (leftson[x]) x=leftson[x];
  return x;
}
void split(ll x,ll y)
{
  makeroot(x);
  access(y);
  splay(y);
}
void link(ll x,ll y)
{
  makeroot(x);
  if (findroot(y)!=x) fa[x]=y;
}
void cut(ll x,ll y)
{
  makeroot(x);
  if (findroot(y)==x&&fa[x]==y&&!rightson[x])
  {
    fa[x]=leftson[y]=0;
    updata(y);
  }
}
int main()
{
  cin>>n>>m;
  for (ll i=1;i<=n;i++) cin>>v[i];
  ll c,x,y;
  while (m--)
  {
    cin>>c>>x>>y;
    if (c==0)
    {
      split(x,y); 
      cout<<data[y]<<endl;
    }
    if (c==1)
    {
      link(x,y);
    }
    if (c==2)
    {
      cut(x,y);
    }
    if (c==3)
    {
      splay(x); v[x]=y; updata(x);
    }
  }
  return 0;
} 
原文地址:https://www.cnblogs.com/yinwuxiao/p/8450118.html