BIT (Binary Indexed tree,bit) tree-like array. What kind of data structure is a tree-like array? We know that tree arrays are created to solve dynamic and continuous query problems.
Data structure that is, give you an array of n elements A[1],a[2],a[3]....a[n] (subscript to start from 1) then, the following two operations are supported:
1.ADD (x, y) means a[x] plus D for subscript x. Template defaults to update (x, y)
2.Query (l,r) is calculated A[l]+a[l+1]+...+a[r]. The template defaults to read (x) for 1-x and then read (y) to find the 1-y, and then use Read (y)-read (x-1) to get [X, Y] and
So, bit is a dynamic continuous and query problem.
Before learning bit, we must have a certain understanding and understanding of bit operations, we know that for negative numbers, in the computer is in its complement of the form of storage, and the complement is in its original code on the basis of the symbol bit can not change, the rest of you take the reverse after adding a result.
Look at such operations, such as the binary representation of 7:00000111, then-7 of the binary representation is: 1111001, then the result of 00000111&11110001 is 00000001, so
Such an operation helped us find the 7 binary representation, the last 1 position, with this concept. If we do the j-=j& (-j) operation, we know that this is the process of 111->110->100->000.
With all that said, how does a tree-like array actually come true? With tree[]. Tree[i] represents the and of the elements of an array of [I (i& (i)) +1,i] Within this interval (why this is, as explained below)
For example, we for (int i = 1;i <= n;i++) a[i]=i;
The value of the element that you want to ask for a[1]~a[15]. We know that the binary representation of 15 is (1111) 2
TREE[15] = sum[15,15];
TREE[14] = sum[13,14];
TREE[12] = sum[9,12];
TREE[8] = sum[1,8];
SUM[1,15] = tree[1,8]+tree[9,12]+tree[13,14]+tree[15,15];
Why this is the result, we find that for node I, if he is a left dial hand node, then the father's number is i+i& (-i).
If I is the right child node, then the father is the number of nodes is i-i& (-i);
It is not difficult to prove that the time complexity of both operations is O (NLOGN), when preprocessing, the array of a and C are emptied, and then perform n update () operation, the total time complexity is nlogn.
-read (int pos) to find out Sum[1,pos].
-update (int pos,int v) put A[pos] Plus V
It is important to note that the update point then queries the interval, and the subscript must start at 1.
If the value of Sum[l,r] is calculated, then we use Read (l)-read (r-1) to implement
Here are two ways to implement read.
The first, which is written with while
int int POS) { 0; while (pos>0 ) { ans+ =Tree[pos]; POS-=pos& (-POS); } return ans;}
The second type, using the for write
int int POS) { int0; for int j = pos;j;j-=j& (-j)) { ans+ =tree[j];} }
Again, that's the way the update (int pos,int val) is written.
The first, while notation
void int pos,int val) { while (pos <= N) { Tree[pos]+ = val ; POS+=pos& (-POS); }}
The second kind, for the wording
void int pos,int val) { forint j = pos;j <= n;j+=j& (- j) { Tree[j]+ =val; }}
According to their own habits to write it, in fact, can not.
Data Structure Learning route:
Tree-like, tree-and-line tree-------------------
Tree-like Array (BIT) beginner