#P2286. [HNOI2004] 宠物收养场

[HNOI2004] 宠物收养场

Description

Fanfan opened a pet adoption center. The center offers two services: taking in abandoned pets and allowing new owners to adopt these pets.

Each adopter hopes to adopt a satisfactory pet. Based on the adopter’s requirements, Fanfan uses a special formula of his own invention to obtain the adopter’s desired feature value aa (aa is a positive integer, a<231a < 2^{31}). He also assigns a feature value to each pet in the shelter. This makes handling the entire adoption process convenient. The shelter often faces two situations: too many abandoned pets, or too many adopters and too few pets.

When there are too many abandoned pets, if an adopter arrives desiring a feature value aa, they will adopt the currently unadopted pet whose feature value is closest to aa. (No two pets share the same feature value, and no two adopters share the same desired feature value.) If there are two candidates with feature values aba-b and a+ba+b, the adopter will adopt the pet with feature value aba-b.

When there are too many adopters, if a pet arrives, which adopter can adopt it? The adopter who can adopt it is the one whose desired feature value is closest to the pet’s feature value. If the pet’s feature value is aa and there are two adopters desiring aba-b and a+ba+b, then the adopter with desired value aba-b will successfully adopt the pet.

If an adopter adopts a pet with feature value aa while their desired feature value is bb, the adopter’s dissatisfaction is ab|a-b|.

You are given the sequence of arrivals of adopters and pets at the shelter over a year. Please compute the total dissatisfaction of all adopters who successfully adopted a pet. At the beginning of the year, there are neither pets nor adopters in the shelter.

Input Format

The first line contains an integer nn, n80000n \leq 80000, the total number of pets and adopters arriving at the shelter during the year. The next nn lines, in order of arrival time, describe these arrivals. Each line contains two integers a,ba, b, where a{0,1}a \in \{0, 1\} (a=0a = 0 indicates a pet, a=1a = 1 indicates an adopter), and bb is the pet’s feature value or the adopter’s desired feature value. (At any given time, the shelter contains either only pets or only adopters, and their counts do not exceed 1000010000.)

Output Format

Output a single integer: the total dissatisfaction of all adopters who successfully adopted a pet, modulo 10000001000000.

5                  
0 2                      
0 4                         
1 3
1 2
1 5

3

Hint

Sample explanation:

Note: 32+24=3|3-2| + |2-4| = 3. The last adopter has no pet to adopt.

Translated by ChatGPT 5