蒟蒻Splay挂了求助……

P6136 【模板】普通平衡树(数据加强版)

Refined_heart @ 2020-02-28 21:08:18

#include<bits/stdc++.h>
using namespace std;
const int inf=1<<30;
struct node{
    node *ch[2],*fa;
    int siz,v,num;
}*rt,*null;
inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=(s<<1)+(s<<3)+(ch^48);
        ch=getchar();
    }
    return s*w;
}
inline void pushup(node *x){x->siz=x->ch[0]->siz+x->ch[1]->siz+x->num;}
inline void rotate(node *x){
    node *y=x->fa,*z=y->fa;int k=y->ch[1]==x;
    z->ch[z->ch[1]==y]=x;x->fa=z;
    y->ch[k]=x->ch[k^1];x->ch[k^1]->fa=y;
    x->ch[k^1]=y;y->fa=x;pushup(y);pushup(x);
}
inline void splay(node *x,node *g){
    node *y,*z;
    while(x->fa!=g){
        y=x->fa,z=y->fa;
        if(z!=g){(z->ch[1]==y)^(y->ch[1]==x)?rotate(x):rotate(y);}   
        rotate(x);
    }
    if(g==null)rt=x;
}
inline node* new_(){node *x=new node;x->ch[0]=x->ch[1]=null;x->fa=null;x->siz=x->num=1;return x;}
inline void Insert(int v){
    node *x=rt;
    if(x==null){rt=new_();rt->v=v;return;}
    node *fa=null;
    while(x!=null){
        fa=x;if(v!=x->v)x=x->ch[v>=x->v];
        else{splay(x,null);x->siz++;x->num++;return;}
    }
    x=new_();fa->ch[v>=fa->v]=x;
    x->fa=fa;x->v=v;splay(x,null);
}
inline void del(node *x){
    splay(x,null);x->siz--;x->num--;
    if(x->num)return;
    int k;
    if(x->ch[0]!=null)k=0;
    else if(x->ch[1]!=null)k=1;
    else {rt=null;delete(x);return;}
    node *y=x->ch[k];
    while(y->ch[k^1]!=null)y=y->ch[k^1];
    splay(y,x);y->ch[k^1]=x->ch[k^1];
    x->ch[k^1]->fa=y;y->fa=null;rt=y;delete(x);
}
inline void Del(int v){
    node *x=rt;
    while(x!=null){
        if(v!=x->v)x=x->ch[v>=x->v];
        else {del(x);return;}
    }
}
inline int rank(int v){
    node *x=rt;
    while(x!=null){
        if(v>x->v)x=x->ch[1];
        else if(v<x->v)x=x->ch[0];
        else {
            splay(x,null);
            return x->ch[0]->siz+1; 
        }
    }
    return 0;
}
inline int Kth(int k){
    node *x=rt;int c;
    while(x!=null){
        c=x->ch[0]->siz;
        if(c>=k)x=x->ch[0];
        else {
            k-=c+x->num;
            if(k<=0){splay(x,null);return x->v;}
            x=x->ch[1];
        }
    }
    return 0;
}
inline int pre(int v){
    node *x=rt;
    int ans=-inf;
    while(x!=null) {
        if(x->v<v){ans=max(ans,x->v),x=x->ch[1];}
        else x=x->ch[0];
    }
    return ans;
}
inline int last(int v){
    node *x=rt;int ans=inf;
    while(x!=null){
        if(x->v>v){ans=min(ans,x->v);x=x->ch[0];}
        else x=x->ch[1];
    }
    return ans;
}
int n,opt,x,m,p,realans;
int main(){
    null=new node ;null->siz=0;
    rt=null;n=read();m=read();
    for(int i=1;i<=n;++i){
        x=read();
        Insert(x);
    }
    for(int i=1;i<=m;++i){
        opt=read(),x=read();x^=p;
        if(opt==1)Insert(x);
        else if(opt==2)Del(x);
        else if(opt==3)p=rank(x),realans^=p;
        else if(opt==4)p=Kth(x),realans^=p;
        else if(opt==5)p=pre(x),realans^=p;
        else p=last(x),realans^=p;
    }
    printf("%d\n",realans);
    return 0;
}

不清楚为啥rank有点挂还有30分……也不知道为啥rank会随便跳到0……


by Warriors_Cat @ 2020-02-28 21:09:29

呜呜呜指针版康不懂……


by Smile_Cindy @ 2020-02-28 21:13:10

@Refined_heart

不是说了要先插入再删除么……


by Smile_Cindy @ 2020-02-28 21:13:24

就是3,5,6要先插后删


by Refined_heart @ 2020-02-28 21:13:44

@蒟蒻的名字 有人能回复我已经很高兴了……当初发了好几个贴子0回复……


by Refined_heart @ 2020-02-28 21:16:02

@Alpha 啊……明白了,谢谢大佬qwq

已A


|