C + + Road Advanced--splay tree (Pet adoption)

Source: Internet
Author: User
Tags abs

1285 Pet Adoption Institute

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

Recently, a Q opened a pet house. The adoption agency offers two services: Adopt pets abandoned by their owners and allow new owners to adopt them.
Each adopter wants to adopt a pet that he or she is satisfied with, ah Q according to the adopter's request, through a special formula invented by himself, the adoptive pet's characteristic value A (A is a positive integer, a<2^31), and he gives each pet in the adoption of a characteristic value. So that he can easily deal with the entire adoption of pets, pet adoption will always have two things: abandoned pets too much or want to adopt a pet too many people, and too few pets.
1. When a pet is abandoned, if an adopter is to be adopted, the adoptive pet is expected to have a characteristic value of a, then it will adopt a pet that is closest to a in a pet that is currently not adopted. (None of the two pet's feature values can be the same, and any two adopters may not have the same characteristic value for the adopted pet) if there are two pets that meet the requirements, that is, there are two pets. Their characteristics are a-B and a+b. Then the adoptive person will adopt the pet with a A-B characteristic value.
2. There are too many people to adopt a pet, and if a pet is adopted, which one will adopt it? The adopter who is able to adopt it, is the one who wishes to adopt the pet's characteristic value closest to the pet's characteristic value, if the pet has a characteristic value of a, there are two adopters who want to adopt a pet with a characteristic value of a-B and a+b, then the one with a A-B feature will succeed in adopting the pet.

An adopter has adopted a pet with a characteristic value of a, and it wants to adopt a pet with a characteristic value of B, so the adopter's dissatisfaction is abs (A-a).


"Task description"
You've got a year in which the adoptive and adopted pets come to the adoption, and you want to count the total dissatisfaction of all the adoptive adopters who adopted the pet. At the beginning of the year, there were neither pets nor adopters in the adoption.

Enter a description Input Description

The first act is a positive integer n,n<=80000 that represents the total number of pets and adopters who have come to the adoption agency during the year. The next n lines, in chronological order of arrival, describe the circumstances of the year when the pets and adopters came to the adoption. Each line has two positive integers a, b, of which a=0 represents a pet, a=1 represents an adopter, B denotes a pet's characteristic value, or a trait that the adopter wishes to adopt. (at the same time in the adoption, either all pets, or all adoptions, the number of pets and adopters will not exceed 10,000)

Output description Output Description

There is only a positive integer that represents the total amount of dissatisfaction of all adopters adopting a pet in a year after mod 1000000 results.

Sample input Sample Input

5

0 2

0 4

1 3

1 2

1 5

Sample output Sample Output

3

Data range and Tips Data Size & Hint

(ABS (3-2) + ABS (2-4) = 3, the last adopter has no pets to adopt)

Code:

1#include <cstdio>2#include <iostream>3#include <algorithm>4 #defineMAXN 101005 6 using namespacestd;7 8 intn,tr[maxn][2],num[maxn],fa[maxn],kind,root,size,after,before,ans;9 Ten voidRotateint&k,intx) One    { A         inty=fa[x],z=Fa[y],l,r; -         if(tr[y][0]==X) l=0;ElseL=1; -r=l^1; the         if(k==y) k=x; -            Else -           if(tr[z][0]==y) tr[z][0]=x;Elsetr[z][1]=x; -Fa[x]=z; Fa[y]=x; fa[tr[x][r]]=y; +TR[Y][L]=TR[X][R]; tr[x][r]=y;  -    } +  A voidSplay (int&k,intx) at     { -       inty,z;  -        while(x!=k) -         { -y=Fa[x]; -z=Fa[y]; in           if(y!=k) -              { to                  if((tr[z][0]==y) ^ (tr[y][0]==x)) rotate (k,x); +                      Elserotate (k,y);  -               } the rotate (k,x);  *          }  $              Panax Notoginseng     } -  the voidInsint&k,intXintLast ) +     { A       if(k==0)  the        { +size++; -k=size; $num[k]=x; $fa[k]=Last ; - splay (root,k); -            return; the        } -      if(X<num[k]) ins (tr[k][0],X,K);ElseINS (tr[k][1],x,k); Wuyi     } the  - voidDelintx) Wu     { - splay (root,x);  About         if(tr[x][0]*tr[x][1]==0) root=tr[x][0]+tr[x][1];  $             Else -               { -                   intk=tr[k][1]; -                    while(tr[k][0]) k=tr[k][0]; Atr[k][0]=tr[x][0]; +fa[tr[x][0]]=K; theroot=tr[x][1]; -               } $fa[root]=0;  the     } the  the voidAsk_before (intKintx) the    { -        if(k==0)return ; in        if(x>=Num[k]) the            { theBefore=K; AboutAsk_before (tr[k][1],x);  the            } the         ElseAsk_before (tr[k][0],x);  the    } +     - voidAsk_after (intKintx) the    {Bayi          if(k==0)return ; the          if(x<=Num[k]) the             { -After=K; -Ask_after (tr[k][0],x); the             } the         ElseAsk_after (tr[k][1],x);  the        }     the  - intMain () the   { thescanf"%d",&N);  the     intf,x;94        for(intI=0; i<n;i++) the       { thescanf"%d%d",&f,&x);  the         if(!root)98         { AboutKind=F;  -Ins (root,x,0); 101         }102         Else103            if(f==kind) Ins (root,x,0);104                Else the                 {106before=after=-1; 107 Ask_before (root,x); 108 Ask_after (root,x);109                   if(before==-1) {ans+=num[after]-x;ans%=1000000;d El (after);} the                       Else if(after==-1) {ans+=x-num[before];ans%=1000000;d El (before);}111                                 Else the                                     {113                                         if(x-num[before]>num[after]-x) {ans+=num[after]-x;ans%=1000000;d El (after);} the                                             Else{ans+=x-num[before];ans%=1000000;d El (before);} the                                     } the                        117                 }118      }119printf"%d", ans);  -}

C + + Road Advanced--splay tree (Pet adoption)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.