Splay TLE88 求助
查看原帖
Splay TLE88 求助
166078
Thunder_S楼主2022/1/25 21:13
#include<cstdio>
#include<algorithm>
#define ls(x) t[x].son[0]
#define rs(x) t[x].son[1]
#define fa(x) t[x].fa
#define root t[0].son[1]
#define N 100005
#define inf 2147483647
using namespace std;
struct node
{
    int fa,son[2],val,num,size;
}t[N];
int n,typ,x,tot;
int read()
{
    int res=0,fh=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') fh=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch-'0'),ch=getchar();
    return res*fh;
}
void pushup(int x) {t[x].size=t[ls(x)].size+t[rs(x)].size+t[x].num;}
int lorr(int x) {return t[fa(x)].son[0]==x?0:1;}
void con(int x,int y,int son) {t[y].son[son]=x;t[x].fa=y;}
void rot(int x)
{
    int y=fa(x),z=fa(y);
    int ys=lorr(x),zs=lorr(y);
    con(t[x].son[ys^1],y,ys);
    con(y,x,ys^1);
    con(x,z,zs);
    pushup(y);pushup(x);
}
void splay(int x,int rt)
{
    rt=fa(rt);
    while (fa(x)!=rt)
    {
        int y=fa(x);
        if (t[y].fa==rt) rot(x);
        else if (lorr(x)==lorr(y)) rot(y),rot(x);
        else rot(x),rot(x);
    }
}
int newp(int x,int fa)
{
    t[++tot].fa=fa;
    t[tot].num=t[tot].size=1;
    t[tot].son[0]=t[tot].son[1]=0;
    t[tot].val=x;
    return tot;
}
void ins(int x)
{
    int now=root;
    if (root==0) newp(x,0),root=tot;
    else
    {
        while (true)
        {
            ++t[now].size;
            if (t[now].val==x) 
            {
                t[now].num++;
                splay(now,root);
                return;
            }
            int to=x<t[now].val?0:1;
            if (!t[now].son[to])
            {
                int p=newp(x,now);
                t[now].son[to]=p;
                splay(p,root);
                return;
            }
            now=t[now].son[to];
        }
    }
}
int getid(int x)
{
    int now=root;
    while (true)
    {
        if (!now) return 0;
        if (t[now].val==x)
        {
            splay(now,root);
            return now;
        }
        int to=x<t[now].val?0:1;
        now=t[now].son[to];
    }
}
void del(int x)
{
    int p=getid(x);
    if (!p) return;
    if (t[p].num>1) t[p].num--,t[p].size--;
    else
    {
        if (!t[p].son[0]&&!t[p].son[1])
        {
            root=0;
            return;
        }
        else if (!t[p].son[0])
        {
            root=t[p].son[1];
            t[root].fa=0;
            return;
        }
        else
        {
            int lson=t[p].son[0];
            while  (t[lson].son[1]) lson=t[lson].son[1];
            splay(lson,t[p].son[0]);
            con(t[p].son[1],lson,1);
            con(lson,0,1);
            pushup(lson);
        }
    }
}
int rnk(int x)
{
    int now=root,ans=0;
    while (true)
    {
        if (t[now].val==x) return ans+t[t[now].son[0]].size+1;
        int to=x<t[now].val?0:1;
        if (to==1) ans+=t[t[now].son[0]].size+t[now].num;
        now=t[now].son[to];
    }
}
int kth(int x)
{
    int now=root;
    while (true)
    {
        int lsize=t[now].size-t[t[now].son[1]].size;
        if (t[t[now].son[0]].size<x&&x<=lsize)
        {
            splay(now,root);
            return t[now].val;
        }
        if (x<lsize) now=t[now].son[0];
        else now=t[now].son[1],x-=lsize;
    }
}
int pre(int x)
{
    int now=root,ans=-inf;
    while (now)
    {
        if (t[now].val<x) ans=max(ans,t[now].val);
        int to=x<=t[now].val?0:1;
        now=t[now].son[to];
    }
    return ans;
}
int suf(int x)
{
    int now=root,ans=inf;
    while (now)
    {
        if (t[now].val>x) ans=min(ans,t[now].val);
        int to=x<t[now].val?0:1;
        now=t[now].son[to];
    }
    return ans;
}
int main()
{
    n=read();
    while (n--)
    {
        typ=read();x=read();
        if (typ==1) ins(x);
        if (typ==2) del(x);
        if (typ==3) printf("%d\n",rnk(x));
        if (typ==4) printf("%d\n",kth(x));
        if (typ==5) printf("%d\n",pre(x));
        if (typ==6) printf("%d\n",suf(x));
    }
    return 0;
}
2022/1/25 21:13
加载中...