Bzoj_3224_ General Balance tree (TREAP)

Source: Internet
Author: User

Describe

Treap template questions. The following actions are supported:

1. Insert;

2. Deletion;

3.rank (x);

4.kth (k);

5.pre (x);

6.SUC (x);

Analysis

When you write more, you'll be ripe ... Yesterday read the book, debugging today when knocked many times, feeling full of tree+heap.

P.S.

The 1.RANK,KTH,PRE,SUC function can be written in a non-recursive form.

The 2.remove function is written in two ways:

(1). LRJ petition on the method:

If the node you want to delete has only one subtree, use that subtree instead of the node you want to delete, and delete the node you want to delete. If there are two subtrees, rotate the Keng Gen subtree with a small priority value, and then recursively delete the node to be deleted in another subtree, each time this rotation will reduce the tree, and eventually the first case.

(2). The method that appears in the solution:

If the node you want to delete has no subtree, you can delete it directly. If there are subtrees, either two or one, rotate the subtree with a small precedence value (note that null precedence is set to INF), and then recursively delete nodes in another subtree, and each of these rotations will reduce the tree. The first situation will eventually be achieved.

#include <cstdio>#include<cstdlib>using namespacestd;Const intoo=~0u<<1;structtreap{structnode{Node* ch[2]; intV,s,r,c; Node (intV,node *t): V (v) {ch[0]=ch[1]=t; R=rand (); S=c=1; } BOOL operator< (ConstNode &AMP;RHS)Const{returnr<RHS.R;} voidPush_up () {s=ch[0]->s+ch[1]->s+C;} }*root,*NULL; Treap () {NULL=NewNode0,0); NULL->s=NULL->c=0; NULL->r=Oo; Root=NULL; }    voidRotate (node* &o,BOOLd) {Node* k=o->ch[!d]; o->ch[!d]=k->ch[d]; k->ch[d]=N; o->push_up (); K->push_up (); o=K; }    voidInsert (node* &o,intx) {        if(o==NULL) o=NewNode (x,NULL); Else{            if(o->v==x) {o->c++; o->s++; }            Else{                BOOLD=x>o->v; Insert (o-ch[d],x); if(o->ch[d]<o) Rotate (o,!d); o-push_up (); }        }    }    voidRemove (node* &o,intx) {        if(o->v==x) {            if(o->c>1) o->c--; Else{                if(o->ch[0]!=NULL&&o->ch[1]!=NULL){                    BOOLd=o->ch[0]<o->ch[1]; Rotate (o,d); Remove (o-ch[d],x); }                Else{node* u=o; if(o->ch[0]==NULL) o=o->ch[1]; Elseo=o->ch[0]; Deleteu; }            }        }        Else{            BOOLD=x>o->v; Remove (o-ch[d],x); }        if(o!=NULL) o->push_up (); }    intKTH (node* o,intk) {        ints=o->ch[0]->s+o->C; if(k>o->ch[0]->s&&k<=s)returnO->W; if(k<=o->ch[0]->s)returnKTH (o->ch[0],k); Else returnKTH (o->ch[1],k-s); }    intRank (node *o,intx) {        ints=o->ch[0]->s+o->B; if(X==O-&GT;V)returno->ch[0]->s+1; if(X&LT;O-&GT;V)returnRank (o->ch[0],x); Else returnS+rank (o->ch[1],x); }    intPreintx) {Node* t=Root; intret=0;  while(t!=NULL){            if(t->v<x) {ret=t->v; T=t->ch[1]; }            Elset=t->ch[0]; }        returnret; }    intSucintx) {Node*t=Root; intret=0;  while(t!=NULL){            if(t->v>x) {ret=t->v; T=t->ch[0]; }            Elset=t->ch[1]; }        returnret; }}tree;intMain () {intn,a,b; scanf ("%d",&N);  while(n--) {scanf ("%d%d",&a,&b); Switch(a) { Case(1): Tree.insert (TREE.ROOT,B); Break;  Case(2): Tree.remove (TREE.ROOT,B); Break;  Case(3):p rintf ("%d\n", Tree.rank (tree.root,b)); Break;  Case(4):p rintf ("%d\n", tree.kth (tree.root,b)); Break;  Case(5):p rintf ("%d\n", Tree.pre (b)); Break;  Case(6):p rintf ("%d\n", TREE.SUC (b)); Break; }    }    return 0;}

Bzoj_3224_ General Balance tree (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.