HDU 4585 Balance Tree Treap

Source: Internet
Author: User

HDU 4585 Balance Tree Treap

Question: Give n groups of numbers. The first number is id, and the second number is level. Each input one outputs the fight between this person and the other person, this person will find the person with the most similar level. If there are two people with the same level as him, he will choose a fight with a lower level than him.

Train of Thought: Use treap to complete, you can use STL to water, but you need to practice Treap to write the Balance Tree. For everyone's level, we find the ranking t of his level, then find the number of the T-1 and the number of t + 1, and then compare. Output after the discussion, PS: no good template, you can only learn one acceptable on the internet .......

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include using namespace std; typedef long ll; const int inf = 0x3f3f3f; const int maxn = 100010; class Treap {public: struct Treap_Node {Treap_Node * left, * right; int value, fix, weight, size; // The fix is generated randomly to ensure that the binary tree does not degrade. wweight is the weight, size: subtree size} * root, * null; Treap () {null = new struct Treap_Node; null-> size = 0; null-> weight = 0; null-> value = inf; null-> left = null; null-> right = null; null-> fix = inf; root = null;} void Treap_Print (Treap_Node * P) {// output if (P! = Null) {Treap_Print (P-> left); printf ("% d \ n", P-> value); Treap_Print (P-> right );}} void Treap_Left_Rotate (Treap_Node * & a) {// Treap_Node * B = a-> right; a-> right = B-> left; b-> left = a; B-> size = a-> size; a-> size = a-> left-> size + a-> right-> size + a-> weight; a = B;} void Treap_Right_Rotate (Treap_Node * &) {// The Treap_Node * B = a-> left; a-> left = B-> right; B-> right =; b-> size = a-> size; a-> size = a-> left-> size + a-> right-> size + a-> weight; a = B;} int Treap_Find (Treap_Node * P, int value) {// check whether there is a value. if (P = null) return 0; if (P-> value = value) return 1; else if (value
     
      
Value) return Treap_Find (P-> left, value); else return Treap_Find (P-> right, value);} void Treap_Insert (Treap_Node * & P, int value) {// insert a number if (P = null) {P = new Treap_Node; P-> left = P-> right = null; // left and right sons are empty P-> value = value; P-> fix = rand (); P-> weight = 1; P-> size = 1 ;} else if (value = P-> value) {P-> weight ++;} else if (value
      
        Value) {Treap_Insert (P-> left, value); if (P-> left-> fix
       
         Fix) Treap_Right_Rotate (P);} else {Treap_Insert (P-> right, value); if (P-> right-> fix
        
          Fix) Treap_Left_Rotate (P);} P-> size = P-> left-> size + P-> right-> size + P-> weight ;} void Treap_Delete (Treap_Node * & P, int value) {// delete a number if (P = null) return; if (value
         
           Value) Treap_Delete (P-> left, value); else if (value> P-> value) Treap_Delete (P-> right, value ); else if (P-> weight> 1) P-> weight --; else if (P-> left = NULL) & (P-> right = NULL )) {delete P; P = NULL;} else {if (P-> left-> fix
          
            Right-> fix) Treap_Left_Rotate (P); else Treap_Right_Rotate (P); Treap_Delete (P, value );} p-> size = P-> left-> size + P-> right-> size + P-> weight;} int Treap_pred (Treap_Node * P, int value, treap_Node * optimal) {if (P = null | value = P-> value) return optimal-> value; if (P-> value
           
             Right, value, P); else return Treap_pred (P-> left, value, optimal);} int Treap_succ (Treap_Node * P, int value, Treap_Node * optimal) {if (P = null | value = P-> value) return optimal-> value; if (P-> value) return Treap_succ (P-> left, value, P); else return Treap_succ (P-> right, value, optimal);} int Treap_Findkth (Treap_Node * P, int k) {// calculate the K-th largest number if (P = null) return 0; int t = P-> left-> size; if (k
            
              Left, k); else if (k> t + P-> weight) return Treap_Findkth (P-> right, k-(t + P-> weight )); else return P-> value;} int Treap_Rank (Treap_Node * P, int value, int cur) {int t = P-> left-> size; if (value = P-> value) return t + cur + 1; else if (value
             
               Value) return Treap_Rank (P-> left, value, cur); else return Treap_Rank (P-> right, value, t + cur + P-> weight );} void Treap_erase (Treap_Node * & P) {if (P-> left! = Null) Treap_erase (P-> left); if (P-> right! = Null) Treap_erase (P-> right); delete P ;}}; int id [5000010]; int main () {int n, a, B; while (scanf ("% d", & n )! =-1) {if (n = 0) break; Treap tree; int ans1, ans2; memset (id, 0, sizeof (id )); scanf ("% d", & a, & B); printf ("% d 1 \ n", a); id [B] = a; tree. treap_Insert (tree. root, B); for (int I = 1; I
              
               

 

Related Article

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.