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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #include<bits/stdc++.h> using namespace std; constexpr int N = 5e5+5;
template <typename T> void read(T &x){x=0; T f(0); char ch=getchar(); while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48); ch=getchar();} x=f?-x:x;} template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){read(x);read(arg...);} template <typename T> inline void write(T x){static char buf[64]; static int tot(0); if(x<0) putchar('-'),x=-x; do buf[++tot]=(x%10)+48,x/=10; while(x); do putchar(buf[tot--]); while(tot);} template <typename T> void write(T x,char c){static char buf[64]; static int tot(0); if(x<0) putchar('-'),x=-x; do buf[++tot]=(x%10)+48,x/=10; while(x); do putchar(buf[tot--]); while(tot); putchar(c);}
class Splay{ #define val(x) t[x].val #define ls(x) t[x].ch[0] #define rs(x) t[x].ch[1] #define son(x,nxt) t[x].ch[nxt] #define fa(x) t[x].fa #define cnt(x) t[x].cnt #define siz(x) t[x].siz private: struct node{ int val,fa,ch[2],siz,cnt; }t[N]; int root,tot; public: bool which(int x){ return rs(fa(x))==x; } void pushup(int x){ siz(x)=siz(ls(x))+siz(rs(x))+cnt(x); } void connect(int x,int y,int nxt){ son(y,nxt)=x; fa(x)=y; } void rotate(int x){ int y=fa(x),z=fa(y); int fx=which(x),fy=which(y); connect(son(x,fx^1),y,fx); connect(y,x,fx^1); connect(x,z,fy); pushup(y); pushup(x); } void splay(int x,int y){ y=fa(y); while(fa(x)!=y){ if(fa(fa(x))==y) rotate(x); else if(which(x)==which(fa(x))) rotate(fa(x)), rotate(x); else rotate(x), rotate(x); } if(y==0){ root=x; connect(x,0,1); } } void newPoint(int val,int fa,int nxt){ tot++; fa(tot)=fa; cnt(tot)=siz(tot)=1; val(tot)=val; son(fa,nxt)=tot; } void insert(int val){ if(root==0){ newPoint(val,0,1); root=tot; return; } int now=root; while(1){ siz(now)++; if(val(now)==val){ cnt(now)++; splay(now,root); return; } int nxt=val(now)<val, son=son(now,nxt); if(!son){ newPoint(val,now,nxt); splay(tot,root); return; } now=son; } } int find(int val){ int now=root; while(1){ if(!now) return 0; if(val(now)==val){ splay(now,root); return now; } int nxt=val(now)<val, son=son(now,nxt); now=son; } } void delet(int val){ int now=find(val); if(!now) return; if(cnt(now)>1){ cnt(now)--; siz(now)--; return; } if(!ls(now) && !rs(now)){ root=0; } else if(!ls(now)){ root=rs(root); fa(root)=0; } else if(!rs(now)){ root=ls(root); fa(root)=0; } else{ int pos=ls(now); while(rs(pos)) pos=rs(pos); splay(pos,root); connect(rs(now),pos,1); pushup(pos); } } int rnk(int val){ int now=root,s=0; while(now){ if(val(now)==val){ splay(now,root); return siz(ls(now))+1; } if(val(now)<val){ s+=siz(ls(now))+cnt(now); now=rs(now); } else{ now=ls(now); } } return s+1; } int find_k(int k){ int now=root; while(1){ int used=siz(now)-siz(rs(now)); if(k>siz(ls(now)) && k<=used){ break; } if(k>=used){ k-=used; now=rs(now); } else{ now=ls(now); } } splay(now,root); return val(now); } int lower(int val){ int ans=-2147483647; int now=root; while(now){ if(val(now)<val && val(now)>ans){ ans=val(now); } if(val>val(now)){ now=rs(now); } else{ now=ls(now); } } return ans; } int upper(int val){ int ans=2147483647; int now=root; while(now){ if(val(now)>val && val(now)<ans){ ans=val(now); } if(val<val(now)){ now=ls(now); } else{ now=rs(now); } } return ans; } }tr;
int n,opt,val;
int main(){ read(n); while(n--){ read(opt,val); int ans; switch(opt){ case 1:{ tr.insert(val); break; } case 2:{ tr.delet(val); break; } case 3:{ ans=tr.rnk(val); break; } case 4:{ ans=tr.find_k(val); break; } case 5:{ ans=tr.lower(val); break; } case 6:{ ans=tr.upper(val); break; } } if(opt>2) write(ans,'\n'); } }
|