• 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;
        }
    }
  • 相关阅读:
    hdu 1018
    hdu 1005
    hdu 1222
    hdu 1297
    hdu 1568
    WCF入门, 到创建一个简单的WCF应用程序
    BarTender 通过ZPL命令操作打印机打印条码, 操作RFID标签
    WCF入门的了解准备工作
    C# Bartender模板打印 条码,二维码, 文字, 及操作RFID标签等。
    Qt configure脚本说明
  • 原文地址:https://www.cnblogs.com/jz-597/p/11145294.html
Copyright © 2020-2023  润新知