替罪羊树 模板 bzoj3224

#include<stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const double a=0.7;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    register int x=0,f=1;
    register char ch=nc();
    while(ch<'0'||ch>'9'){if(ch=='-')f*=-1;ch=nc();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=nc();
    return f*x;
}
struct node{int s[2],f,size,num;}t[100001];int tnum,root;
inline void init(){
    tnum=2;root=1;
    t[1].num=-2147483647;t[1].size=2;t[1].s[1]=2;
    t[2].num=2147483647;t[2].size=1;t[2].f=1;
}
inline bool balance(int po){
    return (double)t[po].size*a>=(double)t[t[po].s[0]].size
         &&(double)t[po].size*a>=(double)t[t[po].s[1]].size;
}
int E[100001],esize;
void travel(int po){
    if(t[po].s[0])travel(t[po].s[0]);
    E[++esize]=po;
    if(t[po].s[1])travel(t[po].s[1]);
}
int build(int l,int r){
    if(l>r)return 0;
    int mid=(l+r)/2,po=E[mid];
    t[t[po].s[0]=build(l,mid-1)].f=po;
    t[t[po].s[1]=build(mid+1,r)].f=po;
    t[po].size=t[t[po].s[0]].size+t[t[po].s[1]].size+1;
    return po;
}
inline void rebuild(int po){
    esize=0;travel(po);
    int fa=t[po].f,ws=(t[t[po].f].s[1]==po);
    int npo=build(1,esize);
    t[t[fa].s[ws]=npo].f=fa;
    if(po==root)root=npo;
}
inline void insert(int num){
    int now=root,npo=++tnum;
    t[npo].size=1;t[npo].num=num;
    while(true){
        t[now].size++;
        bool ws=(num>=t[now].num);
        if(t[now].s[ws])now=t[now].s[ws];
        else {t[t[now].s[ws]=npo].f=now;break ;}
    }
    int inv=0;
    for(int i=npo;i;i=t[i].f)if(!balance(i))inv=i;
    if(inv)rebuild(inv);
}
inline int rank(int num){
    int now=root,ans=0;
    while(now){
        if(t[now].num<num)ans+=t[t[now].s[0]].size+1,now=t[now].s[1];
        else now=t[now].s[0];
    }
    return ans;
}
inline int getkth(int kth){
    int now=root;
    while(true){
        if(t[t[now].s[0]].size==kth-1)return now;
        else if(t[t[now].s[0]].size>=kth)now=t[now].s[0];
        else kth-=t[t[now].s[0]].size+1,now=t[now].s[1];
    }
    return now;
}
inline int getn(int num)
{
    int now=root;
    while(true)
    {
        if(t[now].num==num)return now;
        else now=t[now].s[t[now].num<num];
    }
}
inline void erase(int po)
{
    if(t[po].s[0]&&t[po].s[1])
    {
        int tpo=t[po].s[0];
        while(t[tpo].s[1])tpo=t[tpo].s[1];
        t[po].num=t[tpo].num;
        po=tpo;
    }
    int son=(t[po].s[0])?t[po].s[0]:t[po].s[1],ws=(t[t[po].f].s[1]==po);
    t[t[t[po].f].s[ws]=son].f=t[po].f;
    for(int i=t[po].f;i;i=t[i].f)t[i].size--;
    if(po==root)root=son;
}
inline int succ(int num)
{
    int now=root,ans=2147483647;
    while(now)
    {
        if(t[now].num>num)ans=min(ans,t[now].num),now=t[now].s[0];
        else now=t[now].s[1];
    }
    return ans;
}
inline int pred(int num)
{
    int now=root,ans=-2147483647;
    while(now)
    {
        if(t[now].num<num)ans=max(ans,t[now].num),now=t[now].s[1];
        else now=t[now].s[0];
    }
    return ans;    
}
int main(int argc, char *argv[])
{
    init();
    int n=read();
    for(int i=1;i<=n;i++){
        int p=read(),num=read();
        if(p==1)insert(num);
        if(p==2)erase(getn(num));
        if(p==3)printf("%d\n",rank(num));
        if(p==4)printf("%d\n",t[getkth(num+1)].num);
        if(p==5)printf("%d\n",pred(num));
        if(p==6)printf("%d\n",succ(num));
    }
    return 0;
}

If someone loves a flower, of which just one single blossom grows in all the millions and millions of stars, it is enough to make him happy just to look at the stars. He can say to himself, “Somewhere, my flower is there…” But if the sheep eats the flower, in one moment all his stars will be darkened… And you think that is not important!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值