写在前面:
由我们可爱的Daniel Sleator和Robert Tarjan提出的一种数据结构,平衡树的一种,本质是二叉树。
至于到底是splay还是spaly,我认为可能splay更对一些
毕竟splay是有实意的单词,更有可能一点。而且WIKI百科页也是splay
以下是本人学习splay的一点过程,请多指教喽

SPLAY

 

 那么我在这里复习整理一下spaly的代码相关吧

例题:http://www.lydsy.com/JudgeOnline/problem.php?id=3224

参考博客:http://blog.csdn.net/clove_unique/article/details/50636361

  1 #include<cstdio>
  2 #define maxn 500100
  3 using namespace std;
  4 int root,N,tot;
  5 inline int read(){
  6     register int x=0,t=1;
  7     register char ch=getchar();
  8     while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  9     if(ch=='-'){t=-1;ch=getchar();}
 10     while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
 11     return x*t;
 12 }
 13 struct node{
 14     int ch[2],ff,cnt,val,sum;
 15 }t[maxn];
 16 void pushup(int u){
 17     t[u].sum=t[t[u].ch[0]].sum+t[t[u].ch[1]].sum+t[u].cnt;
 18 }
 19 void rotate(int x){
 20     register int y=t[x].ff;
 21     register int z=t[y].ff;
 22     register int k=t[y].ch[1]==x;
 23     t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
 24     t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
 25     t[x].ch[k^1]=y;t[y].ff=x;
 26     pushup(y),pushup(x);
 27 }
 28 void splay(int x,int goal){
 29     while(t[x].ff!=goal){
 30         int y=t[x].ff;
 31         int z=t[y].ff;
 32         if(z!=goal)
 33             (t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);
 34         rotate(x);
 35     }
 36     if(goal==0)
 37         root=x;
 38 }
 39 void insert(int x){
 40     int u=root,ff=0;
 41     while(u&&t[u].val!=x){
 42         ff=u;
 43         u=t[u].ch[x>t[u].val];
 44     }
 45     if(u)
 46         t[u].cnt++;
 47     else{
 48         u=++tot;
 49         if(ff)
 50             t[ff].ch[x>t[ff].val]=u;
 51         t[tot].ch[0]=0;
 52         t[tot].ch[1]=0;
 53         t[tot].ff=ff;t[tot].val=x;
 54         t[tot].cnt=t[tot].sum=1;
 55     }
 56     splay(u,0);
 57 }
 58 void find(int x){
 59     int u=root;
 60     if(!u)return;
 61     while(t[u].ch[x>t[u].val]&&x!=t[u].val)
 62         u=t[u].ch[x>t[u].val];
 63     splay(u,0);
 64 }
 65 int next(int x,int f){
 66     find(x);
 67     int u=root;
 68     if((t[u].val>x&&f)||(t[u].val<x&&!f))return u;
 69     u=t[u].ch[f];
 70     while(t[u].ch[f^1])u=t[u].ch[f^1];
 71     return u;
 72 }
 73 void del(int x){
 74     int la=next(x,0);
 75     int ne=next(x,1);
 76     splay(la,0),splay(ne,la);
 77     int d=t[ne].ch[0];
 78     if(t[d].cnt>1){
 79         t[d].cnt--;
 80         splay(d,0);
 81     }
 82     else
 83         t[ne].ch[0]=0;
 84 }
 85 int K_th(int x){
 86     int u=root;
 87     if(t[u].sum<x)
 88         return 0;
 89     while(1){
 90         int y=t[u].ch[0];
 91         if(x>t[y].sum+t[u].cnt){
 92             x-=t[y].sum+t[u].cnt;
 93             u=t[u].ch[1];
 94         }
 95         else if(t[y].sum>=x)
 96             u=y;
 97         else
 98             return t[u].val;
 99     }
100 }
101 int main(){
102     insert(-2147483647);
103     insert(+2147483647);
104     N=read();
105     while(N--){
106         int opt=read();
107         if(opt==1)insert(read());
108         else if(opt==2)del(read());
109         else if(opt==3){
110             find(read());
111             printf("%d\n",t[t[root].ch[0]].sum);
112         }
113         else if(opt==4)printf("%d\n",K_th(read()+1));
114         else if(opt==5)printf("%d\n",t[next(read(),0)].val);
115         else if(opt==6)printf("%d\n",t[next(read(),1)].val);
116     }
117     return 0;
118 }
View Code

相关文章:

  • 2021-09-19
  • 2021-08-26
  • 2021-07-24
  • 2021-10-27
猜你喜欢
  • 2022-12-23
  • 2021-11-25
  • 2021-12-19
  • 2022-12-23
  • 2021-10-23
  • 2021-07-21
  • 2021-06-09
相关资源
相似解决方案