tyvj1728 Normal Balance Tree

Source: Internet
Author: User

To thoroughly understand the tree array, try to use a tree-like array to make a normal balance tree

A tree array can only be done offline, or it is guaranteed that the size of the value is acceptable within the range of the array, because it is required to be offline because all the numbers must be discretized beforehand.

And then we look at the picture on the Rujia Blue Book.

With the following code, you can find all the prefixes and the first one greater than or equal to K

1 intKthintk) {2     intans=0;3      for(intI= -; i>=0; --i) {4Ans + =1<<i;5         if(Ans>=sz | | C[ANS]&GT;=K) ans-=1<<i;6         Elsek-=C[ans];7     }8     returnseq[ans+1];9}

What principle, we understand that, the tree array as a tree, in fact, his node number of the sequence traversal is orderly, with the balance tree is similar to the wonderful, assuming now in (1000) 2 this point, if left, it is equivalent to not take [1,8], and then in [1,7], if go to the right, It is equivalent to taking [1,8], and then in [9,15], as to whether to go to the left or to the right to the balance tree to find the big K is the same. It can then be found that the balance tree is equivalent to [1,n], while the binary index tree is in [1,2^k] (k is the smallest positive integer of 2^k>=n).

Give the full code of the normal balance tree

1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <cstdio>5#include <algorithm>6 7 using namespacestd;8 9 Const intmaxn=100010;Ten  One intC[maxn],sz; A  - intSumintx) { -     intret=0; the      for(;0<x && x<=sz; X-=x&-x) ret+=C[x]; -     returnret; - } -  + voidAddintXintd) { -      for(;0<x&&x<=sz; x+= x&-x) c[x]+=D; + } A  at intSeq[maxn],tot; - voidHashinit () { -Sort (seq+1, seq+tot+1); -SZ = Unique (seq+1, seq+tot+1)-(seq+1); - } - intHashintx) { in     returnLower_bound (seq+1, seq+sz+1, X)-seq; - } to  + intOPT[MAXN],NUM[MAXN]; -  the intKthintk) { *     intans=0; $      for(intI= -; i>=0; --i) {Panax NotoginsengAns + =1<<i; -         if(Ans>=sz | | C[ANS]&GT;=K) ans-=1<<i; the         Elsek-=C[ans]; +     } A     returnseq[ans+1]; the }             +  - intMain () { $ #ifdef DEBUG $Freopen ("In.txt","R", stdin); - //freopen ("OUT.txt", "w", stdout); - #endif the      -     intN;Wuyiscanf"%d",&n); the      for(intI=1; i<=n;i++) { -scanf"%d%d", opt+i,num+i); Wu         if(opt[i]!=4) Seq[++tot] =Num[i]; -     } About      $ hashinit (); -      -      for(intI=1; i<=n;i++) { -         if(opt[i]==1) Add (hash (num[i)),1); A         if(opt[i]==2) Add (hash (num[i)),-1); +         if(opt[i]==3) printf ("%d\n", SUM (hash (num[i))-1)+1); the         if(opt[i]==4) printf ("%d\n", KTH (Num[i])); -         if(opt[i]==5) printf ("%d\n", KTH (SUM (hash (num[i))-1))); $         if(opt[i]==6) printf ("%d\n", KTH (SUM (hash (num[i)) +1)); the     }     the      the     return 0; the}

tyvj1728 Normal Balance Tree

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.