HDU 4217 Data Structure?/treap

Source: Internet
Author: User

Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4217

Use line tree to write, high efficiency.

This problem was written in C language before the treap water.

Now touch the C + + rewrite again ...

Insert with no duplicate elements removes the K-belt garbage Collection, as follows:

1#include <cstdio>2#include <cstdlib>3#include <iostream>4#include <algorithm>5typedefLong Longll;6 Const intMax_n =300100;7 structnode{8     intV, S, fix;9Node *ch[2];TenInlinevoid  One     Set(int_fix,int_v =0,int_s =0, Node *p =NULL) { Afix = _fix, V = _v, s =_s; -ch[0] = ch[1] =p; -     } theInlinevoidpush_up () { -s = ch[0]->s + ch[1]->s +1; -     } - }; + intrun () { -     Static intx =184082857; +X + = (x <<2) |1; A     returnx; at } - structtreap{ -Node *tail, *NULL, *Root; - Node Stack[max_n]; -     inttop; -Node *Store[max_n]; in     voidinit () { -Tail = &stack[0]; to         NULL= tail++; +         NULL-Set(0x7fffffff); -Root =NULL; thetop =0; *     } $Node *newnode (intv) {Panax NotoginsengNode *p =NULL; -         if(top) p = store[--top]; the         Elsep = tail++; +P->Set(Run (), V,1,NULL); A         returnp; the     } +     voidRotate (node* &x,intd) { -Node *k = x->ch[!d]; $X-&GT;CH[!D] = k->Ch[d]; $K-&GT;CH[D] =x; -K->s = x->s; -X->push_up (); thex =K; -     }Wuyi     voidInsert (node* &x,intv) { the         if(x = =NULL){ -x =NewNode (v); Wu             return; -}Else { About             intD = v > x->v; $Insert (x->Ch[d], v); -             if(X->ch[d]->fix < X->fix) rotate (x,!d); -X->push_up (); -         } A     } +     voidDel (node* &x,intv) { the         if(x = =NULL)return; -x->s--; $         if(X->v = =v) { the             if(x->ch[0] ==NULL|| x->ch[1] ==NULL){ thestore[top++] =x; thex = x->ch[0] ==NULL? x->ch[1]: x->ch[0]; the}Else { -                 intD = x->ch[0]->fix < x->ch[1]->fix; inRotate (x,!d); theDel (x->ch[!d], v); the             } About}Else { theDel (x->ch[v>x->v], v); the         } the         if(X! =NULL) x->push_up (); +     } -     intFind_kth (Node *x,intk) { the         intt =0;Bayi          for (;;) { thet = x->ch[0]->s; the             if(k = = T +1) Break; -             Else if(K < T +1) x = x->ch[0]; -             ElseK-= t +1, x = x->ch[1]; the         } the         returnX->v; the     } the }treap; - intMain () { the #ifdef LOCAL theFreopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout);94 #endif the ll ans; the     intT, N, M, K, tmp, C =1; thescanf"%d", &t);98      while(t--){ AboutTreap.init (), ans =0; -scanf"%d%d", &n, &m);101          for(inti =1; I <= N; i++) 102 Treap.insert (Treap.root, i);103          while(m--){104scanf"%d", &k); theAns + = TMP =treap.find_kth (Treap.root, k);106 Treap.del (Treap.root, TMP);107         }108printf"Case %d:%lld\n"C++, ans);109     } the     return 0;111}
View Code

HDU 4217 Data Structure?/treap

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.