As a mengbier in the oier of a weak province, simply tell me how I learned the basics of the tree-like array
Not a gorgeous split line.
- tree-like array (Binary Indexed Tree (B.I.T), Fenwick tree) is a data structure that queries and modifies the complexity of both log (n).
- The inventor, named Fenwick Tree, was first published by Peter M. Fenwick in 1994 with the title "a New Data Structure for cumulative Frequency Tables" in software practice A ND EXPERIENCE ".
- It is used primarily to query the sum of all elements in any piece of data.
- A simple modification allows scope modification in the complexity of log (n).
This means that you can modify the value of one of the ax values (or a segment) in a sequence of {an} by a series of magical operations, and find the first n subparagraphs (or any paragraph) of {an}.
The forehead sister!!! Right?
We know that the attribute of the noun "tree array" is "tree", it is only used to modify the array, right?
So the "tree array" is a string of supernatural arrays!
As an example:
Normal an array {an}:a[1]=1, a[2]=2, a[3]=3, a[4]=4, a[5]=5, a[6]=6, a[7]=7, A[8]=8, a[9]=9
You want to get the nth number, just use a[n] to express it (nonsense)
But if you want to count the first X and {SN}, you can only add from a[1] one item to A[X], which is obviously time consuming when you need to calculate {SN} a lot!!!
So the wit you think of first calculate each sn, but when {an} need to modify what to do?
Do you want to modify each of the relevant SN?
So the big boys yy out the tree-like array
Magical Array {cn}: C[1]=1, c[2]=3, c[3]=3, c[4]=10, c[5]=5, c[6]=11, c[7]=7, c[8]=36, c[9]=9
I said {cn} can express all the information of {an}, do you believe it?
The shadowy dividing line
If the node number is x, then this node is governed by an interval of 2^k (where K is the number of x binary end 0) elements. The last element of this interval is ax, so it is obvious: Cn = A (n–2^k + 1) + ... + an in other words for example: c[1], 1 of the binary number is 1,k=0, it is the length of the 2^0 interval and , Jinan c[1]=a[1] c[2], 2 of the binary number is 10,k=1, it is the tube length of the interval of 2^1, and C[2]=a[1]+a[2] c[4], 4 of the binary number is 100,k=2, it is the tube length of the 2^2 interval And, C[4]=a[1]+a[2]+a[3]+a[4] c[6], 6 of the binary number is 110,k=1, it is the tube length of the interval of 2^1, and C[6]=a[5]+a[6] c[9], 9 of the binary number is 10 01,k=0, it is the 2^0 of the interval of the tube length and the c[9]=a[9]
readers are strongly advised to use paper and pen to simulate again!!! There is a function that can simply calculate the 2^k
1 int lowbit (int x)2{3 return x&-x; 4 }
If you don't believe, use written calculation.
What do I need to do with the top n now?
Start with C[n], and don't repeat the data!
For example:
I ask for the first 6 items and, that is, from c[6] began to add, found that c[6] has represented 2 items, add the c[6], add c[6-2], add c[4] value, found C[4] represents 4 items, at this time add the top 6
Just look at the code.
1 intSumintN)2 {3 intans=0;4 while(n>0)5 {6ans+=C[n];7n-=lowbit (n); Add up the number that the item can represent, plus the number that has not been expressed8 }9 Ten returnans; One}
Change the value of one item to change each related item in {CN}
1 void Add ( int x,int v) // x position plus v 2 { 3 while (x<=n) // n is the boundary, how many number 4 { Span style= "COLOR: #008080" >5 c[x]+=V; 6 x+=lowbit (x); // 7 } 8 }
That from A[x]+a[x+1]+a[x+2]+......+a[y-1]+a[y], it can be expressed as sum (y)-sum (x-1);
is the arbitrary sequential sequence and
The shadowy dividing line
under the text of the foundation or not, may not speak very well, please forgive me
Provide some questions about the tree-like array:
Beginner Exercise 1
Beginner Exercise 2 (requires abstraction of the model)
Tree-like array small white (1)