Tree-like array
——! x^n+y^n=z^n
Well, the picture is online search ...
We make
C[1]=A[1]
C[2]=A[2]+C[1]
C[3]=A[3]
C[4]=A[4]+C[3]+C[2]
C[5]=A[5]
C[6]=C[5]+A[6]
C[7]=A[7]
C[8]=A[8]+C[4]+C[6]+C[7]
C[9]=A[9]
In fact, we are the c[n] of the jurisdiction of the I element, and this I is n to binary when the first occurrence from the forward 1 and the next 0 of the size of the number of components (recorded as Lowbit (n)).
Give an example c[6]=a[5]+a[6]
6= (2,lowbit) (6) = (10) 2=2;
But this will produce a problem, how to beg lowbit?
Using the computer's complement feature we can get:
Lowbit (x) =x& (×). [Know the complement of the students hand push can, do not know hurriedly Baidu]
Of course, as a data structure to have his usefulness.
With a tree-like array we can do two simple operations:
<i> the value of a[1]+a[2]+l+a[x]
<ii> Single-point update
<i> Zone Search
Suppose we ask for 1~x interval and we can first query c[x], obviously c[x] is contained in the scope of our request;
However, when we have finished querying c[x], we should continue to query the non-queried parts;
Note: c[x] Jurisdiction: [X-lowbit (x) +1,x], so we should continue to query C[x-lowbit (x)];
Repeat the above operation until the query is complete.
Code:
1 int getsum (x) {2 int res=0; 3 for (; x;x-=lowbit (x)) 4 res+=bit[x]; 5 return Res; 6 }
My tree-like array has always liked to play a bit ah ...
<ii> Single-point update
We hope A[x]+inc
Of course we will let C[x]+inc first;
Then we need to find the c[y] that governs c[x], because x must ∈[y-lowbit (y) +1,y], how do we do this?
In fact, we just need to make a 0 on the back of X, just think about it.
Code:
1 void Update (x,inc) {2 for (; x<=n;x+=lowbit (x)) 3 Bit[x]+=Inc; 4 }
What, do you think the code is a lot shorter? (Compare line tree)
Tree-like array [data structure]