• 我终于会手打lct了!


     1 #include<iostream>
     2 #include<cstdio>
     3 #define MAXN 100010
     4 using namespace std;
     5 int read()
     6 {
     7     int x=0,t=1,c;
     8     while(!isdigit(c=getchar()))if(c=='-')t=-1;
     9     while(isdigit(c))x=x*10+c-'0',c=getchar();
    10     return x*t;
    11 }
    12 int ch[MAXN][2],fa[MAXN];
    13 int size[MAXN];
    14 bool rev[MAXN];
    15 bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
    16 void pushdown(int x)
    17 {
    18     if(rev[x])
    19     {
    20         rev[x]^=1;if(ch[x][0])rev[ch[x][0]]^=1;if(ch[x][1])rev[ch[x][1]]^=1;
    21         swap(ch[x][0],ch[x][1]);
    22     }
    23 }
    24 void maintain(int x)
    25 {
    26     size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    27 }
    28 void rotate(int x)
    29 {
    30     int d=(ch[fa[x]][1]==x),y=fa[x],z=fa[y];
    31     pushdown(y);pushdown(x);
    32     if(!isroot(y))ch[z][ch[z][1]==y]=x;
    33     fa[x]=z;
    34     ch[y][d]=ch[x][d^1];
    35     if(ch[x][d^1])fa[ch[x][d^1]]=y;
    36     ch[x][d^1]=y;
    37     fa[y]=x;
    38     maintain(y);maintain(x);
    39 }
    40 int Q[MAXN],cntQ=0;
    41 void splay(int x)
    42 {
    43     while(!isroot(x))
    44     {
    45         int y=fa[x],z=fa[fa[x]];
    46         if(!isroot(fa[x]))
    47         {
    48             if(ch[y][1]==x^ch[z][1]==y)rotate(x);
    49             else rotate(y);
    50         }
    51         rotate(x);
    52     }
    53 }
    54 void access(int x)
    55 {
    56     for(int i=x;i;i=fa[i])Q[cntQ++]=i;
    57     while(cntQ--)pushdown(Q[cntQ]);
    58     for(int i=x,t=0;i;t=i,i=fa[i])splay(i),ch[i][1]=t,maintain(i);
    59 }
    60 void makeroot(int x)
    61 {
    62     access(x);splay(x);rev[x]^=1;
    63 }
    64 void split(int x,int y)
    65 {
    66     makeroot(x);access(y);splay(x);
    67 }
    68 void link(int x,int y)
    69 {
    70     access(y),splay(y),fa[y]=x;
    71 }
    72 void cut(int x,int y)
    73 {
    74     split(x,y);ch[x][1]=fa[y]=0;maintain(x);
    75 }
    76 int main()
    77 {
    78     char o[2];
    79     while(true)
    80     {
    81         scanf("%s",o);
    82         if(o[0]=='l')
    83         {
    84             int x=read(),y=read();
    85             link(x,y);
    86         }
    87         if(o[0]=='c')
    88         {
    89             int x=read(),y=read();
    90             cut(x,y);
    91         }
    92         if(o[0]=='q')
    93         {
    94             int x=read(),y=read();
    95             split(x,y);splay(x);
    96             printf("Size:%d
    ",size[x]);
    97         }
    98     }
    99 }

    最简单的模板,调了一个小时。。。各大神犇轻喷

  • 相关阅读:
    【LeetCode】Binary Tree Upside Down
    【LeetCode】171. Excel Sheet Column Number
    【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
    【Algorithm】回溯法与深度优先遍历的异同
    【C++】自定义比较函数小结
    【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
    【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
    【LeetCode】Add Two Numbers
    【LeetCode】5. Longest Palindromic Substring
    【LeetCode】6. ZigZag Conversion
  • 原文地址:https://www.cnblogs.com/lvzijian/p/5595940.html
Copyright © 2020-2023  润新知