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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=100005;
struct Tree { int child[2],father; int size,val; Tree() {} Tree(int l,int r,int f,int s,int v):father(f),size(s),val(v) { child[0]=l; child[1]=r; } };
struct ScapeGoat_Tree { const double alpha=0.7; Tree tree[maxn]; int root,size; #define ls(x) tree[x].child[0] #define rs(x) tree[x].child[1] #define fa(x) tree[x].father #define val(x) tree[x].val #define size(x) tree[x].size ScapeGoat_Tree() { tree[++size]=Tree(0,2,0,2,-INT_MAX); tree[++size]=Tree(0,0,1,1,INT_MAX); root=1; } bool balance(int index) { return size(index)*alpha>=max(size(ls(index)),size(rs(index))); } bool checkson(int index) { return rs(fa(index))==index; } void push_up(int index) { size(index)=size(ls(index))+1+size(rs(index)); } int cnt,a[maxn]; void dfs(int index) { if(ls(index))dfs(ls(index)); a[++cnt]=index; if(rs(index))dfs(rs(index)); } int build(int Left,int Right) { if(Left>Right)return 0; int mid=(Left+Right)>>1,pos=a[mid]; ls(pos)=build(Left,mid-1); rs(pos)=build(mid+1,Right); fa(ls(pos))=fa(rs(pos))=pos; push_up(pos); return pos; } void rebuild(int index) { cnt=0; dfs(index); int father=fa(index),side=checkson(index); int pos=build(1,cnt); tree[father].child[side]=pos; fa(pos)=father; if(index==root)root=pos; } void insert(int v) { int now=root,father=0; while(now) { father=now; size(father)++; now=tree[now].child[val(now)<=v]; } tree[now=++size]=Tree(0,0,father,1,v); if(father)tree[father].child[val(father)<=v]=now; int pos=0; for(int i=now; i; i=fa(i)) if(!balance(i))pos=i; if(pos)rebuild(pos); } void remove(int index) { if(ls(index)&&rs(index)) { int pre=ls(index); while(rs(pre))pre=rs(pre); val(index)=val(pre); index=pre; } int son=ls(index)?ls(index):rs(index),side=checkson(index); tree[fa(index)].child[side]=son; fa(son)=fa(index); for(int i=fa(index); i; i=fa(i))size(i)--; if(index==root)root=son; } int find(int v) { int now=root; while(now) { if(val(now)==v)return now; else now=tree[now].child[val(now)<v]; } return -1; } int rank(int v) { int now=root,ans=0; while(now) { if(v<=val(now))now=ls(now); else { ans+=size(ls(now))+1; now=rs(now); } } return ans; } int kth(int rank) { rank++; int now=root; while(now>0&&rank>=0) { if(ls(now)&&size(ls(now))>=rank)now=ls(now); else { if(rank<=size(ls(now))+1)return now; rank-=size(ls(now))+1; now=rs(now); } } return -1; } int pre(int num) { int now=root,ans=-INT_MAX; while(now) { if(val(now)<num)ans=max(ans,val(now)),now=rs(now); else now=ls(now); } return ans; } int suc(int num) { int now=root,ans=INT_MAX; while(now) { if(val(now)>num)ans=min(ans,val(now)),now=ls(now); else now=rs(now); } return ans; } } sgt;
int n;
int main() { n=Get_Int(); for(int i=1; i<=n; i++) { int order=Get_Int(); if(order==1)sgt.insert(Get_Int()); if(order==2)sgt.remove(sgt.find(Get_Int())); if(order==3)printf("%d\n",sgt.rank(Get_Int())); if(order==4)printf("%d\n",sgt.tree[sgt.kth(Get_Int())].val); if(order==5)printf("%d\n",sgt.pre(Get_Int())); if(order==6)printf("%d\n",sgt.suc(Get_Int())); } return 0; }
|