rt,请求大佬帮我调我的屎山代码
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inline")
#define hh putchar('\n')
#define kg putchar(' ')
//#define int long long
//#define int __int128
namespace quickread{
template<typename T> void read(T &x){
x=0;char c=getchar();T neg=0;
while(!isdigit(c)) neg|=!(c^'-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(neg) x=(~x)+1;
}
template<typename T> void write(T x){
if(x<0) putchar('-'),x=~x+1;
if(x>9) write(x/10);
putchar(x%10^48);
}
inline string readstr(){
char ch=getchar();string str="";
while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){str+=ch;ch=getchar();}
return str;
}
inline char readchar(){
char ch=getchar();
while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
return ch;
}
}
using namespace quickread;
const int N=1e5+10,inf=2147483647;
mt19937 rnd(time(0));
int n,root=1;
namespace Treap{
int cnt=0;
struct node{
int ls,rs,fa;
int value,pri;
int cnt,siz;
node(){fa=ls=rs=0;value=pri=0;cnt=siz=0;}
void build(int val,int f){value=val;pri=rnd()%inf;cnt=siz=1;fa=f;}
};
node treap[N];
void update(int p){treap[p].siz=treap[p].cnt+treap[treap[p].ls].siz+treap[treap[p].rs].siz;}
void zig(int p){//右儿子上
int k=treap[p].rs;
if(p==1) root=k;
treap[p].rs=treap[k].ls,treap[treap[k].ls].fa=p;
treap[k].ls=p,treap[p].fa=k;
update(p),update(k);
}
void zag(int p){//左儿子上
int k=treap[p].ls;
if(p==1) root=k;
treap[p].ls=treap[k].rs,treap[treap[k].rs].fa=p;
treap[k].rs=p,treap[p].fa=k;
update(p),update(k);
}
void _insert(int p,int value){//opt==1
if(!cnt) treap[++cnt].build(p,value);
else if(value==treap[p].value) treap[p].cnt++;
else if(value<treap[p].value){
if(!treap[p].ls) _insert(treap[p].ls,value);
else treap[++cnt].build(value,p),treap[p].ls=cnt;
if(treap[treap[p].ls].pri<treap[p].pri) zag(p);
}else{
if(!treap[p].rs) _insert(treap[p].rs,value);
else treap[++cnt].build(value,p),treap[p].rs=cnt;
if(treap[treap[p].rs].pri<treap[p].pri) zig(p);
}
update(p);
}
void _delete(int p,int value){//opt==2
if(value>treap[p].value) _delete(treap[p].rs,value);
else if(value<treap[p].value) _delete(treap[p].ls,value);
else {
if(treap[p].cnt>1) treap[p].cnt--;
else {
if(!treap[p].ls&&!treap[p].rs){
int k=treap[p].fa;
if(treap[k].ls==p) treap[k].ls=0;
else treap[k].rs=0;
treap[p].fa=treap[p].ls=treap[p].rs=0;
treap[p].value=treap[p].pri=0;
treap[p].cnt=treap[p].siz=0;
}else if(!treap[p].ls&&treap[p].rs){
int k=treap[p].fa;
if(treap[k].ls==p) treap[k].ls=treap[p].rs;
else treap[k].rs=treap[p].rs;
treap[treap[p].rs].fa=k;
treap[p].fa=treap[p].ls=treap[p].rs=0;
treap[p].value=treap[p].pri=0;
treap[p].cnt=treap[p].siz=0;
}else if(treap[p].ls&&!treap[p].rs){
int k=treap[p].fa;
if(treap[k].ls==p) treap[k].ls=treap[p].ls;
else treap[k].rs=treap[p].ls;
treap[treap[p].ls].fa=k;
treap[p].fa=treap[p].ls=treap[p].rs=0;
treap[p].value=treap[p].pri=0;
treap[p].cnt=treap[p].siz=0;
}else{
int k=0;
if(treap[treap[p].ls].pri>treap[treap[p].rs].pri) k=1;
if(!k) zag(p),_delete(p,value);else zig(p),_delete(p,value);
}
}
}
update(p);
}
int ans=0;
int query_kth1(int p,int x){//opt==4
int ans1=1;
if(p==0) ans1=0;
else if(x<treap[p].value) ans1=query_kth1(treap[p].ls,x);
else if(x==treap[p].value) ans1=treap[treap[p].ls].siz+1;
else ans1=query_kth1(treap[p].rs,x)+1+treap[treap[p].ls].siz;
return ans1;
}
int query_kth2(int p,int x){//opt==3
int ans1=0;
if(p==0) ans1=0;
else if(x==treap[treap[p].ls].siz+1) ans1=treap[p].value;
else if(x>treap[treap[p].ls].siz+1) ans1=query_kth2(treap[p].rs,x-treap[treap[p].ls].siz-1);
else ans1=query_kth2(treap[p].ls,x);
return ans1;
}
void query_pre(int p,int x){//opt==5
if(p==0) return;
if(x>treap[p].value){
ans=max(ans,treap[p].value);
query_pre(treap[p].rs,x);
}else query_pre(treap[p].ls,x);
return;
}
void query_nex(int p,int x){//opt==6
if(p==0) return;
if(x<treap[p].value){
ans=min(ans,treap[p].value);
query_nex(treap[p].ls,x);
}else query_nex(treap[p].rs,x);
return;
}
void debug(int p){
cout<<"treap["<<p<<"].value="<<treap[p].value<< " ";
cout<<"treap["<<p<<"].size="<<treap[p].siz<<" ";
cout<<"treap["<<p<<"].ls="<<treap[p].ls<<" ";
cout<<"treap["<<p<<"].rs="<<treap[p].rs<<" ";
cout<<"\n";
if(treap[p].ls!=-1) debug(treap[p].ls);
if(treap[p].rs!=-1) debug(treap[p].rs);
}
}
using namespace Treap;
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
read(n);
for(int i=1;i<=n;i++){
int opt,x;read(opt),read(x);
if(opt==1) _insert(root,x);
else if(opt==2) _delete(root,x);
else if(opt==3) write(query_kth2(root,x)),hh;
else if(opt==4) write(query_kth1(root,x)),hh;
else if(opt==5) ans=-inf,query_pre(root,x),write(ans),hh;
else ans=inf,query_nex(root,x),write(ans),hh;
}
return 0;
}