BZOJ1208 Pet Adoption Institute Treap/splay/set

Source: Internet
Author: User

Lazy with Set

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <Set>5#include <algorithm>6 #defineRep (i,l,r) for (int i=l;i<r;i++)7 #defineUpdate (a) (ans+=a)%=10000008typedefLong Longll;9 using namespacestd;Ten Set<int>s; One Const intinf=0x7fffffff; A ll ans; - intN,pre,opt,a; - intMain () the { -Cin>>N; - s.clear (); - S.insert (INF); +S.insert (-inf); -Rep (I,0, N) { +scanf"%d%d",&opt,&a); A         if(s.size () = =2){ atPre=opt; - S.insert (a); -}Else if(pre!=opt) { -             intl=*--S.lower_bound (a); -             intr=*S.lower_bound (a); -             if(l!=-inf&&a-l<=r-a) { inUpdate (A-l); - s.erase (L);  to}Else{ +Update (R-a); - S.erase (r); the             } *}ElseS.insert (a); $     }Panax Notoginsengprintf"%d", ans); -     return 0; the}
View Code 1208: [HNOI2004] Pet Adoption time limit:10 Sec Memory limit:162 MB
submit:5019 solved:1926
[Submit] [Status] [Discuss] 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 adopter 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 that wants to be adopted. The trait of the pet is closest to that of the pet, and if the pet has a characteristic value of a, there are two adopters who wish to adopt a pet with a characteristic value of a-B and a+b, then the adoptive person with a A-B trait 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). "Mission description" You get a year in which the adoptive and adopted pets come to the adoption, and you want to calculate the total dissatisfaction of all the adoptive adopters who have adopted the pet. At the beginning of the year, there were neither pets nor adopters in the adoption.

Input

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

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 INPUT5
0 2
0 4
1 3
1 2
1 5
Sample OUTPUT3
(ABS (3-2) + ABS (2-4) = 3, the last adopter has no pets to adopt)
HINT Source

Splay

BZOJ1208 Pet Adoption Institute Treap/splay/set

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.