Bzoj topic 1208: [HNOI2004] Pet Adoption (SBT)

Source: Internet
Author: User

1208: [HNOI2004] Pet Adoption Time limit: ten Sec Memory Limit: 162 MB
Submit: 5440 Solved: 2093
[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

[Submit] [Status] [Discuss] 

HOME Back

Well, just open is to judge whether the book is empty when used Tree[root].size==0wa two times,, with Root==0 on the

AC Code

/************************************************************** problem:1208 user:kxh1995 language:c++ Resu lt:accepted time:908 Ms memory:2368 kb****************************************************************/#include &lt ;stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #define MOD 1000000#define INF  0xfffffffstruct s {int key,left,right,size;  }TREE[100100];  int top,root;      void Left_rot (int &x) {int y=tree[x].right;      Tree[x].right=tree[y].left;      Tree[y].left=x;      Tree[y].size=tree[x].size;      tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;  X=y;      } void Right_rot (int &x) {int y=tree[x].left;      Tree[x].left=tree[y].right;      Tree[y].right=x;      Tree[y].size=tree[x].size;      Tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size;  X=y; } void maintain (int &x,bool flag) {if (Flag==false) {if (tree[tree[tree[x].left].left].size> tree[tree[x].right].size) Right_rot (x); else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size) {Left_r                  OT (Tree[x].left);                 Right_rot (x);      } else return;          } else {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size) Left_rot (x); else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size) {rig                  Ht_rot (Tree[x].right);              Left_rot (x);      } else return;      } maintain (Tree[x].left,false);      Maintain (tree[x].right,true);      Maintain (x,true);  Maintain (X,FALSE);          } void Insert (int &x,int key) {if (x==0) {x=++top;          tree[x].left=0;          tree[x].right=0;          tree[x].size=1;      Tree[x].key=key;  } else {tree[x].size++;        if (key<tree[x].key) insert (Tree[x].left,key);           else insert (Tree[x].right,key);      Maintain (X,key>=tree[x].key);      }} int remove (int &x,int key) {tree[x].size--;      if (key>tree[x].key) remove (Tree[x].right,key);          else if (key<tree[x].key) remove (Tree[x].left,key);                  else if (tree[x].left!=0&&tree[x].right==0) {int temp=x;                  X=tree[x].left;              return temp; } else if (!tree[x].left&&tree[x].right!=0) {int                      Temp=x;                      X=tree[x].right;                  return temp;                          } else if (!tree[x].left&&!tree[x].right) {                          int temp=x;                          x=0;        return temp;              } else {int temp=tree[x].right;                          while (Tree[temp].left) Temp=tree[temp].left;                          Tree[x].key=tree[temp].key;                      Remove (Tree[x].right,tree[temp].key);      }} int Getmin (int x) {while (tree[x].left) X=tree[x].left;  return tree[x].key;      } int Getmax (int x) {while (tree[x].right) x=tree[x].right;  return tree[x].key;        } int pred (int &x,int y,int key) {if (x==0) {if (y==0) return INF;    return tree[y].key;      } if (Key>tree[x].key) return pred (Tree[x].right,x,key);  else return pred (Tree[x].left,y,key);        } int succ (int &x,int y,int key) {if (x==0) {if (y==0) return INF;    return tree[y].key; } if (Key<tree[x].key) return succ (Tree[x].left,x,key);  else return succ (Tree[x].right,y,key);    }int find (int &x,int key) {if (x==0) return 0;    if (Key==tree[x].key) return 1;    if (Key<tree[x].key) return find (Tree[x].left,key); else return find (Tree[x].right,key);}    int main () {int n;        while (scanf ("%d", &n)!=eof) {int kind,tkind,k;        top=root=0;        int ans=0;            for (int i=0;i<n;i++) {scanf ("%d%d", &kind,&k); if (root==0| |                Kind==tkind) {insert (ROOT,K);            Tkind=kind;                } else if (Find (root,k)) {remove (root,k);                    } else {int a=pred (ROOT,0,K);                int B=SUCC (ROOT,0,K);                    printf ("+++++%d%d\n", A, b);                    if (A==inf&&b==inf) continue; if (ABS (A-K) <=abs (b-k))                   {ans= (Ans+abs (a-k))%mod;                    Remove (root,a);                        } else {ans= (Ans+abs (b-k))%mod;                    Remove (ROOT,B);    }}} printf ("%d\n", ans%mod); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj topic 1208: [HNOI2004] Pet Adoption (SBT)

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.