Wikioi 1285 stretch tree delete operation

Source: Internet
Author: User

Description Description

Recently, Q opened a pet adoption Institute. Adoption provides two services: Adoption of pets abandoned by the owner and adoption of the pets by the new owner.
Each adopter wants to adopt a pet that he or she is satisfied with. According to the requirements of the adopter, a q uses a special formula he has invented, the characteristic value a (A is a positive integer, A <2 ^ 31) of the pet to be adopted by the adopter is obtained, and he also gives each pet a characteristic value. In this way, he can easily handle the whole process of pet adoption. There are always two situations for pet adoption: too many abandoned pets or too many people who want to adopt pets, there are too few pets.
1. if there are too many abandoned pets, if a adopter arrives, the pet that the adopter wants to adopt features, then it will adopt a pet with the feature value closest to a that is not currently adopted. (The characteristics of any two pets cannot be the same, and the characteristics of any two pets are not the same.) If there are two pets that meet the requirements, that is, if there are two pets whose feature values are A-B and A + B, the adopter will adopt the pet whose feature value is a-B.
2. There are too many people who adopt pets. If a adopted pet arrives, which of the following can be adopted? The adopter who can adopt the PET is the adopter who wants the feature value of the pet closest to that of the pet. If the feature value of the pet is, there are two owners who want to adopt a pet with the characteristics of A-B and A + B respectively, then the recipient with the characteristics of a-B will successfully adopt the pet.

A adopter has adopted a pet with a feature value of A, and the feature value of the pet that it wants to adopt is B, then the dissatisfaction of the adopter is ABS (a-B ).


[Task description]
After you get a year, the number of adopted and adopted pets will reach the adoption site. I hope you will calculate the total dissatisfaction of all adopted pets. At the beginning of the year, there were no pets or breeders in the adoption site.

Input description Input description

The first behavior is a positive integer n, n <= 80000, indicating the total number of pets and owners who come to the adoption site during the year. The next n rows describe the pets and owners who came to the adoption site in the order of the arrival time. Each row has two positive integers, A and B. A = 0 indicates the pet, a = 1 indicates the pet, B indicates the characteristics of the pet, or the characteristics of the pet to be adopted. (If you stay in the adoption center at the same time, either it's all pets, or it's all owners. The number of pets and owners will not exceed 10000)

Output description Output description

There is only one positive integer, indicating the total dissatisfaction of all pets adopted in a year after mod 1000000.

Sample Input Sample Input

5

0 2

0 4

1 3

1 2

1 5

Sample output Sample output

3

Data range and prompt Data size & hint

(ABS (3-2) + ABS (2-4) = 3, the last adopter can adopt without pets)


Nima knocked on this question for a morning, and t was knocked out during the NAP, and t was so outrageous that it was 10010 Ms. I relied on it and I didn't know how to change it.

Forget it. Let's do a simple stretching tree first. Let's take a look at this question later and keep the code. Which of the following experts can help me find out where T is. Thank you very much !!!

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <queue> # include <set> # include <cmath> # include <bitset> # define MEM (, b) memset (a, B, sizeof (A) # define INF 1000000007 # define maxn 10010 using namespace STD; typedef long ll; typedef unsigned long ull; int pre [maxn]; // the front-end int key [maxn]; // the key value int ch [maxn] [2]; // left and right children. 0 indicates left children, 1 is the right child int root, TOT, total; // root node, number of nodes void Reset () {tot = root = Total = 0; MEM (CH, 0); MEM (PRE, 0); MEM (key, 0);} void treaval (int x) {If (x) {treaval (CH [x] [0]); printf ("Node % 2D: Left son % 2D right son % 2D parent node % 2D, val = % 2D \ n ", X, CH [x] [0], CH [x] [1], pre [X], key [x]); treaval (CH [x] [1]) ;}} void debug () {printf ("% d \ n", root); treaval (Root );} void newnode (Int & R, int father, int K) {// in the r position, father is father, create a Value Point. R = ++ tot; Total ++; Pre [R] = Father; key [R] = K; ch [r] [0] = CH [r] [1] = 0;} void rotate (int x, int kind) // kind = 1 indicates the right hand, kind = 0 indicates left-hand {int y = pre [X]; ch [y] [! Kind] = CH [x] [kind]; Pre [CH [x] [kind] = y; ch [x] [kind] = y; if (pre [y]) CH [pre [y] [CH [pre [y] [1] = y] = X; pre [x] = pre [y]; Pre [y] = x;} void splay (int x, int goal) // stretch X to the target position {While (pre [x]! = Goal) {If (pre [pre [x] = goal) rotate (x, CH [pre [x] [0] = X ); else {int y = pre [X]; int kind = CH [pre [y] [0] = y; If (CH [y] [kind] = X) rotate (x ,! Kind), rotate (x, kind); else rotate (Y, kind), rotate (x, kind) ;}} if (! Goal) root = x; // UPDATE root to x} int insert (Int & X) // insert value x {int r = root; while (CH [r] [Key [R] <X]) {If (Key [R] = x) {splay (R, 0); return r ;} R = CH [r] [Key [R] <X];} newnode (CH [r] [x> key [R], R, X ); X = CH [r] [x> key [R]; // return the ID of the new node splay (x, 0); Return X; // return the number of the inserted node} int find (int x) // The node number {int r = root; If (! R) Return-INF; // The tree is not created. If (Key [R] = x) return r is not found; while (CH [r] [x> key [R]) {r = CH [r] [x> key [R]; if (Key [R] = x) return r;} return-INF; // not found} int getmax (int x) // obtain the maximum node number of this subtree {While (CH [x] [1]) x = CH [x] [1]; return X ;} int getmin (int x) // obtain the minimum node number of the subtree {While (CH [x] [0]) x = CH [x] [0]; return X;} void Delete (Int & X) // delete node X {If (! Ch [x] [0] &! Ch [x] [1]) // if this node is a leaf node {If (x = root) {reset (); return;} int y = pre [x]; if (CH [y] [0] = x) CH [y] [0] = 0; else ch [y] [1] = 0; pre [x] = 0;} else if (CH [x] [0] &! Ch [x] [1]) {int M = getmax (CH [x] [0]); If (x = root) root = m; splay (m, x); Pre [m] = pre [X];} else if (! Ch [x] [0] & Ch [x] [1]) {int M = getmin (CH [x] [1]); If (x = root) root = m; splay (M, x); Pre [m] = pre [X];} else if (CH [x] [0] & Ch [x] [1]) {int M = getmax (CH [x] [0]); splay (m, x); ch [m] [1] = CH [x] [1]; Pre [CH [x] [1] = m; If (x = root) root = m ;}} int get_pre (Int & X) // find the precursor value of node X. If no value is found, inf {x = CH [x] [0] is returned. if (! X) Return-INF; while (CH [x] [1]) x = CH [x] [1]; Return key [X];} int get_next (Int & X) {x = CH [x] [1]; If (! X) return INF; while (CH [x] [0]) x = CH [x] [0]; Return key [X];} int main () {int sum = 0, A, B, n; CIN> N; reset (), reset (); For (INT I = 0; I <n; I ++) {scanf ("% d", & A, & B); If (! A) // according to the requirements of the first and second points of the question, know that the operations of pets and people are the same {If (total <1 & total> 0) {int BB = B; int r = insert (bb); int R1 = r, R2 = r; int x = get_pre (R1); int y = get_next (R2 ); if (B-x <= Y-B) {sum = (sum + B-x) % 1000000; Delete (R1), delete (r );} else {sum = (sum + Y-B) % 1000000; Delete (R2), delete (r);} total-= 2;} else if (! TOT &&! TOT) newnode (root, 0, B); else insert (B);} else {If (total> 0) {int BB = B; int r = insert (bb ); int R1 = r, R2 = r; int x = get_pre (R1); int y = get_next (R2); // dog. debug (); If (B-x <= Y-B) {sum = (sum + B-x) % 1000000; // cout <sum <''<x <'' <Y <Endl; Delete (R1), delete (r );} else {sum = (sum + Y-B) % 1000000; // cout <sum <''<x <'' <Y <Endl; delete (R2), delete (r);} // dog. debug (); Total-= 2 ;}else {If (TOT) insert (B); else newnode (root, 0, B );}}} cout <sum <Endl; return 0 ;}


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.