1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include<iostream> #include<cstdio> #include<cstdlib> #define inf (1<<30) #define maxn (2100000) #define db double #define il inline #define RG register using namespace std;
const db al=0.75; struct node { int son[2],fa,size,num; } t[maxn]; int n,cnt,root;
il bool balance(RG int id) { return (db)(t[id].size*al>=(db)t[ t[id].son[0] ].size) && (db)( t[id].size*al>=(db)t[t[ id].son[1] ].size); } int cur[maxn],sum;
il void recycle(RG int id) { if(t[id].son[0]) recycle(t[id].son[0]); cur[++sum]=id; if(t[id].son[1]) recycle(t[id].son[1]); }
il int build(RG int l,RG int r) { if(l>r) return 0; RG int mid=(l+r)>>1,id=cur[mid]; t[ t[id].son[0]=build(l,mid-1) ].fa=id; t[ t[id].son[1]=build(mid+1,r) ].fa=id; t[id].size=t[ t[id].son[0] ].size+t[ t[id].son[1] ].size+1; return id; }
il void rebuild(RG int id) { sum=0; recycle(id); RG int fa=t[id].fa,Son=( t[ t[id].fa ].son[1]==id ); RG int cur=build(1,sum); t[ t[fa].son[Son]=cur ].fa=fa; if(id==root) root=cur; }
il void insert(RG int x) { RG int now=root,cur=++cnt; t[cur].size=1,t[cur].num=x; while(1) { t[now].size++; RG bool Son=(x>=t[now].num); if( t[now].son[Son] ) now=t[now].son[Son]; else { t[ t[now].son[Son]=cur ].fa=now; break; } } RG int flag=0; for(RG int i=cur; i; i=t[i].fa) if(!balance(i)) flag=i; if(flag) rebuild(flag); }
il int get_num(RG int x) { RG int now=root; while(1) { if(t[now].num==x) return now; else now=t[now].son[ t[now].num<x ]; } }
il void erase(RG int id) { if(t[id].son[0] && t[id].son[1]) { RG int cur=t[id].son[0]; while(t[cur].son[1]) cur=t[cur].son[1]; t[id].num=t[cur].num; id=cur; } RG int Son=(t[id].son[0]) ? t[id].son[0]:t[id].son[1]; RG int k=( t[ t[id].fa ].son[1]==id ); t[ t[ t[id].fa ].son[k]=Son ].fa=t[id].fa; for(RG int i=t[id].fa; i; i=t[i].fa) t[i].size--; if(id==root) root=Son; }
il int get_rank(RG int x) { RG int now=root,ans=0; while(now) { if(t[now].num<x) ans+=t[ t[now].son[0] ].size+1,now=t[now].son[1]; else now=t[now].son[0]; } return ans; }
il int get_kth(RG int x) { RG int now=root; while(1) { if(t[ t[now].son[0] ].size==x-1) return now; else if(t[ t[now].son[0] ].size>=x) now=t[now].son[0]; else x-=t[ t[now].son[0] ].size+1,now=t[now].son[1]; } return now; } int main() { cnt=2,root=1; t[1].num=-inf,t[1].size=2,t[1].son[1]=2; t[2].num=inf,t[2].size=1,t[2].fa=1; scanf("%d",&n); RG int type,x; for(RG int i=1; i<=n; i++) { scanf("%d%d",&type,&x); if(type==1) insert(x); else if(type==2) erase( get_num(x) ); else if(type==3) printf("%d\n",get_rank(x)); else if(type==4) printf("%d\n",t[ get_kth(x+1) ].num); else if(type==5) printf("%d\n",t[get_kth(get_rank(x))].num); else if(type==6) printf("%d\n",t[get_kth(get_rank(x+1)+1)].num); } return 0; }
|