# 平衡树学习笔记

Posted by yjjr's blog on December 18, 2017

noip前整个机房除了我都在学treap，都相信noip2017会考平衡树

# 非旋treap(fhq treap)

## 具体操作

### merge(x,y)

• 若x的key< y的key则将x的右儿子变为merge(x的右儿子，y)（即merge(tree[x].r,y)）
• 否则将y的左儿子变为merge(x，y的左儿子)（即merge(x,tree[y].l)）

### split(x,val,new1,new2)

• 若t[x].w<val那么说明val的分界点在x右子树，new1=x，splite(t[x].r,val,t[x].r,new2);
• 否则说明val的分界点在x或其左子树上，new2=x;splite(t[x].l,val,new1,t[x].l);

## code(bzoj3224普通平衡树)

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5+6;
struct tree{int l,r,size,w,f;}t[maxn<<1];
int n,len=0,root=0;
inline int getrank(){return (rand()<<16)+rand();}
void change(int x){
if(x==0)return;
t[x].size=t[t[x].l].size+t[t[x].r].size+1;
}
int merge(int x,int y){
if(x==0||y==0)return x+y;
if(t[x].f>t[y].f){
t[x].r=merge(t[x].r,y);change(x);return x;}
else{t[y].l=merge(x,t[y].l);change(y);return y;}
}
void splite(int x,int val,int &new1,int &new2){
if(x==0){new1=0;new2=0;return;}
if(t[x].w<val){
new1=x;splite(t[x].r,val,t[x].r,new2);
}else{new2=x;splite(t[x].l,val,new1,t[x].l);}
change(x);
}
int insert(int now,int want){
if(now==0)return want;
if(t[now].f>t[want].f){
if(t[now].w<t[want].w)t[now].r=insert(t[now].r,want);else t[now].l=insert(t[now].l,want);
change(now);return now;
}
splite(now,t[want].w,t[want].l,t[want].r);change(want);return want;
}

int find(int now,int want){
if(t[t[now].l].size==want-1)return t[now].w;
if(t[t[now].l].size>=want)return find(t[now].l,want);
return find(t[now].r,want-t[t[now].l].size-1);
}
int findleft(int now){while(t[now].l)now=t[now].l;return t[now].w;}
int findright(int now){while(t[now].r)now=t[now].r;return t[now].w;}

int main()
{
srand(1125);
mem(t,0);
rep(i,1,n){
}
return 0;
}


# splay

## 具体操作

### rotate

void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)l=0;else l=1;r=l^1;
if(y==k)k=x;
else{if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}


### splay(x,k)

void splay(int x,int &k)
{
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if(c[y][0]==x^c[z][0]==y)rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}


### pushup/pushdown

void pushup(int x)
{
int l=c[x][0],r=c[x][1];
size[x]=size[l]+size[r]+1;
mx[x]=max(v[x],max(mx[l],mx[r]));
}

void pushdown(int x)
{
int l=c[x][0],r=c[x][1],t=tag[x];
if(t){
tag[x]=0;
if(l){tag[l]+=t;mx[l]+=t;v[l]+=t;}
if(r){tag[r]+=t;mx[r]+=t;v[r]+=t;}
}
if(rev[x]){
rev[x]^=1;rev[l]^=1;rev[r]^=1;
swap(c[x][0],c[x][1]);
}
}



### find

int find(int x,int rank)
{
if(rev[x]||tag[x])pushdown(x);
int l=c[x][0],r=c[x][1];
if(size[l]+1==rank)return x;
else if(size[l]>=rank)return find(l,rank);
else return find(r,rank-size[l]-1);
}


### rever

void rever(int l,int r)
{
int x=find(root,l),y=find(root,r+2);
splay(x,root);splay(y,c[x][1]);
int z=c[y][0];rev[z]^=1;
}


## code(bzoj3223文艺平衡树)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5+6;
int n,m,sz,rt,fa[maxn],c[maxn][2],id[maxn],size[maxn];
bool rev[maxn];
void pushup(int k)
{
int l=c[k][0],r=c[k][1];
size[k]=size[l]+size[r]+1;
}
void pushdown(int k)
{
int l=c[k][0],r=c[k][1];
if(rev[k]){
swap(c[k][0],c[k][1]);
rev[l]^=1;rev[r]^=1;
rev[k]=0;
}
}
void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)l=0;else l=1;
r=l^1;
if(y==k)k=x;
else{
if(c[z][0]==y)c[z][0]=x;
else c[z][1]=x;
}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x,int &k)
{
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if(c[y][0]==x^c[z][0]==y)rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int k,int rank)
{
pushdown(k);
int l=c[k][0],r=c[k][1];
if(size[l]+1==rank)return k;
else if(size[l]>=rank)return find(l,rank);
else return find(r,rank-size[l]-1);
}
void rever(int l,int r){
int x=find(rt,l),y=find(rt,r+2);
splay(x,rt);splay(y,c[x][1]);
int z=c[y][0];
rev[z]^=1;
}
void build(int l,int r,int f)
{
if(l>r)return;
int now=id[l],last=id[f];
if(l==r){
fa[now]=last;size[now]=1;
if(l<f)c[last][0]=now;
else c[last][1]=now;
return;
}
int mid=(l+r)>>1;now=id[mid];
build(l,mid-1,mid);build(mid+1,r,mid);
fa[now]=last;pushup(mid);
if(mid<f)c[last][0]=now;
else c[last][1]=now;
}

int main()
{