LCT模板

LCT

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cmath>
  6 using namespace std;
  7 #define N 10010
  8 
  9 int f[N],son[N][2],val[N],sum[N],tmp[N];bool rev[N];
 10 bool isroot(int x)
 11 {
 12     return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;
 13 }//判根
 14 
 15 void rev1(int x)
 16 {
 17     if(!x) return;
 18     swap(son[x][0],son[x][1]);
 19     rev[x]^=1;
 20 }//翻转
 21 
 22 void pb(int x)
 23 {
 24     if(rev[x])
 25         rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;
 26 }//翻转
 27 
 28 void up(int x)
 29 {
 30         sum[x]=val[x];
 31         if(son[x][0])sum[x]+=sum[son[x][0]];
 32         if(son[x][1])sum[x]+=sum[son[x][1]];
 33 }//更新值
 34 
 35 void rotate(int x)
 36 {
 37     int y=f[x],w=son[y][1]==x;
 38     son[y][w]=son[x][w^1];
 39     if(son[x][w^1]) f[son[x][w^1]]=y;
 40     if(f[y])
 41         {
 42             int z=f[y];
 43             if(son[z][0]==y)son[z][0]=x;
 44                 else if(son[z][1]==y)son[z][1]=x;
 45         }
 46     f[x]=f[y];f[y]=x;
 47     son[x][w^1]=y;up(y);
 48 }//旋转
 49 
 50 void splay(int x)
 51 {
 52     int s=1,i=x,y;tmp[1]=i;
 53     while(!isroot(i)) tmp[++s]=i=f[i];
 54     while(s) pb(tmp[s--]);
 55     while(!isroot(x))
 56         {
 57             y=f[x];
 58             if(!isroot(y))
 59                 {
 60                     if((son[f[y]][0]==y)^(son[y][0]==x))
 61                     rotate(x);  else  rotate(y);
 62                 }
 63             rotate(x);
 64         }
 65     up(x);
 66 }//伸展
 67 
 68 void access(int x)
 69 {
 70     for(int y=0;x;y=x,x=f[x])
 71         splay(x),son[x][1]=y,up(x);
 72 }//访问
 73 
 74 int root(int x)
 75 {
 76     access(x);splay(x);
 77     while(son[x][0]) x=son[x][0];
 78     return x;
 79 }//换根
 80 
 81 void makeroot(int x)
 82 {
 83     access(x);
 84     splay(x);
 85     rev1(x);
 86 }
 87 
 88 void link(int x,int y)
 89 {
 90     makeroot(x);
 91     f[x]=y;
 92     access(x);
 93 }
 94 
 95 void cutf(int x)
 96 {
 97     access(x);
 98     splay(x);
 99     f[son[x][0]]=0;
100     son[x][0]=0;
101     up(x);
102 }
103 
104 void cut(int x,int y)
105 {
106     makeroot(x);
107     cutf(y);
108 }//分离
109 
110 int ask(int x,int y)
111 {
112     makeroot(x);
113     access(y);
114     splay(y);
115     return sum[y];
116 }//查询值
117 
118 int find(int x)
119 {
120     access(x);
121     splay(x);
122     int y=x;
123     while(son[y][0]) y=son[y][0];
124     return y;
125 }//判断连通性(类LCA)
126 
127 int main()
128 {
129 
130     return 0;
131 }

 

posted @ 2018-06-23 11:35  咸鱼炒蒟蒻  阅读(129)  评论(0编辑  收藏  举报