zoukankan      html  css  js  c++  java
  • BZOJ1208: [HNOI2004]宠物收养所 平衡树 Treap 模板题

    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

    用treap储存宠物收养所的信息,插入和删除操作分别模拟新进来和领养走的。每次当有领养者和宠物进行匹配时,查询前驱和后继与当前的数值的绝对值之差的较小者进行取用。一起做就好,不用刻意分开讨论,因为注意到题目中的“同一时间呆在收养所中的,要么全是宠物,要么全是领养者”。注意如果某一时刻都领养走了的话需要进行特判然后把type(当前收养所中的是人还是狗的信息)

    Source

      1  
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <algorithm>
      5 #include <cstdlib>
      6 using namespace std;
      7 const int MAXN=1000001;
      8 const int INF=1e9;
      9 const int mod=1000000;
     10 struct Treap
     11 {
     12     int ch[2],key,tms,size,dat;
     13 }treap[MAXN];
     14 int root,tot;
     15 inline int cmp(int x,int tar)
     16 {
     17     if(tar==treap[x].dat) return -1;
     18     return (tar<treap[x].dat?0:1);
     19 }
     20 inline void maintain(int x)
     21 {
     22     treap[x].size=treap[x].tms;
     23     if(treap[x].ch[0]) treap[x].size+=treap[treap[x].ch[0]].size;
     24     if(treap[x].ch[1]) treap[x].size+=treap[treap[x].ch[1]].size;
     25 }
     26 inline void rotate(int &x,int d)
     27 {
     28     int p=treap[x].ch[d^1];
     29     treap[x].ch[d^1]=treap[p].ch[d];
     30     treap[p].ch[d]=x;
     31     maintain(x);
     32     maintain(p);
     33     x=p;
     34 }
     35 void ins(int &x,int tar)
     36 {
     37     if(!x) 
     38     {
     39         tot++;
     40         treap[tot].key=rand();
     41         treap[tot].dat=tar;
     42         treap[tot].tms=1;
     43         treap[tot].size=1;
     44         x=tot;
     45         return;
     46     }
     47     int d=cmp(x,tar);
     48     if(d==-1) treap[x].tms++;
     49     else
     50     {
     51         ins(treap[x].ch[d],tar);
     52         if(treap[treap[x].ch[d]].key>treap[x].key) rotate(x,d^1);
     53     }
     54     maintain(x);
     55 }
     56 void del(int &x,int tar)
     57 {
     58     int d=cmp(x,tar);
     59     if(d==-1)
     60     {
     61         if(treap[x].tms>1) treap[x].tms--;
     62         else
     63         {
     64             if(!treap[x].ch[0]&&!treap[x].ch[1]) x=0;
     65             else if(!treap[x].ch[0]&&treap[x].ch[1]) x=treap[x].ch[1];
     66             else if(treap[x].ch[0]&&!treap[x].ch[1]) x=treap[x].ch[0];
     67             else
     68             {
     69                 int t=(treap[treap[x].ch[0]].key>treap[treap[x].ch[1]].key?1:0);
     70                 rotate(x,t);
     71                 del(treap[x].ch[t],tar);
     72             }
     73         }
     74     }
     75     else del(treap[x].ch[d],tar);
     76     maintain(x);
     77 }
     78 inline int GetPre(int x,int tar)
     79 {
     80     int con=-INF;
     81     while(x)
     82     {
     83         int d=cmp(x,tar);
     84         if(d==-1) x=treap[x].ch[0];
     85         else if(d) 
     86         {
     87             con=max(con,treap[x].dat);
     88             x=treap[x].ch[1];
     89         }
     90         else x=treap[x].ch[0];
     91     }
     92     return con;
     93 }
     94 inline int GetNext(int x,int tar)
     95 {
     96     int con=INF;
     97     while(x)
     98     {
     99         int d=cmp(x,tar);
    100         if(d==-1) x=treap[x].ch[1];
    101         else if(d) x=treap[x].ch[1];
    102         else
    103         {
    104             con=min(con,treap[x].dat);
    105             x=treap[x].ch[0];
    106         }   
    107     }
    108     return con;
    109 }
    110 int n,i,op,t,k,l,r,ans,num;
    111 int main(int argc, char *argv[])
    112 {
    113      
    114     scanf("%d",&n);
    115     while(n--)
    116     {
    117         scanf("%d%d",&op,&k);
    118         if(num==0)
    119         {
    120             ins(root,k);
    121             t=op;
    122             num++;
    123         }
    124         else if(t==op)
    125         {
    126             ins(root,k);
    127             num++;
    128         }
    129         else {
    130             l=GetPre(root,k),r=GetNext(root,k);
    131             if(k-l<=r-k) ans=(ans+k-l)%mod,del(root,l);
    132             else ans=(ans+r-k)%mod,del(root,r);
    133             num--;
    134             if(num==0) t=-1;
    135         }
    136     }
    137     printf("%d
    ",ans);
    138     return 0;
    139 }
    140 
  • 相关阅读:
    五种Sublime text 3同时快速编辑多行内容
    update 更新某个字段自动加1
    oracle 一行记录被锁
    事件
    练习题1
    语法
    开始js
    js简述
    概述
    软连接
  • 原文地址:https://www.cnblogs.com/BeyondW/p/5731227.html
Copyright © 2011-2022 走看看