UESTC 395 Dynamic Query System--treap

Source: Internet
Author: User

Test instructions: let you maintain a collection, there are 8 kinds of operations:

1. I x Insert a number

2. R x Delete x

3. Total number of S output (collection size)

4. L x number of queries less than X

5. W k Query the number of K numbers from small to large in the collection

6. C x number of x queries

7. The smallest number in the MI query collection

8. MA query the largest number in the collection

A balanced tree simulation of the naked problem, direct registration and then change the details on the line.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;structnode{Node*ch[2]; intv,r,sz,cnt; voidUp () {sz = ch[0]->sz + ch[1]->sz +CNT;} intcmpintXConst    {        if(x = = v)return-1; returnX >v; }}*NULL,*Root;voidCreate (Node *&o,intv=0) {o=Newnode (); o->sz = o->cnt =1; o->v =v; o->r =rand (); o->ch[0] = o->ch[1] =NULL;}voidRotate (node *&o,intd) {Node*p = o->Ch[d]; o-&GT;CH[D] = p->ch[d^1]; P->ch[d^1] =o; o->up (),p->Up (); o=p;}intCountx (Node *o,intv) {     while(O! =NULL)    {        intD = o->CMP (v); if(d = =-1)            returnO->CNT; o= o->Ch[d]; }    return 0;}voidInsert (node *&o,intv) {    if(O = =NULL) {Create (O,V); return; }    intD = o->CMP (v); if(d = =-1) {o->cnt++; o-Up (); return; } insert (o-ch[d],v); if(O->r > o->ch[d]->r) rotate (o,d); if(O! =NULL) o-Up ();}voidDel (Node *&o,intv) {    if(O = =NULL)return; intD = o->CMP (v); if(d = =-1)    {        if(O->cnt >1) o->cnt--; Else        {            if(o->ch[0] ==NULL) O = o->ch[1]; Else if(o->ch[1] ==NULL) O = o->ch[0]; Else            {                intD2 = o->ch[1]->r < o->ch[0]->R; if(O-&GT;CH[D2] = =NULL) {delete o; o=NULL; return;                } rotate (O,D2); Del (o->ch[d2^1],v); }        }    }    ElseDel (o->ch[d],v); if(O! =NULL) o-Up ();}intKth (Node *o,intk) {    if(O = =NULL|| K = =0|| K > O->sz)return-1; intleft = o->ch[0]->sz; if(k > left && k <= left + o->CNT)returnO->v; if(k > left + o->CNT)returnKth (o->ch[1],k-left-o->CNT); returnKth (o->ch[0],k);}intCount (Node *o,intv) {    if(O = =NULL)return 0; intleft = o->ch[0]->sz; if(O->v = = v)returnLeft ; Else if(V < o->v)returnCount (o->ch[0],v); returnCount (o->ch[1],V) +left+o->CNT;}voidinit () {Create (NULL); NULL->sz =0; Root=NULL;}intMain () {intt,n,i,x; Charss[3]; scanf ("%d",&t);  while(t--) {init (); scanf ("%d",&N);  while(n--) {scanf ("%s", SS); if(ss[0] =='M')            {                if(ss[1] =='I') printf ("%d\n", Kth (Root,1)); Elseprintf ("%d\n", Kth (root,root->sz)); }            Else if(ss[0] =='I') {scanf ("%d",&x);            Insert (ROOT,X); }            Else if(ss[0] =='S') {printf ("%d\n",root->sz); }            Else if(ss[0] =='R') {scanf ("%d",&x);            Del (ROOT,X); }            Else if(ss[0] =='L') {scanf ("%d",&x); printf ("%d\n", Count (root,x)); }            Else if(ss[0] =='W') {scanf ("%d",&x); printf ("%d\n", Kth (root,x)); }            Else if(ss[0] =='C') {scanf ("%d",&x); printf ("%d\n", Countx (root,x)); }        }    }    return 0;}
View Code

UESTC 395 Dynamic Query System--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.