【splay】BZOJ1208宠物收养所

来源:互联网 发布:微信数据占用手机内存 编辑:程序博客网 时间:2024/04/29 16:25

1208: [HNOI2004]宠物收养所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3620  Solved: 1364
[Submit][Status]

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,最后一个领养者没有宠物可以领养)

HINT

Source

Splay

var n,ans,a,b,i,pre,suc,root,tot,min,typ:longint;    t:array[0..100001]of record         l,r,f,v:longint;       end;procedure left(aa:longint);var fa:longint;begin  fa:=t[aa].f;  t[aa].f:=t[fa].f;  if t[t[fa].f].l=fa then t[t[fa].f].l:=aa;  if t[t[fa].f].r=fa then t[t[fa].f].r:=aa;  t[fa].f:=aa;  t[fa].r:=t[aa].l;  t[t[aa].l].f:=fa;  t[aa].l:=fa;end;procedure right(aa:longint);var fa:longint;begin  fa:=t[aa].f;  t[aa].f:=t[fa].f;  if t[t[fa].f].l=fa then t[t[fa].f].l:=aa;  if t[t[fa].f].r=fa then t[t[fa].f].r:=aa;  t[fa].f:=aa;  t[fa].l:=t[aa].r;  t[t[aa].r].f:=fa;  t[aa].r:=fa;end;procedure splay(now,goal:longint);var fa:longint;begin  while t[now].f<>goal do    begin      fa:=t[now].f;      if t[fa].f=goal then         begin          if t[fa].l=now then right(now) else left(now);         end            else              if t[t[fa].f].l=fa then                 begin                   if t[fa].l=now then                      begin                        right(fa);                        right(now);                      end                    else                      begin                        left(now);                        right(now);                      end;                 end                  else                    begin                      if t[fa].r=now then                        begin                          left(fa);                          left(now);                        end                         else                           begin                             right(now);                             left(now);                           end;                    end;    end;    if goal=0 then root:=now;end;procedure find(x:longint);var now:longint;begin  now:=root;  repeat     if (t[now].v<=x) then pre:=now;     if (t[now].v>=x) then suc:=now;     if t[now].v=x then exit;     if x  until now=0;end;procedure insert(v:longint);var now:longint;begin  now:=root;  while true do    begin      if v<=t[now].v then        begin          if t[now].l<>0 then now:=t[now].l            else begin                   inc(tot);                   t[tot].f:=now;                   t[tot].v:=v;                   t[now].l:=tot;                   splay(tot,0);                   exit;                end;          end            else             if t[now].r<>0 then now:=t[now].r               else begin                      inc(tot);                      t[tot].f:=now;                      t[tot].v:=v;                      t[now].r:=tot;                      splay(tot,0);                      exit;                   end;    end;end;procedure del(x:longint);var l,r,new,rk:longint;begin  splay(x,0);  l:=t[x].l;  r:=t[x].r;  if (l=0)and(r<>0) then   begin     root:=r;     t[r].f:=0;     exit;   end;  if (r=0)and(l<>0) then    begin      root:=l;      t[l].f:=0;      exit;    end;  if (r=0)and(l=0) then      begin        root:=0;        t[root].v:=0;        t[root].l:=0;        t[root].r:=0;        typ:=-1;        //tot:=0;不知道为什么加了这句就T了= =        exit;      end;  new:=0; rk:=l;  while (rk<>0) do   begin     new:=rk; rk:=t[rk].r;   end;  splay(new,root);  root:=t[root].l;  t[root].f:=0;  t[root].r:=t[x].r;  t[t[x].r].f:=root;end;begin  readln(n);  ans:=0;  tot:=0;  root:=0;  typ:=-1;  for i:=1 to n do    begin      readln(a,b);      if typ=-1 then typ:=a;      if a=typ then       begin        insert(b);       end        else         begin           pre:=0; suc:=0; min:=0;           find(b);           if suc=0 then min:=pre;           if pre=0 then min:=suc;           if (pre=0)and(suc=0) then             begin               typ:=a;               insert(b);               continue;             end; //这个地方没注意。。wa了很久           if (pre<>0)and(suc<>0) then                if abs(t[pre].v-b)<=abs(t[suc].v-b) then min:=pre else min:=suc;           ans:=(ans+abs(t[min].v-b))mod 1000000;           del(min);          end;    end;    writeln(ans);end.


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 货车卖了没过户怎么办 微交易出金不了怎么办 直播时出现央视影音客户端怎么办 qq账号暂时无法登录怎么办 饿了吃东西胃疼怎么办 早上不吃饭胃疼怎么办 孕晚期胃疼呕吐怎么办 胃疼了一晚上怎么办 微信视频图像倒立怎么办 ps链接图层锁定怎么办 慕课过时间了怎么办 异地恋又要考研怎么办 阴阳师手机绑定上限了怎么办 高考口令卡丢了怎么办 网易将军令换手机了怎么办 须弥bb有弱点土怎么办 战网密码忘记了怎么办 梦三账号忘了怎么办 快手手机号被注册了怎么办 快手该手机号已注册怎么办 手机号被别人注册了快手怎么办 快手显示手机号已注册怎么办 163的邮箱忘了怎么办 河长制账号密码忘了怎么办 网易登录名忘了怎么办 网易通行证安全手机忘了怎么办 美团数据获取失败怎么办 扣扣图片加载不出来怎么办 电脑qq最小化后不见了怎么办 uwp桌面快捷图标显示异常怎么办 网易云自动切歌怎么办 捡到小米手机怎么办才能自己用 dnf启动安装程序出错怎么办 苹果手机相册视频下载出错怎么办 苹果7软件闪退怎么办 微信上有钱账号就是怎么办 手机网速不给力怎么办 xp系统登录密码忘了怎么办 手机百度云网络出错怎么办 百度账号提示异常风险怎么办 刷xp框架卡米怎么办