xrlong

#include<bits/stdc++.h>
using namespace std;
#define pt puts("")
#define int long long
const int INF=LLONG_MAX/2;
inline int read(){
    int x=0;char s=getchar();bool f=true;
    for(;s<'0'||'9'<s;s=getchar()) if(s=='-') f=false;
    for(;'0'<=s&&s<='9';s=getchar()) x=(x<<1)+(x<<3)+(s^48);
    return f?x:~x+1;
}
#define read read()
template<typename T> 
inline void write(T x){
	static T st[45];T top=0;if(x<0)x=~x+1,putchar('-');
	do{st[top++]=x%10;}while(x/=10);while(top)putchar(st[--top]^48);
}
const int N=1e5+10;
namespace splay{
    int root,tot;
    struct splay{
        int fa,ch[2],val,cnt,size;
        #define fa(x) splay[x].fa
        #define lc(x) splay[x].ch[0]
        #define rc(x) splay[x].ch[1]
        #define val(x) splay[x].val
        #define cnt(x) splay[x].cnt
        #define ch(x,y) splay[x].ch[y]
        #define size(x) splay[x].size
    }splay[N];
    inline void pushup(int x){
        size(x)=size(lc(x))+size(rc(x))+cnt(x);
    }
    inline bool checkson(int x){
        return (x==rc(fa(x)));
    }
    inline void rotate(int x){
        int y=fa(x),z=fa(y);bool chk=checkson(x);
        if(z) ch(z,checkson(y))=x;
        fa(x)=z;
        ch(y,chk)=ch(x,chk^1),fa(ch(x,chk^1))=y;
        ch(x,chk^1)=y;fa(y)=x;
        pushup(y);pushup(x);
    }
    inline void Splay(int x,int goal=0){
        while(fa(x)!=goal){
            int y=fa(x),z=fa(y);
            if(z!=goal) rotate(checkson(x)==checkson(y)?y:x);
            rotate(x);
        }
        if(!goal) root=x;
    }
    inline void insert(int k){
        int x=root,f=0;
        while(x&&val(x)!=k) f=x,x=ch(x,val(x)<k);
        if(x) ++cnt(x);
        else{
            x=++tot;
            fa(x)=f;ch(x,0)=ch(x,1)=0;val(x)=k;cnt(x)=1;size(x)=1;
            if(f) ch(f,val(f)<k)=x;
        }
        Splay(x);
    }
    inline void find(int k){
        int x=root;
        while(ch(x,val(x)<k)&&val(x)!=k) x=ch(x,val(x)<k);
        Splay(x);
    }
    inline int pre_next(int k,bool flag){
        find(k);int x=root;
        if(val(x)<k&&!flag) return x;
        if(val(x)>k&&flag) return x;
        x=ch(x,flag);
        while(ch(x,flag^1)) x=ch(x,flag^1);
        return x;
    }
    inline int rnk(int k){
        int x=pre_next(k,0);
        Splay(x);
        return size(lc(x))+cnt(x)+1;
    }
    inline int kth(int k){
        int x=root;
        while(true){
            if(k<=size(lc(x))) x=lc(x);
            else if(k<=size(lc(x))+cnt(x)) return val(x);
            else k-=size(lc(x))+cnt(x),x=rc(x);
        }
    }
    inline void erase(int k){
        int pre=pre_next(k,0),next=pre_next(k,1);
        Splay(pre),Splay(next,pre);
        int x=ch(next,0);
        if(cnt(x)>1){
            --cnt(x);
            Splay(x);
        }
        else fa(x)=val(x)=size(x)=cnt(x)=lc(next)=0;
    }
}using namespace splay;
signed main(){
    #ifndef ONLINE_JUDGE
        freopen("in.in","r",stdin);
        freopen("out.out","w",stdout);
    #endif
    insert(-LLONG_MAX/2);
    insert(LLONG_MAX/2);
    int n=read;
    while(n--){
        int op=read,x=read;
        switch(op){
            case 1:insert(x);break;
            case 2:erase(x);break;
            case 3:write(rnk(x)-1);pt;break;
            case 4:write(kth(x+1));pt;break;
            case 5:write(splay::splay[pre_next(x,0)].val);pt;break;
            case 6:write(splay::splay[pre_next(x,1)].val);pt;break;
        }
    }
}

posted @ 2024-04-18 07:49  CuFeO4  阅读(18)  评论(0编辑  收藏  举报