bzoj1208 [HNOI2004]宠物收养所 splay入门

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


题目描述很清楚,把人和狗都当作节点插入,然后每次查找就行了,删除时就把它的后继拿上来,有注释的


/**************************************************************
    Problem: 1208
    User: yslcl1234
    Language: C++
    Result: Accepted
    Time:108 ms
    Memory:3012 kb
****************************************************************/
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=111111;
const int mod=1000000;
const int inf=0x3fffffff;
int ch[maxn][2],f[maxn],val[maxn],num[2]={0},rt=0,top=0;
inline int read()
{
    char c=getchar();
    int s=0;
    while(c<'0'||c>'9')c=getchar();
    do{
        s=s*10+c-'0';
        c=getchar();
    }while(c>='0'&&c<='9');
    return s;
}
inline void newnode(int y,int &x,int a)
{
    x=++top;
    val[x]=a;
    f[x]=y;
    ch[x][1]=ch[x][0]=0;
}
inline void rotate(int x,int c) 
{
    int y=f[x],z=f[y];
    ch[y][!c]=ch[x][c];
    f[ch[x][c]]=y;
    f[x]=z;
    if(z)ch[z][ch[z][1]==y]=x;
    ch[x][c]=y;
    f[y]=x;
}
inline void splay(int x,int goal)
{
    while(f[x]!=goal)
    {
        int y=f[x],z=f[y];
        if(z==goal)
        {
            if(ch[y][0]==x)rotate(x,1);
            else rotate(x,0);
        }
        else{
            if(ch[z][0]==y)
            {
                if(ch[y][0]==x)rotate(y,1),rotate(x,1);
                else rotate(x,0),rotate(x,1);
            }
            else{
                if(ch[y][1]==x)rotate(y,0),rotate(x,0);
                else rotate(x,1),rotate(x,0);
            }
        }
    }
    if(goal==0)rt=x;
}
inline void insert(int a)
{
    int x=rt;
    while(ch[x][val[x]<a])x=ch[x][val[x]<a];
    newnode(x,ch[x][val[x]<a],a);
    splay(top,0);
}
inline int min1(int a,int b){return a<b?a:b;}//看可以节 
inline int max1(int a,int b){return a>b?a:b;}//省时间不 
inline int findnear(int a,int f)//找前驱后继 
{
    int x=rt,m=inf*f;
    while(x)
    {
        if(val[x]==a)return a;
        if(f>0){if(val[x]>a)m=min1(m,val[x]);}//如果f为真,则为寻找大于a的最小值,否则为小于a的最大值 
        else if(val[x]<a&&f<0)m=max1(m,val[x]);
        x=ch[x][val[x]<a];
    }
    return m;
}
inline int find(int a)
{
    int x=rt;
    while(x)
    {
        if(val[x]==a)return x;
        x=ch[x][val[x]<a];
    }
    return 0;
}
inline void del(int x)
{
    splay(x,0);
    int t=ch[rt][1];
    while(ch[t][0])t=ch[t][0];
    splay(t,rt);
    ch[t][0]=ch[rt][0];
    f[ch[rt][0]]=t;
    f[t]=0;
    rt=t;
}
int main()
{
    int n,i,ans=0;
    n=read();
    newnode(0,rt,-inf);
    newnode(rt,ch[rt][1],inf);
    for(i=1;i<=n;i++)
    {
        int a=read(),b=read();
        if(num[!a]==0)//num[0]表示有几条狗 ,num[1]表示有几个人
        {
            num[a]++;
            insert(b);
        }
        else{
            int minn=findnear(b,1),maxx=findnear(b,-1);
            ans=(ans+min1(minn-b,b-maxx))%mod;
            if(b-maxx<=minn-b)del(find(maxx));
            else del(find(minn));
            num[!a]--;
        }
    }
    printf("%d",ans%mod);
}



猜你喜欢

转载自blog.csdn.net/yslcl12345/article/details/51001262
今日推荐