[Balanced binary tree] pet adoption Institute

Source: Internet
Author: User
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 deal with 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 to say, there are two pets whose feature values are A-B and A + B respectively, then the adopter will adopt the pet whose feature value is a-B. 2. There are too many people who have adopted pets. If a adopted pet arrives, which one can adopt it? The adopter who can adopt the PET is the adopter who wants the feature value of the pet closest to the feature value 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 a year, you will be able to 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 first acts as 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 has only one positive integer, results After mod 1000000 indicate the total dissatisfaction of all pets adopted during the year. Sample input50 20 41 31 21 5 sample output3 (ABS (3-2) + ABS (2-4) = 3, the last adopter can be adopted without pets)

directly use SBT to find the start and end of a vertex, and compare the result.
accode:

# Include <cstdio> # include <cstdlib> # include <algorithm> # include <string> # include <cstring> const int maxn = 80010; const int mod = 1000000; const int INF = 0x3f3f3f; Class SBT {PRIVATE: int LC [maxn], RC [maxn], TOT; int key [maxn], SZ [maxn], T; void Zig (Int & T) {int TMP = Lc [T]; LC [T] = RC [TMP]; RC [TMP] = T; SZ [TMP] = SZ [T]; SZ [T = SZ [LC [T] + SZ [RC [T] + 1; t = T = TMP; return ;} void zag (Int & T) {int t MP = RC [T]; RC [T] = Lc [TMP]; LC [TMP] = T; SZ [TMP] = SZ [T]; SZ [T] = SZ [LC [T] + SZ [RC [RC [T] + 1; t = TMP; return;} vovovoid maintain (Int & T, bool flag) {If (! Flag) {If (SZ [LC [LC [T]> SZ [RC [T]) Zig (t ); if (SZ [RC [LC [T]> SZ [RC [T]) {zag (LC [T]); zig (t) ;}else return ;} el{ if (SZ [RC [RC [T]> SZ [LC [T]) zag (t ); if (SZ [LC [RC [T]> SZ [LC [T]) {Zig (RC [T]); zag (t) ;}else return ;} maintain (LC [T], 0); maintain (RC [T], 1); maintain (T, 0); maintain (T, 1); return ;} void ins (Int & T, int v) {If (! T) {key [T = ++ tot] = V; SZ [T] = 1; return;} + SZ [T]; If (v <key [T]) INS (LC [T], V); else ins (RC [T], V); maintain (T, V> = Key [T]); return ;} int int del (Int & T, int v) {-- SZ [T]; If (if (V = Key [T] | V v <key [T] &! LC [T] | V> key [T] &! RC [T]) {int TMP = Key [T]; If (! LC [T] |! RC [T]) t = Lc [T] + RC [T]; else key [T] = del (LC [T], key [T]); Return TMP ;} if (v <key [T]) return del (LC [T], V); else return del (RC [T], V);} int Prev (INT t, int V, int ans) {If (! T) return ans; If (Key [T] <v ans = STD: max (ANS, key [T]); If (v <key [T]) return Prev (LC [T], V, ANS); else return Prev (RC [T], V, ANS);} int succ (int t, int V, int ans) {If (! T) return ans; If (Key [T]> = V) ans = STD: min (ANS, key [T]); If (v <key [T]) return succ (LC [T], V, ANS); else return succ (RC [[T], V, ANS);} public: sbsbt (): T (0 ), tot (0) {memset (LC, 0, sizeof Lc); memset (RC, 0, sizeof RC); memset (SZ, 0, sizeof sz); memset (key, 0, sizeof key);} void ins (INT v) {ins (T, V);} void del (INT v) {del (T, V );} int Prev (INT v) {return Prev (T, V ,~ INF) ;}int succ (INT v) {return succ (T, V, INF) ;}int size () {return SZ [T] ;}} pet, human; inline int getint () {int res = 0; char TMP; while (! Isdigit (TMP = getchar (); Do res = (RES <3) + (RES <1) + TMP-'0 '; while (isdigit (TMP = getchar (); Return res;} int main () {freopen ("pet. in "," r ", stdin); freopen (" pet. out "," W ", stdout); int ans = 0; For (INT n = getint (), Val; n; -- N) switch (scanf ("\ n"), getchar () {Case '0': val = getint (); If (human. size () {int Prev = human. prev (VAL), succ = human. succ (VAL); If (succ-Val <Val-PREV) {(ANS + = succ-Val) % = MOD; human. del (succ);} else {(ANS + = Val-PREV) % = MOD; human. del (prev) ;}} else pet. INS (VAL); break; Case '1': val = getint (); If (PET. size () {int Prev = pet. prev (VAL), succ = pet. susucc (VAL); If (succ-Val <Val-PREV) {(ANS + = succ-Val) % =. del (succ);} else {(ANS + = Val-PREV) % = MOD; pet. del (prev) ;}} else human. INS (VAL);} printf ("% d \ n", ANS); 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.