数据结构平衡树treap
Posted yifusuyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构平衡树treap相关的知识,希望对你有一定的参考价值。
之前写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)
",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),'
',true);
else if(a == 4) qw(asknum(rot,b),'
',true);
else if(a == 5) qw(askpre(rot,b),'
',true);
else if(a == 6) qw(askpro(rot,b),'
',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的主要内容,如果未能解决你的问题,请参考以下文章