Splay模板

struct Splay_Tree
{
    Splay_Tree *(*root);
    Splay_Tree *fa,*c[2];
    int val;
    int siz;
    long long sum;
    void update();
    bool getson();
    void rotate();
    void splay(Splay_Tree*);
    void insert(int);
    void remove(int);
    int rank(int);
    Splay_Tree* select(int);
} spl[600001],*null=spl;
void Splay_Tree::update()
{
    siz=c[0]->siz+c[1]->siz+1;
    sum=c[0]->sum+c[1]->sum+val;
}
bool Splay_Tree::getson() {return fa->c[0]!=this;}
void Splay_Tree::rotate()
{
    Splay_Tree *y=fa,*z=y->fa;
    bool k=getson();
    if (z!=null)
        z->c[y->getson()]=this;
    fa=z;
    y->c[k]=c[!k];
    c[!k]->fa=y;
    c[!k]=y;
    y->fa=this;
    y->update();
}
void Splay_Tree::splay(Splay_Tree *t)
{
    while (fa!=t)
    {
        Splay_Tree *y=fa,*z=y->fa;
        if (z!=t)
        {
            if (getson()!=y->getson())
                rotate();
            else
                y->rotate();
        }
        rotate();
    }
    update();
    if (t==null)
        *root=this;
}
void Splay_Tree::insert(int _val)
{
    ++siz;
    sum+=_val;
    if (_val>=val)
    {
        if (c[0]!=null)
            c[0]->insert(_val);
        else
        {
            c[0]=&spl[++cnt];
            c[0]->root=root;
            c[0]->fa=this;
            c[0]->c[0]=c[0]->c[1]=null;
            c[0]->val=_val;
            c[0]->siz=1;
            c[0]->sum=_val;
            c[0]->splay(null);
        }
    }
    else
    {
        if (c[1]!=null)
            c[1]->insert(_val);
        else
        {
            c[1]=&spl[++cnt];
            c[1]->root=root;
            c[1]->fa=this;
            c[1]->c[0]=c[1]->c[1]=null;
            c[1]->val=_val;
            c[1]->siz=1;
            c[1]->sum=_val;
            c[1]->splay(null);
        }
    }
}
void Splay_Tree::remove(int _val)
{
    if (_val==val)
    {
        splay(null);
        if (c[0]==null)
        {
            c[1]->fa=null;
            *root=c[1];
        }
        else if (c[1]==null)
        {
            c[0]->fa=null;
            *root=c[0];
        }
        else
        {
            Splay_Tree *pred=c[0];
            while (pred->c[1]!=null)
                pred=pred->c[1];
            pred->splay(this);
            pred->c[1]=c[1];
            c[1]->fa=pred;
            pred->fa=null;
            *root=pred;
            (*root)->update();
        }
        return;
    }
    if (_val>val)
        c[0]->remove(_val);
    else
        c[1]->remove(_val);
}
int Splay_Tree::rank(int _val)
{
    if (this==null)
        return 1;
    if (_val>=val)
        return c[0]->rank(_val);
    return c[0]->siz+1+c[1]->rank(_val);
}
Splay_Tree* Splay_Tree::select(int k)
{
    if (k<=c[0]->siz)
        return c[0]->select(k);
    if (k>c[0]->siz+1)
        return c[1]->select(k-c[0]->siz-1);
    return this;
}
void init()
{
    null->root=&null;
    null->fa=null->c[0]=null->c[1]=null;
}
void insert(int t,int val)
{
    if (root[t]!=null)
        root[t]->insert(val);
    else
    {
        root[t]=&spl[++cnt];
        root[t]->root=&root[t];
        root[t]->fa=root[t]->c[0]=root[t]->c[1]=null;
        root[t]->val=val;
        root[t]->siz=1;
        root[t]->sum=val;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值