bzoj3224 Tyvj 1728 普通平衡树

题目

高级数据结构。。。。。模板题,用平衡树解决。
不太会指针,只好用数组模拟。
存个模板吧。

#include<bits/stdc++.h>
#define N 1000000
using namespace std;
int n,opt,x;
int f[N+1],ch[N+1][2],key[N+1],cnt[N+1],siz[N+1],sz,rt;
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()
{
    int x=0,f=1;
    char c=nc();
    while((c<'0'||c>'9')&&c!='-')c=nc();
    if(c=='-')f=-1,c=nc();
    while(c<='9'&&c>='0')x=x*10+c-'0',c=nc();
    return x*f;
}
inline void clear(int x)
{
    ch[x][0]=ch[x][1]=f[x]=key[x]=cnt[x]=siz[x]=0;
}
inline int get(int x)
{
    return ch[f[x]][1]==x;
}
inline void update(int x)
{
    if(!x)return;
    siz[x]=cnt[x];
    if(ch[x][0])siz[x]+=siz[ch[x][0]];
    if(ch[x][1])siz[x]+=siz[ch[x][1]];
}
inline void rotate(int x)
{
    int old=f[x],oldf=f[old],which=get(x);
    ch[old][which]=ch[x][which^1];f[ch[old][which]]=old;
    f[old]=x,ch[x][which^1]=old;
    f[x]=oldf;
    if(oldf)ch[oldf][ch[oldf][1]==old]=x;
    update(old),update(x);
}
inline void splay(int x)
{
    for(int fa;fa=f[x];rotate(x))
        if(f[fa])rotate((get(x)==get(fa))?fa:x);
    rt=x;
}
inline void insert(int x)
{
    if(rt==0){sz++,clear(sz),key[sz]=x,cnt[sz]=1,siz[sz]=1,rt=sz;return;}
    int now=rt,fa=0;
    while(true)
    {
        if(key[now]==x)
        {
            cnt[now]++;
            update(now),update(fa);
            splay(now);
            break;
        }
        fa=now;
        now=ch[now][key[now]<x];
        if(!now)
        {
            sz++;
            clear(sz);
            key[sz]=x,cnt[sz]=1,siz[sz]=1;
            f[sz]=fa;ch[fa][key[fa]<x]=sz;
            update(fa);
            splay(sz);
            break;
        }
    }
}
inline int find(int x)
{
    int ans=0,now=rt;
    while(true)
    {
        if(x<key[now])now=ch[now][0];
        else
        {
            ans+=(ch[now][0]?siz[ch[now][0]]:0);
            if(x==key[now]){splay(now);return ans+1;}
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
inline int findx(int x)
{
    int now=rt;
    while(true)
    {
        if(ch[now][0]&&x<=siz[ch[now][0]])now=ch[now][0];
        else
        {
            int tmp=(ch[now][0]?siz[ch[now][0]]:0)+cnt[now];
            if(x<=tmp)return key[now];
            x-=tmp,now=ch[now][1];
        }
    }
}
inline int pre()
{
    int now=ch[rt][0];  
    while (ch[now][1]) now=ch[now][1];  
    return now;  
}
inline int suf()
{
    int now=ch[rt][1];  
    while (ch[now][0]) now=ch[now][0];  
    return now;  
}
inline void del(int x)
{
    int tmp=find(x);
    if(cnt[rt]>1){cnt[rt]--;update(rt);return;}
    if(!ch[rt][0]&&!ch[rt][1]){clear(rt);rt=0;return;}
    if(!ch[rt][0])
    {
        int oldrt=rt;
        rt=ch[rt][1];f[rt]=0;
        clear(oldrt);
        return;
    }
    if(!ch[rt][1])
    {
        int oldrt=rt;
        rt=ch[rt][0];f[rt]=0;
        clear(oldrt);
        return;
    }
    int oldrt=rt,leftbig=pre();
    splay(leftbig);
    ch[rt][1]=ch[oldrt][1];
    f[ch[oldrt][1]]=rt;
    clear(oldrt);
    update(rt);

}
int main()
{
    //freopen("in.txt","r",stdin);
    n=read();
    for(int i=1;i<=n;i++)
    {
        opt=read(),x=read();
        if(opt==1)insert(x);
        if(opt==2)del(x);
        if(opt==3)printf("%d\n",find(x));
        if(opt==4)printf("%d\n",findx(x));
        if(opt==5)insert(x),printf("%d\n",key[pre()]),del(x);
        if(opt==6)insert(x),printf("%d\n",key[suf()]),del(x);
    }
    return 0;
} 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值