(Turn) the board of understanding about treap

Treap understanding on the board:

the definition of structure :( variable names generally balanced tree can not understand):
v:value node;
size: the number of child nodes (including their own);
cnt: number of copies of the same value;
l: left son;
r: Right son;

right hand:
his father left his son became, left his son to become his father's son and right;

 

1 void zig(int x)
2 {
3     int h=s[x].l;
4     s[x].l=s[x].r;s[h].r=x;
5     s[h].size=s[x].size;
6     up(x);
7     x=h;
8     return ;
9 }

L:
is the opposite becomes right-handed left-handed;

void zag(int x)
{
    int h=s[x].r;
    s[x].r=s[x].l;s[h].l=x;
    s[h].size=s[x].size;
    up(x);
    x=h;
}

Insert:
1.Insert the function has two functions, x and k;
where k takes the address would be more convenient;
k is the current node is the root node at the start of RT;
2. if the current node is 0, that is, a pass over the is 0, then this value never had before, so create a new node, that is,

1 s[k].size=s[k].cnt=1;
2 s[k].ran=rand();
3 s[k].v=x;
4 return ;

3. If this value is too previously, the copy cnt ++;
    known s [k] .l always less than k, s [k] .r always greater than K;
    therefore see the code

 1 void insert(int x,int &k)
 2 {
 3     if(!k)
 4     {
 5         s[k].size=s[k].cnt=1;
 6         s[k].ran=rand();
 7         s[k].v=x;
 8         return ;
 9     }
10     s[k].size++;
11     if(s[k].v==x) s[k].cnt++;
12     if(x>s[k].v)
13     {
14         insert(x,s[k].r);
15         if(s[s[k].l].ran<s[k].ran)zig(k);
16     }
17     else if(x<s[k].v)
18     {
19         insert(x,s[k].l);
20         if(s[s[k].l].ran<s[k].ran)zag(k);
21     }
22     return ;
23 }

Delete a node:
1. If there is no such node, directly return;
2. If the node is this value, and a copy of this point> 1, tr [k] .cnt -; tr [k] .size--;
3. If the left and right node node has only one value, the point is that his child node (after deleting this point is his child node when this point);
4. his son left the virtual value (balance according to the virtual tree value can increase efficiency) is less than the value of the right virtual son, then right-handed
5.else left;

inline void del(int x,int &k)
{
    if(!k) return ;
    if(tr[k].v==x)
    {
        if(tr[k].cnt>1)
        {
            r [k] .cnt - ;
            tr[k].size--;
        }
        else if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r;
        else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd) zig(k),del(x,k);
        else zag(k),del(x,k);
        return ;
    }
    tr[k].size--;
    if(tr[k].v>x) del(x,tr[k].l);
    else del(x,tr[k].r);
    return ;
}

Asked rankings:
1. If k == 0, 0 then return
2. If this value is the value of the current node, then left to return to his son, +1; (pay attention to plus 1, the left is less than my son, so +1, that is my position);
3. If the value of the current node is greater than the value of recursively to check his left subtree;
4. otherwise, recursively right subtree;

inline int qrnk(int x,int k)
{
    if(!k) return 0;
    if(tr[k].v==x) return tr[tr[k].l].size+1;
    else if(tr[k].v>x) return qrnk(x,tr[k].l);
    else return qrnk(x,tr[k].r)+tr[tr[k].l].size+tr[k].cnt;
}

Asks ranking is the value of x;
1. If you do not have this value, return 0;
the value of 2. If this ranking is less than the left subtree subtree points and ranking points less than the left subtree subtree plus the number of copies of his point;
then the TR return [K] .v;
3. If you want to check the value of less than or equal to the value of the left subtree son qnum return (X, TR [K] .L) ;;
4. otherwise recursively right subtree;

inline int qnum ( int x, int k)
{
    if(!k) return 0;
    if(x>tr[tr[k].l].size&&x<=tr[tr[k].l].size+tr[k].cnt) return tr[k].v;
    else if(x<=tr[tr[k].l].size) return qnum(x,tr[k].l);
    else return qnum(x-tr[tr[k].l].size-tr[k].cnt,tr[k].r);
}

Inquiry precursor:
1. -INF if not return;
2. the value of this point is less than recursive left subtree;
otherwise max (tr [k] .v, qpre (x, tr [k] .r));

 

Asked successor:
1. (lazy about here) and ask precursor is the opposite;

inline int qnxt(int x,int k)
{
    if (! k) return INF;
    if (x> = tr [k] .v) return qnxt (x, tr [k] .r);
    else  return min (tr [k] .v, qnxt (x, tr [k] .l));
}


The main role is to balance the tree:

  1.     Insert the number x;
  2.     Delete number x (if a plurality of the same number, because only one delete);
  3.     Ranking queries the number x (if a plurality of the same number, because the minimum output ranking);
  4.     Queries ranked number x;
  5.     X prior to the evaluation of chemotaxis (the predecessor is defined as less than x, and the maximum number);
  6.     Find successor x (defined as greater than the successor x, and the smallest number).

There is a common problem called balanced tree; children's shoes for just learning to balance the tree;
the complete code:

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<string>
  5 #include<cstdio>
  6 #include<cstdlib>
  7 #include<cmath>
  8 #include<ctime>
  9 using namespace std;
 10 const int inf=0x7fffffff;
 11 int n,rt;
 12 int tot=0;
 13 struct node{
 14     int v,cnt,l,r,rnd,size;
 15 }tr[1000006];
 16 inline void up(int k)
 17 {
 18     tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].cnt;
 19 }
 20 inline void zig(int &k)
 21 {
 22     int h=tr[k].l;
 23     tr[k].l=tr[h].r,tr[h].r=k;
 24     tr[h].size=tr[k].size;
 25     up(k);
 26     k=h;
 27     return ;
 28 }
 29 inline void zag(int &k)
 30 {
 31     int h=tr[k].r;
 32     tr[k].r=tr[h].l,tr[h].l=k;
 33     tr[h].size=tr[k].size;
 34     up(k);
 35     k=h;
 36     return ;
 37 }
 38 inline void insert(int x,int &k){
 39     if(!k){
 40         k=++tot;
 41         tr[k].v=x;
 42         tr[k].cnt=tr[k].size=1;
 43         tr[k].rnd=rand();
 44         return ;
 45     }
 46     tr[k].size++;
 47     if(x==tr[k].v) tr[k].cnt++;
 48     else if(x<tr[k].v)
 49     {
 50         insert(x,tr[k].l);
 51         if(tr[tr[k].l].rnd<tr[k].rnd) zig(k);
 52     }
 53     else if(x>tr[k].v)
 54     {
 55         insert(x,tr[k].r);
 56         if(tr[tr[k].r].rnd<tr[k].rnd) zag(k);
 57     }
 58     return ;
 59 }
 60 inline void del(int x,int &k)
 61 {
 62     if(!k) return ;
 63     if(tr[k].v==x)
 64     {
 65         if(tr[k].cnt>1)
 66         {
 67             tr[k].cnt--;
 68             tr[k].size--;
 69         }
 70         else if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r;
 71         else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd) zig(k),del(x,k);
 72         else zag(k),del(x,k);
 73         return ;
 74     }
 75     tr[k].size--;
 76     if(tr[k].v>x) del(x,tr[k].l);
 77     else del(x,tr[k].r);
 78     return ;
 79 }
 80 inline intqrnk ( int x, int k)
 81  {
 82      if (! k) return  0 ;
83      if (tr [k] .v == x) return tr [tr [k] .l] .size + 1 ;
84      else  if (tr [k] .v> x) return qrnk (x, tr [k] .l);
85      else  return qrnk (x, tr [k] .r) + tr [tr [k] .l] .size + tr [k] .cnt;
86  }
 87 inline int qnum ( int x, int k)
 88  {
 89      if (! K)return 0;
 90     if(x>tr[tr[k].l].size&&x<=tr[tr[k].l].size+tr[k].cnt) return tr[k].v;
 91     else if(x<=tr[tr[k].l].size) return qnum(x,tr[k].l);
 92     else return qnum(x-tr[tr[k].l].size-tr[k].cnt,tr[k].r);
 93 }
 94 inline int qpre(int x,int k)
 95 {
 96     if(!k) return -inf;
 97     if(x<=tr[k].v) return qpre(x,tr[k].l);
 98     else return max(tr[k].v,qpre(x,tr[k].r));
 99 }
100 inline int qnxt(int x,int k)
101 {
102     if(!k) return inf;
103     if(x>=tr[k].v) return qnxt(x,tr[k].r);
104     else return min(tr[k].v,qnxt(x,tr[k].l));
105 }
106 int main()
107 {
108     srand(time(0));
109     scanf("%d",&n);
110     for(int i=1;i<=n;i++)
111     {
112         int f,x;
113         scanf("%d%d",&f,&x);
114         if(f==1) insert(x,rt);
115         if(f==2) del(x,rt);
116         if(f==3) printf("%d\n",qrnk(x,rt));
117         if(f==4) printf("%d\n",qnum(x,rt));
118         if(f==5) printf("%d\n",qpre(x,rt));
119         if(f==6) printf("%d\n",qnxt(x,rt));
120     }
121     return 0;
122 }
View Code

Konjac understand, any errors in other chiefs of the main

Author: lsc


Guess you like

Origin www.cnblogs.com/kalginamiemeng/p/11010811.html