splay树(2020.7.7)

POJ3418

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<vector>
  6 using namespace std;
  7 #define maxn 1100000
  8 #define mem(a,b) memset(a,b,sizeof(a))
  9 #define root10 ch[ch[root][1]][0]
 10 #define root1 ch[root][1]
 11 int size[maxn];
 12 int ch[maxn][2];
 13 int pre[maxn];
 14 int root,tot;
 15 int key[maxn];
 16 int val[maxn];
 17 int n;
 18 int pos;
 19 void init()
 20 {
 21 }
 22 void newnode(int &x,int k,int p,int father)
 23 {
 24     x=++tot;
 25     pre[x]=father;
 26     size[x]=1;
 27     ch[x][0]=ch[x][1]=0;
 28     key[x]=p;
 29     val[x]=k;
 30 }
 31 void push_down(int x)
 32 {
 33 }
 34 void push_up(int x)
 35 {
 36 }
 37 void rot(int x,int kind)
 38 {
 39     int y=pre[x];
 40     push_down(y);
 41     push_down(x);
 42     ch[y][!kind]=ch[x][kind];
 43     pre[ch[x][kind]]=y;
 44     if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
 45     pre[x]=pre[y];
 46     ch[x][kind]=y;
 47     pre[y]=x;
 48     push_up(y);
 49     push_up(x);
 50 }
 51 void splay(int x,int goal)
 52 {
 53     push_down(x);
 54     while(pre[x]!=goal)
 55     {
 56         if(pre[pre[x]]==goal)
 57         {
 58             push_down(pre[x]);
 59             push_down(x);
 60             rot(x,ch[pre[x]][0]==x);
 61         }
 62         else
 63         {
 64             int y=pre[x];
 65             push_down(pre[y]);
 66             push_down(y);
 67             push_down(x);
 68             int kind=ch[pre[y]][0]==y;
 69             if(ch[y][kind]==x)
 70             {
 71                 rot(x,!kind);
 72                 rot(x,kind);
 73             }
 74             else
 75             {
 76                 rot(y,kind);
 77                 rot(x,kind);
 78             }
 79         }
 80     }
 81     push_up(x);
 82     if(goal==0)root=x;
 83 }
 84  
 85 void insert(int k,int p)
 86 {
 87     int rt=root;
 88     int r=root;
 89     while(rt!=0)
 90     {
 91         r=rt;
 92         if(key[rt]<p)rt=ch[rt][1];
 93         else rt=ch[rt][0];
 94         //cout<<r<<" "<<rt<<endl;
 95     }
 96     newnode(ch[r][key[r]<p],k,p,r);
 97     splay(ch[r][key[r]<p],root);
 98     ch[0][0]=ch[0][1]=0;
 99 }
100  
101 int get_min(int rt)
102 {
103     while(ch[rt][0])rt=ch[rt][0];
104     if(rt==root)root=ch[rt][1];
105     ch[pre[rt]][0]=ch[rt][1];
106     pre[ch[rt][1]]=pre[rt];
107     return val[rt];
108 }
109 int get_max(int rt)
110 {
111     while(ch[rt][1])rt=ch[rt][1];
112     if(rt==root)root=ch[rt][0];
113     ch[pre[rt]][1]=ch[rt][0];
114     pre[ch[rt][0]]=pre[rt];
115     return val[rt];
116 }
117 int main()
118 {
119     root=tot=0;
120     int op,k,p;
121     while(~scanf("%d",&op)&&(op))
122     {
123  
124         if(op==1)
125         {
126             scanf("%d%d",&k,&p);
127             insert(k,p);
128         }
129         if(op==2)
130         {
131             printf("%d\n",get_max(root));
132         }
133         if(op==3)
134         {
135             printf("%d\n",get_min(root));
136         }
137     }
138     return 0;
139 }

 

posted @ 2020-07-07 20:54  莫得感情的约德尔人  阅读(105)  评论(0)    收藏  举报