bzoj 1208

1208: [HNOI2004]寵物收養所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 9775  Solved: 3918
[Submit][Status][Discuss]

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)。【任務描述】你獲得了一年當中,領養者和被收養寵物到來收養所的狀況,但願你計算全部收養了寵物的領養者的不滿意程度的總和。這一年初始時,收養所裏面既沒有寵物,也沒有領養者。php

Input

第一行爲一個正整數n,n<=80000,表示一年當中來到收養所的寵物和領養者的總數。接下來的n行,按到來時間的前後順序描述了一年當中來到收養所的寵物和領養者的狀況。每行有兩個正整數a, b,其中a=0表示寵物,a=1表示領養者,b表示寵物的特色值或是領養者但願領養寵物的特色值。(同一時間呆在收養所中的,要麼全是寵物,要麼全是領養者,這些寵物和領養者的個數不會超過10000個)ios

Output

僅有一個正整數,表示一年當中全部收養了寵物的領養者的不滿意程度的總和mod 1000000之後的結果。spa

Sample Input

5
0 2
0 4
1 3
1 2
1 5

Sample Output

3
(abs(3-2) + abs(2-4)=3,最後一個領養者沒有寵物能夠領養)

HINT

 

Source

依舊模板:code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MOD=1000000;
const int MAXN=1000009;
int Abs(int a) { return a>=0?a:-a; }
int fa[MAXN],ch[MAXN][2],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{
    root=sz=0;
    memset(ch,0,sizeof(ch));
    memset(fa,0,sizeof(fa));
    memset(cnt,0,sizeof(cnt));
    memset(size,0,sizeof(size));
}
inline void clear(int x) { fa[x]=ch[x][0]=ch[x][1]=cnt[x]=size[x]=0; }
inline int get(int x) { return ch[fa[x]][1]==x; }
inline void update(int x)
{
    if(x){
        size[x]=cnt[x];
        if(ch[x][0]) size[x]+=size[ch[x][0]];
        if(ch[x][1]) size[x]+=size[ch[x][1]];
    }
}
inline void rotate(int x)
{
    int father=fa[x],ffather=fa[father],which=get(x);
    ch[father][which]=ch[x][!which];fa[ch[father][which]]=father;
    ch[x][!which]=father;fa[father]=x;
    fa[x]=ffather;
    if(ffather) ch[ffather][ch[ffather][1]==father]=x;
    update(father);
    update(x);
}
inline void splay(int x)
{
    for(int father;(father=fa[x]);rotate(x))
        if(fa[father])
            rotate((get(x)==get(father)?father:x));
    root=x;
}
inline void insert(int x)
{
    if(root==0) { root=++sz;fa[sz]=ch[sz][0]=ch[sz][1]=0;cnt[sz]=size[sz]=1;key[sz]=x;return; }
    int now=root,father=0;
    while(1){
        if(key[now]==x) { cnt[now]++;update(now);update(father);splay(now);return; }
        father=now;
        now=ch[father][key[now]<x];
        if(now==0){
            sz++;
            fa[sz]=father;
            ch[father][key[father]<x]=sz;
            ch[sz][0]=ch[sz][1]=0;
            cnt[sz]=size[sz]=1;
            key[sz]=x;
            update(father);
            splay(sz);
            return;
        }
    }
}
inline int find(int x)//找到x的位置
{
    int now=root,ans=0;
    while(1){
        if(x<key[now]) now=ch[now][0];
        else{
            if(ch[now][0]) ans+=size[ch[now][0]];
            if(x==key[now]) { splay(now);return ans+1; }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
inline int rank(int x)//找到排名爲x的數
{
    int now=root;
    while(1){
        if(ch[now][0]&&x<=size[ch[now][0]]) now=ch[now][0];
        else{
            int tmp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
            if(x<=tmp) return key[now];
            x=x-tmp;
            now=ch[now][1];
        }
    }
}
inline int pre()//找前驅
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}
inline int suf()//找後繼
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}
inline void del(int x)//刪去一個x
{
    find(x);
    if(cnt[root]>1) { cnt[root]--;update(root);return; }
    else if(!ch[root][0]&&!ch[root][1]) { root=sz=0;clear(root);return; }
    else if(!ch[root][0]){
        int oldroot=root;
        root=ch[root][1];
        fa[root]=0;
        clear(oldroot);
        return;
    }else if(!ch[root][1]){
        int oldroot=root;
        root=ch[root][0];
        fa[root]=0;
        clear(oldroot);
        return;
    }
    int leftbig=pre(),oldroot=root;
    splay(leftbig);
    ch[root][1]=ch[oldroot][1];
    fa[ch[root][1]]=root;
    clear(oldroot);
    update(root);
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,a,b,flag=0,sum=0;
    ll ans=0;
    init();
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&a,&b);
        if(flag==a||sum==0){
            insert(b);
            flag=a;
            sum++;
        }else if(flag==(!a)&&sum){
            insert(b);
            if(cnt[root]>1) del(b);
            else{
                int tmp1=pre(),tmp2=suf();
                tmp1=(tmp1==0?-1:key[tmp1]);
                tmp2=(tmp2==0?-1:key[tmp2]);
                if(tmp1==-1) { ans+=Abs(b-tmp2);del(tmp2); }
                else if(tmp2==-1) { ans+=Abs(b-tmp1);del(tmp1); }
                else if(Abs(b-tmp1)<=Abs(b-tmp2)){
                    ans+=Abs(b-tmp1);
                    del(tmp1);
                }else{
                    ans+=Abs(b-tmp2);
                    del(tmp2);
                }
                ans%=MOD;
            }
            del(b);
            sum--;
        }
    }
    printf("%lld\n",ans);
    return 0;
}
相關文章
相關標籤/搜索