zoukankan      html  css  js  c++  java
  • 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #define inf 0x7fffffff
      5 #define P 1000000
      6 using namespace std;
      7 struct SplayNode
      8 {
      9     SplayNode *fa,*ch[2];
     10     SplayNode();
     11     int data,size,num;
     12     int chr() {return this==fa->ch[1];}
     13     void updata() {size=ch[1]->size+ch[0]->size+num;}
     14 }*null;
     15 SplayNode::SplayNode() {fa=ch[0]=ch[1]=null; num=1; size=0;}
     16 int n,sum;
     17 inline int read() {char c; int ans=0; while ((c=getchar())==' ' || c=='
    ' || c=='
    '); ans=c-'0'; while (isdigit(c=getchar())) ans=ans*10+c-'0'; return ans;}
     18 namespace Splay
     19 {
     20     SplayNode *Root[2];
     21     void MakeTree()
     22     {
     23         null=new SplayNode;
     24         *null=SplayNode();
     25         Root[0]=null;
     26         Root[1]=null;
     27     }
     28     void rotate(SplayNode *x,int num)
     29     {
     30         SplayNode *r=x->fa;
     31         if (x==null || r==null) return;
     32         int t=x->chr();
     33         r->ch[t]=x->ch[t^1];
     34         r->ch[t]->fa=r;
     35         if (r->fa==null) Root[num]=x;
     36         else r->fa->ch[r->chr()]=x;
     37         x->fa=r->fa;
     38         x->ch[t^1]=r;
     39         r->fa=x;
     40         r->updata();
     41         x->updata();
     42     }
     43     void splay(SplayNode *x,SplayNode *r,int num)
     44     {
     45         for (;x->fa!=r;rotate(x,num))
     46             if (x->fa->fa!=r)
     47                 if (x->chr()==x->fa->chr()) rotate(x->fa,num);
     48                 else rotate(x,num);
     49     }
     50     void insert(int v,int num)
     51     {
     52         SplayNode *r=Root[num];
     53         if (Root[num]==null)
     54         {
     55             Root[num]=new SplayNode;
     56             Root[num]->data=v;
     57             Root[num]->updata();
     58             return;
     59         }
     60         while (r->ch[r->data<v]!=null) r=r->ch[r->data<v];
     61         r->ch[r->data<v]=new SplayNode;
     62         r->ch[r->data<v]->data=v;
     63         r->ch[r->data<v]->fa=r;
     64         splay(r->ch[r->data<v],null,num);
     65     }
     66     SplayNode *find(int v,int num)
     67     {
     68         int lsum=inf,rsum=inf;
     69         SplayNode *r=Root[num],*l=null,*l1=null;
     70         while (r!=null)
     71         {
     72             if (r->data==v)
     73             {
     74                 splay(r,null,num);
     75                 return r;
     76             }
     77             if (abs(r->data-v)<lsum && r->data<v) lsum=abs(r->data-v),l=r;
     78             if (abs(r->data-v)<rsum && r->data>v) rsum=abs(r->data-v),l1=r;
     79             r=r->ch[r->data<v];
     80         }
     81         if (lsum<=rsum)
     82         {
     83             sum=(sum+lsum)%P;
     84             splay(l,null,num);
     85             return l;
     86         }
     87         else
     88         {
     89             sum=(sum+rsum)%P;
     90             splay(l1,null,num);
     91             return l1;
     92         }
     93     }
     94     void pre(SplayNode *x,SplayNode *&ans)
     95     {
     96         SplayNode *r=x->ch[0];
     97         if (r==null) return;
     98         while (r->ch[1]!=null) r=r->ch[1];
     99         ans=r;
    100     }
    101     void suc(SplayNode *x,SplayNode *&ans)
    102     {
    103         SplayNode *r=x->ch[1];
    104         if (r==null) return;
    105         while (r->ch[0]!=null) r=r->ch[0];
    106         ans=r;
    107     }
    108     void del(int v,int num)
    109     {
    110         SplayNode *r=find(v,num);
    111         SplayNode *q=null,*p=null; pre(Root[num],q); suc(Root[num],p);
    112         if (q==null && p==null) 
    113         {
    114             Root[num]=null;
    115             return;
    116         }      
    117         if (q==null) 
    118         {
    119             splay(p,null,num);
    120             Root[num]->ch[0]=null;
    121             Root[num]->updata();
    122             return;
    123         }   
    124         if (p==null)
    125         {
    126             splay(q,null,num);
    127             Root[num]->ch[1]=null;
    128             Root[num]->updata();
    129             return;
    130         }
    131         splay(q,null,num);
    132         splay(p,q,num);
    133         p->ch[0]=null;
    134         p->updata();
    135     }
    136 }
    137 int main()
    138 {
    139     n=read();
    140     Splay::MakeTree();
    141     for (int i=1;i<=n;i++)
    142     {
    143         int x,temp;
    144         temp=read(); x=read();
    145         if (Splay::Root[temp^1]->size!=0)
    146             Splay::del(x,temp^1);
    147         else
    148             Splay::insert(x,temp);
    149         sum%=P;
    150     } 
    151     printf("%d
    ",sum%P);
    152     return 0;
    153 }
    View Code

    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

     
    —Anime Otaku Save The World.
  • 相关阅读:
    HTML5表单_form
    HTML第二篇
    快速创建1000个目录
    centos7忘记root密码重置
    centos7更改网卡名称
    centos7磁盘在线扩容
    sqlserver开启远程访问
    安装php后无法动态加载库
    编译openssl和Apache报错checking for SSL_CTX_new... no
    centos6.5设置key登录
  • 原文地址:https://www.cnblogs.com/DMoon/p/5308333.html
Copyright © 2011-2022 走看看