Soldier kills 1 (tree-like array)

Source: Internet
Author: User

First of all, let's talk about the tree-like array:

A tree-like array (Binary Indexed tree (BIT), Fenwick tree) is a data structure that queries and modifies the complexity of both log (n). It is primarily used to query the sum of all elements between any two bits, but only one element can be modified at a time, and a simple modification allows scope modification at the complexity of log (n), but only one element can be queried at this point.

Suppose the array A[1..N], then query A[1]+...+a[n] Time is the log level, and is an online data structure, support at any time to modify the value of an element, the complexity is also the log level. To see the diagram above:The node of this tree is numbered c1,c2 ... Cn. So that the value of each node is the sum of the values of this tree, it is easy to find:C1 = A1C2 = A1 + a2c3 = A3C4 = A1 + A2 + A3 + a4c5 = a5c6 = A5 + a6c7 = A7c8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ... C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16 Here's an interesting property: Set the node number to x, then this node has a range of 2 ^k (where k is the number of x binary end 0) elements. Because the last element of this interval is bound to be ax, it is obvious: Cn = A (n–2^k + 1) + ... + an calculate this 2^k there is a quick way to define a function as follows:
1 int lowbit (int  x)2{34return x& (-X  ); 5 6 }

When you want to query for a sum (n (for A[n] and),Can be based on the following algorithm:
Step1: Make sum = 0, turn the second step;
Step2: if n <= 0, the algorithm ends, returns the sum value, otherwise sum = sum + Cn, turn to the third step;
Step3: Make n = n–lowbit (n), turn the second step.
1 intSum (intN)2 {3     intsum=0;4      while(n>0)5     {6sum+=C[n];7n=n-lowbit (n);8     }    9     returnsum;Ten}
It can be seen that this algorithm is to add this interval and all together, why is the efficiency is log (n)? The following is a proof: n = n–lowbit (n) This step is actually equivalent to subtracting the last 1 of the binary of N. The binary of n has a maximum of log (n) 1, so the query efficiency is log (n). So modify, modify a node, must modify all its ancestors, the worst case is to modify the first element, up to log (n) ancestors. So the modification algorithm is as follows (to a node I plus x):
Step1: When I > N, the algorithm ends, otherwise it turns the second step;
Step2: ci = ci + x, i = i + lowbit (i) go to the first step. i = i +lowbit (i) This process is actually just a process of filling the end 1 with 0.
The code is as follows:
1 void Change (int i,int.  x)2{3while      (i <=N)4     {5           c[i]=c[i]+x; 6           i=i+lowbit (i); 7      }8 }

The tree array is too fast for the sum of the arrays! Reference: Baidu Encyclopedia good, so soldiers kill 1 of the topic code can be summed in a tree-like array: code source
1#include"stdio.h"2#include <string.h>3 inta[1000000];4 intMain ()5 {6         intn,sum;7scanf"%d%d",&n,&sum);8         inti,j,k;9Memset (A,0,sizeof(a));Ten          for(i=1; i<=n;i++) One         { A              intnum; -scanf"%d",&num); -j=i; the               while(j<=N) -              { -a[j]=a[j]+num; -j+=j& (-j); +              } -         } +          for(i=0; i<sum;i++) A         { atscanf"%d%d",&k,&j); -               ints1=0, s2=0; -k=k-1; -                while(k>=1) -               {           -s1=s1+A[k]; ink-=k& (-k); -                } to                 while(j>=1) +                { -s2=s2+A[j]; thej-=j& (-j); *                } $printf"%d", s2-s1);Panax NotoginsengPutchar ('\ n'); -          } the          return 0; +}

Soldier kills 1 (tree-like array)

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.