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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> using namespace std;
#define re #define int long long
class Quick_Input_Output{ private: static const int S=1<<21; #define tc() (A==B&&(B=(A=Rd)+fread(Rd,1,S,stdin),A==B)?EOF:*A++) char Rd[S],*A,*B; #define pc putchar public: #undef gc #define gc getchar inline int read(){ int res=0,f=1;char ch=gc(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=gc();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=gc(); return res*f; } inline void write(int x){ if(x<0) pc('-'),x=-x; if(x<10) pc(x+'0'); else write(x/10),pc(x%10+'0'); } #undef gc #undef pc }I; #define File freopen("tmp.in","r",stdin);freopen("tmp.out","w",stdout);
class Splay{
public: int root,tot; struct Tree{ int fa,ch[2],val,cnt,size; }tr[100010]; inline void upd(int x){ tr[x].size=tr[tr[x].ch[0]].size+tr[tr[x].ch[1]].size+tr[x].cnt; } inline void rotate(int x){ re int y=tr[x].fa,z=tr[y].fa,k=tr[y].ch[1]==x; tr[z].ch[tr[z].ch[1]==y]=x; tr[x].fa=z; tr[y].ch[k]=tr[x].ch[k^1]; tr[tr[x].ch[k^1]].fa=y; tr[x].ch[k^1]=y; tr[y].fa=x; upd(y);upd(x); } inline void splay(int x,int rt){ while(tr[x].fa!=rt){ re int y=tr[x].fa,z=tr[y].fa; if(z!=rt) (tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y); rotate(x); } if(rt==0) root=x; } inline void find(int x){ re int u=root; if(!u) return ; while(tr[u].ch[x>tr[u].val]&&x!=tr[u].val) u=tr[u].ch[x>tr[u].val]; splay(u,0); } inline void insert(int x){ re int u=root,ff=0; while(u&&tr[u].val!=x){ ff=u; u=tr[u].ch[x>tr[u].val]; } if(u) tr[u].cnt++; else{ u=++tot; if(ff) tr[ff].ch[x>tr[ff].val]=u; tr[u].ch[0]=tr[u].ch[1]=0; tr[u].fa=ff; tr[u].val=x; tr[u].cnt=1; tr[u].size=1; } splay(u,0); } inline int pre(int x){ find(x); re int u=root; if(tr[u].val<x) return u; u=tr[u].ch[0]; while(tr[u].ch[1]) u=tr[u].ch[1]; return u; } inline int nxt(int x){ find(x); re int u=root; if(tr[u].val>x) return u; u=tr[u].ch[1]; while(tr[u].ch[0]) u=tr[u].ch[0]; return u; } inline void del(int x){ re int Pre=pre(x),Nxt=nxt(x); splay(Pre,0);splay(Nxt,Pre); re int Del=tr[Nxt].ch[0];
if(tr[Del].cnt>1) tr[Del].cnt--,splay(Del,0); else tr[Nxt].ch[0]=0; } inline int rank(int x){ re int u=root; if(tr[u].size<x) return 0; while(1){ re int y=tr[u].ch[0]; if(x>tr[y].size+tr[u].cnt) x-=tr[y].size+tr[u].cnt,u=tr[u].ch[1]; else if(tr[y].size>=x) u=y; else return tr[u].val; } } inline int Rank(int x){ find(x); return tr[tr[root].ch[0]].size; } inline void PrintSplay(){ cout<<"Now Root = "<<root<<endl; for(int i=1;i<=tot;i++){ cout<<"Node id:"<<i<<" Fa:"<<tr[i].fa<<" CHL:"<<tr[i].ch[0]<<" CHR:"<<tr[i].ch[1]<<" "<<tr[i].cnt<<" "<<tr[i].val<<" sz:"<<tr[i].size<<endl; } } }T; int n; signed main(){
T.root=0;T.tot=0; T.insert(-2147483647); T.insert(2147483647); n=I.read(); for(int op,x,i=1;i<=n;i++){ op=I.read();x=I.read(); if(op==1){ T.insert(x); }else if(op==2){ T.del(x); }else if(op==3){ I.write(T.Rank(x));putchar('\n'); }else if(op==4){ I.write(T.rank(x+1));putchar('\n'); }else if(op==5){ I.write(T.tr[T.pre(x)].val);putchar('\n'); }else if(op==6){ I.write(T.tr[T.nxt(x)].val);putchar('\n'); }
} }
|