[HNOI2004]宠物收养场(splay的应用)

题意:传送门

题解:这个题要么这个时刻宠物多,要么这个时刻顾客多,然后可以用个cnt来表示出此刻是顾客树还是宠物树,然后在删除这个点是不能用这个点的前驱与后继这个函数了,不然T到爆啊,需要重新写个函数,两个还是有点区别呢?

附上代码:


#include<bits/stdc++.h>

using namespace std;

const int maxn=8e4+5;
const int mod=1000000;

inline int read()
{
    register int x=0,t=1;
    register char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-'){t=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*t;
}

int root,tot;

struct Node
{
    int ch[2];
    int val;
    int ff;
    int size;
    int cnt;
}t[maxn];

inline void pushup(int u)
{
    t[u].size=t[t[u].ch[0]].size+t[t[u].ch[1]].size+t[u].cnt;
}

inline void rotate(int x)
{
    int y=t[x].ff;
    int z=t[y].ff;
    int k=(t[y].ch[1]==x);
    t[z].ch[t[z].ch[1]==y]=x;
    t[x].ff=z;
    t[y].ch[k]=t[x].ch[k^1];
    t[t[x].ch[k^1]].ff=y;
    t[x].ch[k^1]=y;
    t[y].ff=x;
    pushup(y);pushup(x);
}

inline void splay(int x,int goal)
{
    while(t[x].ff!=goal){
        int y=t[x].ff;
        int z=t[y].ff;
        if(z!=goal){
            (t[y].ch[0]==x)^(t[z].ch[0]==y)?rotate(x):rotate(y);
        }
        rotate(x);
    }
    if(goal==0){
        root=x;
    }
}

inline void insert(int x)
{
    int u=root,ff=0;
    while(u&&t[u].val!=x){
        ff=u;
        u=t[u].ch[x>t[u].val];
    }
    if(u){
        t[u].cnt++;
    }else{
        u=++tot;
        if(ff){
            t[ff].ch[x>t[ff].val]=u;
        }
        t[u].ch[0]=t[u].ch[1]=0;
        t[tot].ff=ff;
        t[tot].val=x;
        t[tot].cnt=1;
        t[tot].size=1;
    }
    splay(u,0);
}

inline void find(int x)
{
    int u=root;
    if(!u){
        return;
    }
    while(t[u].ch[x>t[u].val]&&x!=t[u].val){
        u=t[u].ch[x>t[u].val];
    }
    splay(u,0);
}

inline int Next(int x,int f)
{
    find(x);
    int u=root;
    if(t[u].val>=x&&f){
        return u;
    }
    if(t[u].val<=x&&!f){
        return u;
    }
    u=t[u].ch[f];
    while(t[u].ch[f^1]){
        u=t[u].ch[f^1];
    }
    return u;
}

inline int Next_une(int x,int f)
{
    find(x);
    int u=root;
    if(t[u].val>x&&f){
        return u;
    }
    if(t[u].val<x&&!f){
        return u;
    }
    u=t[u].ch[f];
    while(t[u].ch[f^1]){
        u=t[u].ch[f^1];
    }
    return u;
}

inline void Delete(int x)
{
    int last=Next_une(x,0);
    int next=Next_une(x,1);
    splay(last,0);splay(next,last);
    int del=t[next].ch[0];
    if(t[del].cnt>1){
        t[del].cnt--;
        splay(del,0);
    }else{
        t[next].ch[0]=0;
    }
}

inline int kth(int x)
{
    int u=root;
    if(t[u].size<x){
        return 0;
    }
    while(1){
        int y=t[u].ch[0];
        if(x>t[y].size+t[u].cnt){
            x-=t[y].size+t[u].cnt;
            u=t[u].ch[1];
        }else{
            if(t[y].size>=x){
                u=y;
            }else{
                return t[u].val;
            }
        }
    }
}

int main()
{
    int n=read();
    int cnt=0,ans=0;
    insert(+214748364);
    insert(-214748364);
    while(n--){
        int k=read(),x=read();
        if(cnt==0){
            insert(x);
        }
        if(cnt>0){
            if(k==0){
                insert(x);
            }else{
                int a1=t[Next(x,0)].val;
                int a2=t[Next(x,1)].val;
                if(abs(a1-x)<=abs(a2-x)){
                    (ans+=abs(a1-x))%=mod;
                    Delete(a1);
                }else{
                    (ans+=abs(a2-x))%=mod;
                    Delete(a2);
                }
            }
        }
        if(cnt<0){
            if(k==1){
                insert(x);
            }else{
                int a1=t[Next(x,0)].val;
                int a2=t[Next(x,1)].val;
                if(abs(a1-x)<=abs(a2-x)){
                    (ans+=abs(a1-x))%=mod;
                    Delete(a1);
                }else{
                    (ans+=abs(a2-x))%=mod;
                    Delete(a2);
                }
            }
        }
        cnt=cnt+(k==0?1:-1);
    }
    printf("%d\n",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值