HDU 4006 Treap

Source: Internet
Author: User

Continue to test the template ...

1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <cstdio>5#include <ctime>6 using namespacestd;7 8 structNode9 {TenNode * ch[2]; One     intV, R, size, CNT; A     intcmpintx) -     { -         if(x = = v)return-1; the         returnX < v?0:1; -     }     -     voidMaintain () -     { +Size =CNT; -         if(ch[0]! = NULL) Size + = ch[0]->size; +         if(ch[1]! = NULL) Size + = ch[1]->size; A     } at }; -  - voidRotate (Node * & O,intd) - { -Node * k = o->ch[d ^1]; -O->ch[d ^1] = k->Ch[d]; inK-&GT;CH[D] =o; -O->maintain (); toK->maintain (); +o =K; - } the  * voidInsert (Node * & O,intx) $ {Panax Notoginseng     if(O = =NULL) -     { theo =NewNode (); +o->ch[0] = o->ch[1] =NULL; AO->v =x; theO->r =rand (); +O->size = o->cnt =1; -     } $     Else $     { -         intD = o->CMP (x); -         if(d = =-1 ) the         { -o->cnt++;Wuyio->size++; the         } -         Else Wu         { -Insert (o->Ch[d], x); About             if(O->ch[d]->r > o->R) $             { -Rotate (o, D ^1 ); -             } -             Else A             { +O->maintain (); the             } -         } $     } the } the  the intKTH (Node * o,intk) the { -     intTMP = (o->ch[0] = = NULL?0: o->ch[0]->size); in     if(k >= tmp +1&& k <= tmp + o->cnt)returnO->v; the     Else if(K < TMP +1)returnKTH (o->ch[0], k); the     Else returnKTH (o->ch[1], k-tmp-o->CNT); About } the  the voidClear (Node *o) the { +     if(o = = NULL)return ; -Clear (o->ch[0] ); theClear (o->ch[1] );Bayi     Deleteo; the } the  - intMain () - { the     intN, K; the      while(SCANF ("%d%d", &n, &k)! =EOF) the     { theNode * Root =NULL; -         Charop[2]; the          for(inti =0; I < n; i++ ) the         { thescanf"%s", op);94             if(op[0] =='I' ) the             { the                 inttmp; thescanf"%d", &tmp);98 Insert (root, tmp); About             } -             Else101             {102printf"%d\n", kth (Root, Root->size +1-k));103             }104         } the Clear (root);106Root =NULL;107     }108     return 0;109}

HDU 4006 Treap

Related Keywords:

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.