mxqz
查看原帖
mxqz
541254
BugGod楼主2023/10/31 22:19

样例 MLE,提交后面 2WA,其余 MLE。

#include<bits/stdc++.h>
using namespace std;
#define inf (1ll<<31ll)-1ll
#define val(p)tree[p].val
#define ls(p)tree[p].l
#define rs(p)tree[p].r
#define cnt(p)tree[p].cnt
#define siz(p)tree[p].siz
struct _BST_
{
    int ncnt=0;
    struct node
    {
        int val,l,r,cnt,siz;
    }tree[100010];
    void insleft(int p,int x)
    {
        ncnt++;
        val(ncnt)=x;
        cnt(ncnt)=siz(ncnt)=1;
        ls(p)=ncnt;
    }
    void insright(int p,int x)
    {
        ncnt++;
        val(ncnt)=x;
        cnt(ncnt)=siz(ncnt)=1;
        rs(p)=ncnt;
    }
    void add(int p,int x)
    {
        siz(p)++;
        if(val(p)==x){cnt(p)++;return ;}
        if(val(p)>x)
        {
            if(ls(p))add(ls(p),x);
            else insleft(p,x);
        }
        else
        {
            if(rs(p))add(rs(p),x);
            else insright(p,x);
        }
    }
    int qfront(int p,int x,int ans)
    {
        if(val(p)>=x)
        {
            if(!ls(p))return ans;
            return qfront(ls(p),x,ans);
        }
        if(!rs(p))
        {
            if(val(p)<x)return val(p);
            return ans;
        }
        if(cnt(p))return qfront(rs(p),x,val(p));
        return qfront(rs(p),x,ans);
    }
    int qnext(int p,int x,int ans)
    {
        if(val(p)<=x)
        {
            if(!rs(p))return ans;
            return qnext(rs(p),x,ans);
        }
        if(!ls(p))
        {
            if(val(p)>x)return val(p);
            return ans;
        }
        if(cnt(p))return qnext(ls(p),x,val(p));
        return qnext(ls(p),x,ans);
    }
    int qrank(int p,int x)
    {
        if(!x)return x;
        if(x==val(p))return siz(ls(p));
        if(x<val(p))return qrank(ls(p),x);
        return qrank(rs(p),x)+siz(ls(p))+cnt(p);
    }
    int qvalue(int p,int x)
    {
        if(p==0)return inf;
        if(siz(ls(p))>=x)return qvalue(ls(p),x);
        if(siz(ls(p))+cnt(p)>=x)return val(p);
        return qvalue(rs(p),x-siz(ls(p))-cnt(p));
    }
}bst;
int n,opt,xx;
int main()
{
    cin>>n;
    while(n--)
    {
        cin>>opt>>xx;
        if(opt==1)cout<<bst.qrank(1,xx)+1<<endl;
        if(opt==2)cout<<bst.qvalue(1,xx)<<endl;
        if(opt==3)cout<<bst.qfront(1,xx,-inf)<<endl;
        if(opt==4)cout<<bst.qnext(1,xx,inf)<<endl;
        if(opt==5)bst.add(1,xx);
    }
    return 0;
}
2023/10/31 22:19
加载中...