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->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