码迷,mamicode.com
首页 > 其他好文 > 详细

【数据结构】【平衡树】treap

时间:2018-11-01 18:30:53      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:amp   out   getchar   key   else   lse   平衡   mes   max   

之前写treap的传送门
之前写的那个太毒瘤了,这次放一个更毒瘤的指针版上来

#include<cstdio>
#include<iostream>
#define rg register
#define ci const int
#define cl const long long

typedef long long int ll;
typedef unsigned int uit;

template <typename T>
inline void qr(T &x) {
    rg char ch=getchar(),lst=' ';
    while((ch > '9') || (ch < '0')) lst=ch,ch=getchar();
    while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    if(lst == '-') x=-x;
}

namespace IO {
    char buf[120];
}

template <typename T>
inline void qw(T x,const char aft,const bool pt) {
    if(x < 0) {x=-x,putchar('-');}
    rg int top=0;
    do {IO::buf[++top]=x%10+'0';} while(x/=10);
    while(top) putchar(IO::buf[top--]);
    if(pt) putchar(aft);
}

template <typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template <typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template <typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;}

template <typename T>
inline void mswap(T &_a,T &_b) {
    T _temp=_a;_a=_b;_b=_temp;
}

const int maxn = 100010;
const int INF = 20000000;

inline uit GetRandom(uit &x) {
    x^=x<<5;
    x^=x>>7;
    x^=x<<17;
    return x;
}

struct Treap {
    Treap *ls,*rs,*fa;
    int v,sz,sm,key;
    Treap() {ls=rs=fa=NULL;key=v=sz=sm=0;}
    inline int GetRelation(ci _v) {
        if(this->v == _v) return 0;
        else if(this->v > _v) return -1;
        else return 1;
    }
    inline void ChangeV(ci _v) {
        this->sm+=_v;this->sz+=_v;
    }
    inline bool NeedTurn(Treap *_son) {
        return this->key > _son->key;
    }
    inline void update() {
        this->sz=this->sm;//printf("QwQ%d ",this->sz);
        if(this->ls) this->sz+=this->ls->sz;//printf("(%d %d)",this->ls->v,this->ls->sz);
        if(this->rs) this->sz+=this->rs->sz;
    //  printf("(%d %d)\n",this->v,this->sz);
    }
    inline bool IsLeftSon() {
        return this->fa->ls == this;
    }
    inline void RTurn() {
        Treap *newrt=this->ls;
        this->ls=newrt->rs;
        if(this->ls) this->ls->fa=this;
        if(this->IsLeftSon()) this->fa->ls=newrt;
        else this->fa->rs=newrt;
        newrt->fa=this->fa;
        this->fa=newrt;
        newrt->rs=this;
        this->update();newrt->update();
    }
    inline void LTurn() {
        Treap *newrt=this->rs;
        this->rs=newrt->ls;
        if(this->rs) this->rs->fa=this;
        if(this->IsLeftSon()) this->fa->ls=newrt;
        else this->fa->rs=newrt;
        newrt->fa=this->fa;
        this->fa=newrt;
        newrt->ls=this;
        this->update();newrt->update();
    }
    void dltit() {
        this->key=INF;
        while((this->ls) || (this->rs)) {
            if(this->ls) {
                if(this->rs) {
                    if(this->ls->key < this->rs->key) this->RTurn();
                    else this->LTurn();
                }
                else this->RTurn();
            }
            else this->LTurn();
        }
        for(Treap *i=this->fa;i->fa;i=i->fa) {
            i->update();
        }
        if(this->IsLeftSon()) this->fa->ls=NULL;
        else this->fa->rs=NULL;
        *this=Treap();
    }
};
Treap *pool[maxn],*rot,qwq[maxn];
int top;

int n;
uit sed=19620718;

void buildroot();
void buildpool();
void add(Treap*,ci);
void dlt(Treap*,ci);
int askrnk(Treap*,ci);
int asknum(Treap*,ci);
int askpre(Treap*,ci);
int askpro(Treap*,ci);
void LinkNew(Treap*,ci,ci);

int main() {
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    qr(n);
    buildpool();
    buildroot();
    int a,b;
    while(n--) {
        a=b=0;qr(a);qr(b);
        if(a == 1) add(rot,b);
        else if(a == 2) dlt(rot,b);
        else if(a == 3) qw(askrnk(rot,b),'\n',true);
        else if(a == 4) qw(asknum(rot,b),'\n',true);
        else if(a == 5) qw(askpre(rot,b),'\n',true);
        else if(a == 6) qw(askpro(rot,b),'\n',true);
    }
    return 0;
}

void buildpool() {
    for(rg int i=0;i<maxn;++i) pool[i]=qwq+i;
    top=maxn-1;
}

void buildroot() {
    rot=pool[top--];
    rot->key=-INF;rot->v=INF;rot->sz=rot->sm=1;
}

void LinkNew(Treap *u,ci tp,ci v) {
    Treap *newp=pool[top--];
    newp->fa=u;newp->v=v;newp->key=mabs(int(GetRandom(sed)));
    newp->sz=newp->sm=1;
    if(tp < 0) u->ls=newp;
    else u->rs=newp;
}

void add(Treap* u,ci v) {
    int k=u->GetRelation(v);
    if(!k) {u->ChangeV(1);return;}
    else if(k < 0) {
        if(u->ls != NULL) add(u->ls,v);
        else LinkNew(u,-1,v);
        if(u->NeedTurn(u->ls)) u->RTurn();
    }
    else {
        if(u->rs != NULL) add(u->rs,v);
        else LinkNew(u,1,v);
        if(u->NeedTurn(u->rs)) u->LTurn();
    }
    u->update();
}

void dlt(Treap *u,ci v) {
    int k=u->GetRelation(v);
    if(!k) {
        u->ChangeV(-1);
        if(!(u->sm)) {u->dltit();pool[++top]=u;}
        return;
    }
    else if(k < 0) dlt(u->ls,v);
    else dlt(u->rs,v);
    u->update();
}

int askrnk(Treap *u,ci v) {
    int k=u->GetRelation(v);
    if(!k) {
        if(u->ls) return u->ls->sz+1;
        else return 1;
    }
    else if(k < 0) return askrnk(u->ls,v);
    else {
        if(u->ls) return askrnk(u->rs,v)+u->sm+u->ls->sz;
        else return askrnk(u->rs,v)+u->sm;
    }
}

int asknum(Treap *u,ci v) {
    int s=u->sm;
    if(u->ls) {
        int k=u->ls->sz;
        if(k >= v) return asknum(u->ls,v);
        s+=u->ls->sz;
    }
    if(s >= v) return u->v;
    else return asknum(u->rs,v-s);
}

int askpre(Treap *u,ci v) {
    if(u->v < v) {
        if(u->rs) return mmax(askpre(u->rs,v),u->v);
        else return u->v;
    }
    if(u->ls) return askpre(u->ls,v);
    return -INF;
}

int askpro(Treap *u,ci v) {
    if(u->v > v) {
        if(u->ls) return mmin(askpro(u->ls,v),u->v);
        else return u->v;
    }
    if(u->rs) return askpro(u->rs,v);
    return INF;
}

【数据结构】【平衡树】treap

标签:amp   out   getchar   key   else   lse   平衡   mes   max   

原文地址:https://www.cnblogs.com/yifusuyi/p/9890834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!