1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MB
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
0 2
0 4
1 3
1 2
1 5
Sample Output
3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
分析:平衡树入门题,关键在于查找一个点的前驱和后继,并进行删除操作。
用平衡树记录排队的人和宠物,并记录当前的树是代表排队的人还是宠物,当代表的含义相同的时候,直接插入SBT中,不同的时候,从SBT中找到满足条件的前驱或者后继,更新答案后在SBT中删除该值即可。
program pro;
const modd=1000000;
var
n:longint;
left,right,s,key:array[0..1000000]of longint;
tot,i,sum,q,pr,ans,pre,succ,xx,yy,root,kind:longint;
procedure rightrorate(var x:longint);
var
k:longint;
begin
k:=left[x];
left[x]:=right[k];
right[k]:=x;
s[k]:=s[x];
s[x]:=s[left[x]]+s[right[x]]+1;
x:=k;
end;
procedure leftrorate(var x:longint);
var
k:longint;
begin
k:=right[x];
right[x]:=left[k];
left[k]:=x;
s[k]:=s[x];
s[x]:=s[left[x]]+s[right[x]]+1;
x:=k;
end;
procedure maintain(var x:longint; bo:boolean);
begin
if bo=false then
begin
if s[left[left[x]]]>s[right[x]] then rightrorate(x)
else
if s[right[left[x]]]>s[right[x]] then
begin
leftrorate(left[x]);
rightrorate(x);
end
else exit;
end
else
begin
if s[right[right[x]]]>s[left[x]] then leftrorate(x)
else
if s[left[right[x]]]>s[left[x]] then
begin
rightrorate(right[x]);
leftrorate(x);
end
else exit;
end;
maintain(left[x],false);
maintain(right[x],true);
maintain(x,true);
maintain(x,false);
end;
procedure insert(var x:longint; y:longint);
begin
if x=0 then
begin
inc(tot);
x:=tot;
key[x]:=y; s[x]:=1;
left[x]:=0; right[x]:=0;
end
else
begin
inc(s[x]);
if y<key[x] then insert(left[x],y)
else insert(right[x],y);
maintain(x,y>=key[x]);
end;
end;
function delete(var x:longint; y:longint):longint;
begin
dec(s[x]);
if (key[x]=y)or((y<key[x])and(left[x]=0))or((y>key[x])and(right[x]=0)) then
begin
delete:=key[x];
if (left[x]=0)or(right[x]=0) then x:=left[x]+right[x]
else key[x]:=delete(left[x],key[x]+1);
end
else
if y<key[x] then delete:=delete(left[x],y)
else delete:=delete(right[x],y);
end;
function getpred(var x,y:longint):longint;
begin
if x=0 then exit(y);
if y<=key[x] then getpred:=getpred(left[x],y)
else
begin
getpred:=getpred(right[x],y);
if getpred=y then getpred:=key[x];
end;
end;
function getsucc(var x,y:longint):longint;
begin
if x=0 then exit(y);
if key[x]<=y then getsucc:=getsucc(right[x],y)
else
begin
getsucc:=getsucc(left[x],y);
if getsucc=y then getsucc:=key[x];
end;
end;
begin
readln(n);
root:=0; ans:=0; sum:=0; kind:=0;
for i:=1 to n do
begin
readln(q,pr);
if (sum=0)or(kind=q) then
begin
kind:=q;
inc(sum);
insert(root,pr);
end
else
begin
pre:=getpred(root,pr);
succ:=getsucc(root,pr);
xx:=pr-pre;
yy:=succ-pr;
dec(sum);
if xx=0 then
begin
ans:=(ans+yy)mod modd;
delete(root,succ);
continue;
end;
if yy=0 then
begin
ans:=(ans+xx)mod modd;
delete(root,pre);
continue;
end;
if xx<=yy then
begin
ans:=(ans+xx)mod modd;
delete(root,pre);
end
else
begin
ans:=(ans+yy)mod modd;
delete(root,succ);
end;
end;
end;
writeln(ans);
end .