#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cstdio> using namespace std; #define MAXT 1000000 #define INF 0x3f3f3f3f struct node { int val,cnt,siz; node* fa,*ch[2]; node(){} node(int a,int b,int c) { val=a;cnt=b;siz=c; } void update() { siz=ch[1]->siz+ch[0]->siz+cnt; } }; node nil_node(0,0,0),*nil=&nil_node; struct Splay_tree { node *root; int ncnt; node E[MAXT]; queue<int> Q; Splay_tree() { root=nil; ncnt=0; int i; for (i=1;i<MAXT;i++) { Q.push(i); } } node* new_node(int key) { int now=Q.front(); Q.pop(); E[now].val=key; E[now].cnt=E[now].siz=1; E[now].ch[0]=E[now].ch[1]=nil; return &E[now]; } void rotate(node *now,int pp) { node *y=now->fa; y->ch[!pp]=now->ch[pp]; if (now->ch[pp]!=nil)now->ch[pp]->fa=y; now->fa=y->fa; if (y->fa!=nil)/**/ { if (y->fa->ch[0]==y) { y->fa->ch[0]=now; }else { y->fa->ch[1]=now; } } y->fa=now; now->ch[pp]=y; y->update(); now->update();/**/ } void Splay(node* now,node *top) { if (now==top||now==nil)return; node *y; while (now->fa!=top) { y=now->fa; if (now==y->ch[0]) { if (y->fa!=top&&y==y->fa->ch[0])rotate(now,1); rotate(now,1); }else { if (y->fa!=top&&y==y->fa->ch[1])rotate(now,0); rotate(now,0); } } if (top==nil) { root=now; } } void Insert(int key) { node* now,*x; now=root; if (root==nil) { root=new_node(key); x=root; root->fa=nil; return ; } while(1) { now->siz++; if (now->val==key) { x=now;/**/ now->cnt++; break; } if (key<now->val) { if (now->ch[0]==nil) { now->ch[0]=new_node(key); now->ch[0]->fa=now; x=now->ch[0]; break; }else { now=now->ch[0]; continue; } } if (key>now->val) { if (now->ch[1]==nil) { now->ch[1]=new_node(key); now->ch[1]->fa=now; x=now->ch[1]; break; }else { now=now->ch[1]; continue; } } } Splay(x,nil); } void Delete(node *now) { if (now==nil) { throw "fuck"; } if (now->cnt>1) { now->siz--; now->cnt--; while (now!=root) { now=now->fa; now->siz--; } return ; } Splay(now,nil); if (now->ch[0]==nil) { root=now->ch[1]; now->ch[1]->fa=nil; return ; } if (now->ch[1]==nil) { root=now->ch[0]; now->ch[0]->fa=nil; return ; } Splay(get_min(now->ch[0]),root); Splay(get_min(now->ch[1]),root); now->ch[1]->ch[0]=now->ch[0]; now->ch[0]->fa=now->ch[1]; now->ch[1]->fa=nil; root=now->ch[1]; root->update(); } node* get_min(node* now) { if (now==nil)return now; while (now->ch[0]!=nil)now=now->ch[0]; return now; } node *search(int key) { node *now; now=root; while (1) { if (now->val==key) { return now; } if (key<now->val) { now=now->ch[0]; }else { now=now->ch[1]; } } return nil; } int get_rank(int key) { Splay(search(key),nil); return root->ch[0]->siz+1; } int get_val(node *now,int rank) { if (rank<=now->ch[0]->siz) { return get_val(now->ch[0],rank); } if (rank>now->ch[0]->siz+now->cnt) { return get_val(now->ch[1],rank-now->ch[0]->siz-now->cnt); } return now->val; } int prev(node *now,int key) { int ret=-INF; if (now==nil)return -INF; if (key>now->val) { return max(prev(now->ch[1],key),now->val); } if (key<=now->val) { return prev(now->ch[0],key); } } int next(node *now,int key) { if (now==nil)return INF; if (key<now->val) { return min(next(now->ch[0],key),now->val); } if (key>=now->val) { return next(now->ch[1],key); } } void Scan(node* now) { if (now==nil) { return ; } if (now->ch[0]!=nil && now->ch[0]->fa!=now)cout<<"Error_a"; if (now->ch[1]!=nil && now->ch[1]->fa!=now)cout<< "Error_b"; if (now->siz!=now->ch[0]->siz+now->ch[1]->siz+now->cnt)cout<<"Error_c"; Scan(now->ch[0]); printf("%d[%d] ",now->val,now->cnt); Scan(now->ch[1]); } }spt; int n,m; int main() { // freopen("input.txt","r",stdin); // freopen("output1.txt","w",stdout); int i,x,opt; scanf("%d",&m); for (i=0;i<m;i++) { scanf("%d%d",&opt,&x); switch (opt) { case 1: spt.Insert(x); break; case 2: spt.Delete(spt.search(x)); break; case 3: printf("%d\n",spt.get_rank(x)); break; case 4: printf("%d\n",spt.get_val(spt.root,x)); break; case 5: printf("%d\n",spt.prev(spt.root,x)); break; case 6: printf("%d\n",spt.next(spt.root,x)); break; } // spt.Scan(spt.root);cout<<endl; } return 0; }
转载于:https://www.cnblogs.com/mhy12345/p/3793658.html