1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 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)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。
Input
第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
Output
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
Sample Input
0 2
0 4
1 3
1 2
1 5
Sample Output
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
HINT
Source
依旧模板:
#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; }