替罪羊树 板子

来源:互联网 发布:华硕超频软件 编辑:程序博客网 时间:2024/05/01 01:35

平衡因子一般用的0.7,0.75或0.8,随机化也行

重要的部分大概就这些吧

void dfs(const int &x){    if(tr[x].lc) dfs(tr[x].lc),tr[x].lc=0;    id[++cnt]=x;    if(tr[x].rc) dfs(tr[x].rc),tr[x].rc=0;}int build(const int &l,const int &r,const int &ff){    int mid=(l+r)>>1;    int x=id[mid];    fa[x]=ff; siz[x]=1;    if(l<mid) siz[x]+=siz[tr[x].lc=build(l,mid-1,x)];    if(r>mid) siz[x]+=siz[tr[x].rc=build(mid+1,r,x)];    return x;}int rebuild(int x){    int ff=fa[x];    cnt=0; dfs(x); x=build(1,cnt,ff);    return x;}void Balanced(const int &pos){    int deep=1,x=pos;    while(fa[x]) deep++,x=fa[x];    if((double)deep<(double)log(N)/log(1/A)) return ;    x=fa[pos];    while((double)siz[tr[x].lc]<A*siz[x]&&          (double)siz[tr[x].rc]<A*siz[x]) x=fa[x];    if(!x) return ;    if(x==root) {root=rebuild(root); return ;}    int ff=fa[x];    int &T=tr[ff].lc==x?tr[ff].lc:tr[ff].rc; T=rebuild(x);}
0 0