Splay TLE on #5,#9 求助

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

快斗游鹿 @ 2022-07-28 20:45:39

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5000000;
struct Node{
    ll ch[2];
    ll f,cnt,son,val;
}t[N];
ll n,m,_x,root,tot,ans;
inline void read(ll &x) {
    x=0;
    register ll f=1;
    register char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while (c>='0'&&c<='9')
        x=(x<<3)+(x<<1)+c-'0',c=getchar();
    x*=f;
}
inline void push_up(ll x){
    t[x].son=t[t[x].ch[0]].son+t[t[x].ch[1]].son+t[x].cnt;
}
inline void rotate(ll x){//旋转
    register ll y=t[x].f;
    register ll z=t[y].f;
    register ll k;
    if(t[y].ch[0]==x)k=0;
    else k=1;
    t[z].ch[t[z].ch[1]==y]=x;t[x].f=z;
    t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].f=y;
    t[y].f=x;t[x].ch[k^1]=y;
    push_up(y);push_up(x);
}
inline void Splay(ll x,ll goal){
    while(t[x].f!=goal){
        register ll y=t[x].f;
        register ll z=t[y].f;
        if(z!=goal){
            (t[z].ch[0])^(t[y].ch[0]==x)?rotate(x):rotate(y);
        }
        rotate(x);
    }
    if(goal==0){
        root=x;
    }
}
inline void insert(ll x){//插入
    register ll u=root,ff=0;
    while(u&&t[u].val!=x){
        ff=u;u=t[u].ch[t[u].val<x];
    }
    if(u){
        t[u].cnt++;
    }
    else{
        u=++tot;
        if(ff){
            t[ff].ch[t[ff].val<x]=u;
        }
        t[u].f=ff;t[u].val=x;t[u].son=t[u].cnt=1;
    }
    Splay(u,0);
}
inline void find(ll x){//找位置
    register ll u=root;
    if(!u)return;
    while(t[u].ch[t[u].val<x]&&t[u].val!=x){
        u=t[u].ch[t[u].val<x];
    }
    Splay(u,0);
}
inline ll next(ll x,ll f){//找前驱/后继
    find(x);
    register ll u=root;
    u=t[u].ch[f];
    while(t[u].ch[f^1])u=t[u].ch[f^1];
    return u;
}
inline void _delete(ll x){//删除
    register ll first=next(x,0);
    register ll last=next(x,1);
    Splay(first,0);Splay(last,first);
    ll del=t[last].ch[0];
    if(t[del].cnt>1){
        t[del].cnt--;
        Splay(del,0);
    }
    else{
        t[last].ch[0]=0;
    }
}
inline ll rk(ll x){//按排名找值
    register ll u=root;
    if(t[u].son<x)return t[u].son;
    while(1){
        ll y=t[u].ch[0];
        if(x>t[y].son+t[u].cnt){
            x=x-t[y].son-t[u].cnt;
            u=t[u].ch[1];
        }
        else if(t[y].son>=x){
            u=y;
        }
        else{
            return t[u].val;
        }
    }
}
int main(){
    //freopen("std.in","r",stdin);
    read(n);read(m);
    insert(-114514191999);
    insert(114514191999);
    for(register int i=1;i<=n;i++){
        register ll aaa;read(aaa);
        insert(aaa);    
    }
    for(register int i=1;i<=m;i++){
        register ll opt,x;read(opt);read(x);
        x^=_x;
        if(opt==1){
            insert(x);
        }
        else if(opt==2){
            _delete(x);
        }
        else if(opt==3){
            insert(x);
            find(x);_x=t[t[root].ch[0]].son;ans^=_x;
            _delete(x);
        }
        else if(opt==4){
            _x=rk(x+1);ans^=_x;
        }
        else if(opt==5){
            insert(x);
            _x=t[next(x,0)].val;ans^=_x;
            _delete(x);
        }
        else{
            insert(x);
            _x=t[next(x,1)].val;ans^=_x;
            _delete(x);
        }
    }
    printf("%lld",ans);
    return 0;
}

by ppip @ 2022-07-28 21:12:54

不建议 Splay,大力推荐无旋 Treap


|