bzoj1208 [HNOI2004] 宠物收养所 splay

15 篇文章 0 订阅

Description


最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

Solution


化简题意可以发现就是在一堆数字中间找最接近的数(绕)
那么开一棵splay,每次查找前驱后继删掉即可

删除x节点的操作可以看成是把x旋到根,再把x的后继旋到根的右子节点,删掉x让后继成为根即可
特判一下x是最大值的情况就直接删掉它

Code


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
const int INF=0x3f3f3f3f;
const int MOD=1000000;
const int N=100005;
struct treeNode{int fa,key,son[2];}t[N<<2];
int cnt=0,root=0;
int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}
void update(int &x,int fa,int key) {t[x=++cnt]=(treeNode){fa,key,{0,0}};}
void rrotate(int x) {
    int y=t[x].fa; int z=t[y].fa;
    t[y].son[0]=t[x].son[1];
    if (t[x].son[1]) t[t[x].son[1]].fa=y;
    t[x].fa=z;
    t[x].son[1]=y; t[y].fa=x;
    if (z) {
        if (t[z].son[0]==y) t[z].son[0]=x;
        else t[z].son[1]=x;
    }
}
void lrotate(int x) {
    int y=t[x].fa; int z=t[y].fa;
    t[y].son[1]=t[x].son[0];
    if (t[x].son[0]) t[t[x].son[0]].fa=y;
    t[x].fa=z;
    t[x].son[0]=y; t[y].fa=x;
    if (z) {
        if (t[z].son[0]==y) t[z].son[0]=x;
        else t[z].son[1]=x;
    }
}
void splay(int x,int goal) {
    while (t[x].fa!=goal) {
        int y=t[x].fa; int z=t[y].fa;
        if (z==goal) {
            if (t[y].son[0]==x) rrotate(x);
            else lrotate(x);
        } else {
            if (t[z].son[0]==y&&t[y].son[0]==x) {
                rrotate(y);
                rrotate(x);
            } else if (t[z].son[0]==y&&t[y].son[1]==x) {
                lrotate(x);
                rrotate(x);
            } else if (t[z].son[1]==y&&t[y].son[0]==x) {
                rrotate(x);
                lrotate(x);
            } else if (t[z].son[1]==y&&t[y].son[1]==x) {
                lrotate(y);
                lrotate(x);
            }
        }
    }
    if (!goal) root=x;
}
void insert(int key) {
    if (!root) {
        update(root,0,key);
        return ;
    }
    int x=root;
    while (x) {
        if (t[x].key>key) {
            if (!t[x].son[0]) {
                update(t[x].son[0],x,key);
                break ;
            } else x=t[x].son[0];
        } else {
            if (!t[x].son[1]) {
                update(t[x].son[1],x,key);
                break ;
            } else x=t[x].son[1];
        }
    }
    splay(cnt,0);
}
void del(int x) {
    splay(x,0);
    int tmp=t[x].son[1];
    if (!tmp) {
        root=t[x].son[0];
        t[root].fa=0;
        return ;
    }
    while (t[tmp].son[0]) tmp=t[tmp].son[0];
    splay(tmp,root);
    t[tmp].son[0]=t[root].son[0];
    t[tmp].fa=0;
    t[t[root].son[0]].fa=tmp;
    root=tmp;
}
int get_pre(int key) {
    int x=root;
    int y=0;
    while (x) {
        if (t[x].key<=key) {
            y=x;
            x=t[x].son[1];
        } else x=t[x].son[0];
    }
    if (y) splay(y,0);
    return y;
}
int get_nex(int key) {
    int x=root;
    int y=0;
    while (x) {
        if (t[x].key>=key) {
            y=x;
            x=t[x].son[0];
        } else x=t[x].son[1];
    }
    if (y) splay(y,0);
    return y;
}
int main(void) {
    t[0].key=INF;
    int n=read();
    int now=2;
    int size=0;
    int ans=0;
    while (n--) {
        int opt=read();
        int x=read();
        if (!size) {
            now=opt;
            insert(x);
            size++;
        } else if (opt==now) {
            insert(x);
            size++;
        } else if (opt!=now) {
            size--;
            int pre=get_pre(x);
            int nex=get_nex(x);
            int PRE=std:: abs(x-t[pre].key);
            int NEX=std:: abs(x-t[nex].key);
            if (PRE<=NEX) {
                del(pre);
                ans=(ans+PRE)%MOD;
            } else {
                del(nex);
                ans=(ans+NEX)%MOD;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值