BZOJ1208:[HNOI2004]宠物收养所——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=1208

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)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

Input

第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

Output

仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

Sample Input

5
0 2
0 4
1 3
1 2
1 5

Sample Output

3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

————————————————————————————————

这题看似要用两棵平衡树,但其实不然。

只是我们需要记录当前平衡树里装的是人还是动物。

这个可以用sum累加减来记录。

那么我们为了表示出宠物(或人)走了,我们需要删除操作。

删除操作具体看http://blog.csdn.net/clove_unique/article/details/50630280

注意本代码的删除操作的含义是删掉根节点,所以我们需要将我们要删的点splay到根才可以。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int N=80001;
const int p=1000000;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int fa[N],tr[N][2],key[N],cnt[N],size[N];
int root,sz;
inline void clear(int x){
    fa[x]=tr[x][0]=tr[x][1]=key[x]=cnt[x]=size[x]=0;
    return;
}
inline bool get(int x){
    return tr[fa[x]][1]==x;
}
inline void update(int x){
    if(x){  
    size[x]=cnt[x];  
    if(tr[x][0])size[x]+=size[tr[x][0]];  
    if(tr[x][1])size[x]+=size[tr[x][1]];  
    }  
    return;
}
inline void rotate(int x){
    int old=fa[x],oldf=fa[old],which=get(x);
    tr[old][which]=tr[x][which^1];fa[tr[old][which]]=old;  
    fa[old]=x;tr[x][which^1]=old;fa[x]=oldf;
    if(oldf)tr[oldf][tr[oldf][1]==old]=x;
    update(old);update(x);
    return;
}
inline void splay(int x){
    int f=fa[x];
    while(f){
    if(fa[f])rotate((get(x)==get(f)?f:x));
    rotate(x);f=fa[x];
    }
    root=x;
    return;
}
inline void insert(int v){
    if(!root){
    sz++;tr[sz][0]=tr[sz][1]=fa[sz]=0;
    key[sz]=v;cnt[sz]=size[sz]=1;root=sz;
    return;
    }
    int now=root,f=0;
    while(233){
    if(key[now]==v){
        cnt[now]++;update(now);update(f);splay(now);
        break;
    }
    f=now;
    now=tr[now][key[now]<v];
    if(!now){
        sz++;tr[sz][0]=tr[sz][1]=0;fa[sz]=f;
        key[sz]=v;cnt[sz]=size[sz]=1;
        tr[f][key[f]<v]=sz;
        update(f);splay(sz);
        break;
    }
    }
    return;
}
inline int pre(){//前驱
    if(cnt[root]>1)return root;
    int now=tr[root][0];  
    while(tr[now][1])now=tr[now][1];  
    return now;  
}     
inline int nxt(){//后继
    if(cnt[root]>1)return root;
    int now=tr[root][1];  
    while(tr[now][0])now=tr[now][0];  
    return now;
}
inline void del(){
    if(cnt[root]>1){
    cnt[root]--;return;
    }
    if(!tr[root][0]&&!tr[root][1]){
    clear(root);root=0;return;
    }
    if(!tr[root][0]){  
    int oldroot=root;root=tr[root][1];fa[root]=0;clear(oldroot);return;
    }
    else if(!tr[root][1]){  
    int oldroot=root;root=tr[root][0];fa[root]=0;clear(oldroot);return;
    }
    int leftbig=pre(),oldroot=root;  
    splay(leftbig);
    fa[tr[oldroot][1]]=root;
    tr[root][1]=tr[oldroot][1];
    clear(oldroot);
    update(root);
    return;
}
inline int query(int v){
    int k1=pre(),k2=nxt(),ans;
    del();
    if(!k1){
    ans=key[k2]-v;
    splay(k2);
    del();
    }else if(!k2){
    ans=v-key[k1];
    splay(k1);
    del();
    }else if(v-key[k1]<=key[k2]-v){
    ans=v-key[k1];
    splay(k1);
    del();
    }else{
    ans=key[k2]-v;
    splay(k2);
    del();
    }
    return ans;
}
int main(){
    int n=read();
    int sum=0,ans=0;
    for(int i=1;i<=n;i++){
    int a=read();
    int b=read();
    if(!a)sum++;
    else sum--;
    if(sum==0){
        ans+=abs(key[root]-b)%p;
        ans%=p;
        del();
    }else if(sum>0){
        insert(b);
        if(a){
        ans+=query(b)%p;
        ans%=p;
        }
    }else{
        insert(b);
        if(!a){
        ans+=query(b)%p;
        ans%=p;
        }
    }
    }
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2017-12-27 17:23  luyouqi233  阅读(160)  评论(0编辑  收藏  举报