splay被卡

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

JimmyFlower @ 2020-03-05 10:29:46

6 T了

#pragma GCC optimize("Ofast","-funroll-loops","-fdelete-null-pointer-checks")
#pragma GCC target("ssse3","sse3","sse2","sse","avx2","avx")
#include<cctype>
#include<cstdio>
#define ri register int
using namespace std;
int n,m,opt,x,la=0,ans=0;
int root=0,len=0;
struct splay{int d,n,s,f,son[2];}tr[1110000];
inline int read()
{
    ri x=0; register char c=getchar();
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);x=(x<<3)+(x<<1)+(c&15),c=getchar());
    return x;
}
inline void update(int x)
{
    int lc=tr[x].son[0],rc=tr[x].son[1];
    tr[x].s=tr[lc].s+tr[rc].s+tr[x].n;
}
inline void add(int d,int f)
{
    len++;
    tr[len].d=d,tr[len].n=tr[len].s=1,tr[len].f=f,tr[len].son[0]=tr[len].son[1]=0;
    if(d<tr[f].d) tr[f].son[0]=len; else tr[f].son[1]=len;
}
inline void connect(int r,int R,int w)
{
    if(r) tr[r].f=R;
    tr[R].son[w]=r;
}
inline void rotate(int x,int w)
{
    int f=tr[x].f,ff=tr[f].f;
    connect(tr[x].son[w],f,w^1);
    int k=0; if(tr[ff].son[1]==f)k=1;
    connect(x,ff,k);
    connect(f,x,w);
    update(f),update(x);
}
inline void splay(int x,int rt)
{
    while(tr[x].f!=rt)
    {
        int f=tr[x].f,ff=tr[f].f;
        if(ff==rt)
        {
            if(tr[f].son[0]==x) rotate(x,1);
            else rotate(x,0);
        }
        else
        {
                 if(tr[ff].son[0]==f&&tr[f].son[0]==x) rotate(f,1),rotate(x,1);
            else if(tr[ff].son[1]==f&&tr[f].son[1]==x) rotate(f,0),rotate(x,0);
            else if(tr[ff].son[0]==f&&tr[f].son[1]==x) rotate(x,0),rotate(x,1);
            else if(tr[ff].son[1]==f&&tr[f].son[0]==x) rotate(x,1),rotate(x,0);
        }
    }
    if(rt==0) root=x;
}
inline int getip(int d)
{
    int x=root;
    while(tr[x].d!=d)
    {
        if(d<tr[x].d)
        {
            if(!tr[x].son[0]) break;
            x=tr[x].son[0];
        }
        else
        {
            if(!tr[x].son[1]) break;
            x=tr[x].son[1];
        }
    }
    return x;
}
inline void ins(int d)
{
    if(root==0){add(d,0),root=len;return;}
    int x=getip(d);
    if(tr[x].d==d)
    {
        tr[x].n++,update(x);
        splay(x,0);
    }
    else
    {
        add(d,x),update(x);
        splay(len,0);
    }
}
inline void del(int d)
{
    int x=getip(d); splay(x,0);
    if(tr[x].n>1){tr[x].n--,update(x);return;}
    if(!tr[x].son[0]&&!tr[x].son[1]) root=len=0;
    else if(!tr[x].son[0]&&tr[x].son[1]) root=tr[x].son[1],tr[root].f=0;
    else if(tr[x].son[0]&&!tr[x].son[1]) root=tr[x].son[0],tr[root].f=0;
    else
    {
        int p=tr[x].son[0];
        while(tr[p].son[1]) p=tr[p].son[1];
        splay(p,x);
        connect(tr[x].son[1],p,1);
        root=p,tr[p].f=0,update(p);
    }
}
inline int getrk(int d)
{
    int x=getip(d); splay(x,0);
    if(tr[x].d<d) return tr[tr[x].son[0]].s+tr[x].n+1;
    else return tr[tr[x].son[0]].s+1;
}
inline int kth(int k)
{
    int x=root;
    while(1)
    {
        int lc=tr[x].son[0],rc=tr[x].son[1];
        if(k<=tr[lc].s) x=lc;
        else if(k>tr[lc].s+tr[x].n) k=k-(tr[lc].s+tr[x].n),x=rc;
        else break;
    }
    splay(x,0);
    return tr[x].d;
}
inline int pre(int d)
{
    int x=getip(d); splay(x,0);
    if(d<=tr[x].d&&tr[x].son[0])
    {
        x=tr[x].son[0];
        while(tr[x].son[1]) x=tr[x].son[1];
    }
    return tr[x].d;
}
inline int nxt(int d)
{
    int x=getip(d); splay(x,0);
    if(tr[x].d<=d&&tr[x].son[1])
    {
        x=tr[x].son[1];
        while(tr[x].son[0]) x=tr[x].son[0];
    }
    return tr[x].d;
}
int main()
{
    n=read(),m=read();
    for(ri i=1;i<=n;i++) x=read(),ins(x);
    for(ri i=1;i<=m;i++)
    {
        opt=read(),x=read()^la;
        if(opt==1) ins(x);
        else if(opt==2) del(x);
        else if(opt==3) ans^=(la=getrk(x));
        else if(opt==4) ans^=(la=kth(x));
        else if(opt==5) ans^=(la=pre(x));
        else if(opt==6) ans^=(la=nxt(x));
    }
    printf("%d",ans);
    return 0;
}

by yummy @ 2020-03-05 10:30:34

这题杀敌一千,自损八百(


by Smile_Cindy @ 2020-03-05 10:31:40

@JimmyFlower Splay在评测高峰期难以通过,建议另找个时间交。


by JimmyFlower @ 2020-03-05 10:31:56

#6,#被吞了


by Polaris_Dane @ 2020-03-05 10:32:16

可以过啊...


by Lice @ 2020-03-05 10:32:19

@JimmyFlower fread


by JimmyFlower @ 2020-03-05 10:32:36

hhh,ok谢谢


by BFqwq @ 2020-03-05 10:36:54

@JimmyFlower 建议右转题解第一篇(


by JimmyFlower @ 2020-03-05 10:39:36

hh好的谢谢


|