【码风清新】【轻微压行】splay20pts求助/kk
查看原帖
【码风清新】【轻微压行】splay20pts求助/kk
203008
YamadaRyou楼主2021/12/25 00:24

rt

AC了#1和#12,WA了#3,其他全T了(果然换了个名字后WA的次数就少了(大雾

写法较离谱,见谅

#include<stdio.h>
const int maxn=100001;
struct{
    int ch[2],f,v,cnt,sz;
}tree[maxn];int tot,root;
inline int newnode(int k){return tree[++tot].v=k,tree[tot].cnt=tree[tot].sz=1,tot;}
inline int getc(int x){return tree[tree[x].f].ch[1]==x;}
inline void pushup(int x){tree[x].sz=tree[tree[x].ch[0]].sz+tree[tree[x].ch[1]].sz+tree[x].cnt;}
inline void rotate(int x){
    int f=tree[x].f,gf=tree[f].f,d=getc(x);
    if(tree[f].f)tree[tree[f].f].ch[getc(f)]=x;tree[x].f=tree[f].f;
    if(tree[x].ch[d^1])tree[tree[x].ch[d^1]].f=f;tree[f].ch[d]=tree[x].ch[d^1];
    tree[f].f=x,tree[x].ch[d^1]=f;
    pushup(f),pushup(x);
}
inline void splay(int x,int to=0){
    for(;tree[x].f!=to;rotate(x))
        if(tree[tree[x].f].f!=to)
            rotate(getc(tree[x].f)^getc(x)?x:tree[x].f);
    if(!to)root=x;
}
inline void find(int k){
    int cur=root;
    for(;tree[cur].ch[tree[cur].v<k]&&tree[cur].v!=k;cur=tree[cur].ch[tree[cur].v<k]);
    splay(cur);
}
inline void ins(int k){
    if(!root)return (void)(root=newnode(k));
    find(k);
    if(tree[root].v<k){
        int x=newnode(k);
        if(tree[root].ch[1])tree[tree[root].ch[1]].f=x;tree[x].ch[1]=tree[root].ch[1];
        tree[root].ch[1]=0,tree[root].f=x;tree[x].ch[0]=root;
        root=x;
    }else if(tree[root].v>k){
        int x=newnode(k);
        if(tree[root].ch[0])tree[tree[root].ch[0]].f=x;tree[x].ch[0]=tree[root].ch[0];
        tree[root].ch[0]=0,tree[root].f=x;tree[x].ch[1]=root;
        root=x;
    }else ++tree[root].cnt,++tree[root].sz;
}
inline int rnk(int x){
    if(!root)return 1;
    find(x);
    return tree[root].v>=x?tree[tree[root].ch[0]].sz+1:tree[tree[root].ch[0]].sz+2;
}
inline void kth(int k){
    if(k<1)k=1;if(k>tree[root].sz)k=tree[root].sz;
    int cur=root;
    for(;;){
        if(tree[cur].ch[0]&&k<=tree[tree[cur].ch[0]].sz)cur=tree[cur].ch[0];
        else{
            k-=tree[tree[cur].ch[0]].sz+tree[cur].cnt;
            if(k<=0)return splay(cur);
            cur=tree[cur].ch[1];
        }
    }
}
inline void pre(int x){kth(rnk(x)-1);}
inline void suf(int x){kth(rnk(x+1));}
inline bool del(int x){
    pre(x);int p=root;
    suf(x);splay(p,root);
    int&cur=tree[p].ch[1];
    if(!cur)return 0;
    if(tree[cur].cnt>1)--tree[cur].cnt;
    else cur=0;
    pushup(p),pushup(root);
    return 1;
}
int main(){
    int m,op,x;
    scanf("%d",&m);
    for(;m--;){
        scanf("%d%d",&op,&x);
        switch(op){
            case 1:ins(x);break;
            case 2:del(x);break;
            case 3:printf("%d\n",rnk(x));break;
            case 4:printf("%d\n",(kth(x),tree[root].v));break;
            case 5:printf("%d\n",(pre(x),tree[root].v));break;
            default:printf("%d\n",(suf(x),tree[root].v));
        }
    }
    return 0;
}
2021/12/25 00:24
加载中...