A tree-like array is a data structure that can do the following
Give a sequence with an initial value of all 0 a1,a2,... an.
* Given I, calculate A1+a2+...+ai
* Given I and X, execute AI + = x
1. The implementation based on segment tree
If you use a segment tree, you only need to make a few changes to the RMQ sample to achieve these two functions. The corresponding intervals are maintained on each node of the segment tree.
Next, let's look at how to calculate the and from S to T. In the implementation of line-based tree, this and can be directly obtained.
But if we can calculate (from 1 to T's and)-(from 1 to s-1), we can also get s to T's and. That is, as long as any I, we can calculate 1 to I of the part and is sufficient.
Under such constraints, what changes will be brought about? We can find that the value of the right son of each node in the segment tree is not required (if you want to use the value of this point at the time of calculation, then the value of the sibling on the left side will definitely be used, this time only need to use their father's value).
The data structure based on the above idea is bit. Bit is easier to implement and faster than line tree.
Structure of the 2.BIT
Bit uses the array to maintain the part and
That is, after removing unwanted nodes from the segment tree, the remaining nodes are then mapped to the array. Let's compare the length of the interval corresponding to each node and the binary representation of the node number. The length of the 1,3,5,7 ending with 1 is 1, and finally there is a 0 of the 2,6 length is 2, the last 2 0 of the length is 4 .... In this way, the binary representation of the number can be easily corresponded to the interval. Using this property, bit can be implemented by a very simple bitwise operation.
Sum of 3.BIT
The sum of the preceding I is calculated from the beginning of I, and the value of the current position I is continuously added to the result, and the binary lowest bit of I is subtracted from I for the 0-bit corresponding power until I becomes 0. The last 1 of the binary of I can be obtained by i&-i.
Update of the value of 4.BIT
To increase the value of item I, X needs to start from I, constantly increase the value of the current position I x, and I of the binary minimum non-0-bit corresponding to the power to I.
Complexity of the 5.BIT
A total of O (Logn) values need to be manipulated, and all complexity is O (logn).
Implementation of the 6.BIT
Incidentally, I-=i&-i can also write i=i& (i-1).
[1, N]int BIT[MAXN + 1], n;int sum (int i) { int s = 0; while (i > 0) { s + = bit[i]; I-= i &-i; } return s;} void Add (int i, int x) { while (i <= N) { Bit[i] + = x; i + = i &-i; }}
7. Two-D bit
Bit can be easily extended to a two-dimensional situation. For the two-dimensional bit of w*h, you only need to create a bit of h for the number of x-axis elements, and then manage the bits through the bit in the y-axis direction. That is, each element of bit in the y-axis direction is not an integer, but a bit in the x-axis direction. The complexity of all operations is O (LOGW * logh). The same approach can be used to extend the situation to a higher dimension.
Binary Indexed trees (tree-like array)