【模板】splay
来源:互联网 发布:java跨域上传图片 编辑:程序博客网 时间:2024/05/19 03:20
#include<iostream>#include<cstdio>#define MAXN 100000 #define INF 0x7ffffffusing namespace std;int n;int ch[MAXN][2],use[MAXN],fa[MAXN];int size=0,root=0;inline void rotate(int x,int &k){ int y=fa[x],z=fa[y],d; if(ch[y][0]==x)d=0;else d=1; if(y==k)k=x; else { if(ch[z][0]==y)ch[z][0]=x; else ch[z][1]=x; } fa[y]=x,fa[x]=z,fa[ch[x][d^1]]=y; ch[y][d]=ch[x][d^1],ch[x][d^1]=y;}void splay(int x,int &k){ int y,z; while(x!=k) { y=fa[x],z=fa[y]; if(y!=k) { if((ch[y][0]==x)^(ch[z][0]==y))rotate(x,k); else rotate(y,k); } rotate(x,k); }}void insert(int &x,int num,int pre){ if(x==0){x=++size,use[x]=num,fa[x]=pre,splay(x,root);return ;} if(num<use[x])insert(ch[x][0],num,x); else insert(ch[x][1],num,x);}int find(int x,int num){ if(use[x]==num)return x; if(num<use[x])return find(ch[x][0],num); else return find(ch[x][1],num); }void del(int x){ splay(x,root); if(ch[x][0]*ch[x][1]==0)root=ch[x][0]+ch[x][1]; else { int t=ch[x][1]; while(ch[t][0]!=0)t=ch[t][0]; fa[ch[x][0]]=t;ch[t][0]=ch[x][0]; root=ch[x][1]; } fa[root]=0;}void print(int x){ if(x==0)return ; print(ch[x][0]); printf("%d ",use[x]); print(ch[x][1]);}int main(){ scanf("%d",&n); int t1,t2; for(int i=1;i<=n;i++) { scanf("%d",&t1); insert(root,t1,0); } print(root); while(cin>>t1) { t2=find(root,t1); printf("%d\n",t2); if(t2)del(t2); print(root); } while(1); return 0; }
0 0
- SPLAY模板
- (模板)splay
- splay模板
- splay 模板
- splay 模板
- 【模板】splay
- splay模板
- splay模板
- Splay模板
- Splay模板
- splay 模板
- Splay模板
- 【模板】Splay
- splay模板
- Splay模板
- Splay模板
- Splay模板
- 【Splay模板】
- warning: could not load any Objective-C class information. This will significantly reduce the qualit
- linux与windows下文件转换
- 有关冲突域的定义
- 动态添加一个布局
- iOS 字典与字符串的相互转换
- 【模板】splay
- iOS Tabbar和Naviagtion
- TestDisk使用教程
- JFinal学习(一)
- solution Of Pat 1110. Complete Binary Tree (25)
- 在用信鸽推送JPUSH2.1.0的时候报错
- Ubuntu平台融合(Convergence)演进
- Erlang supervisor规格的dynamic行为分析
- 翻翻git之---RecycleView的上拉,下拉刷新,样式切换,添加foot和header的强大库 RecyclerViewManager