Fenwick tree (2) - in order to prepare the future with

First write code:

The first function

int LowBit(int x) //求 最小幂2^k
{ return x & (-x); }

第二个函数

int getSum(int pos)   //求前n项和
{   
    int sum = 0;   
    while(pos > 0)   
    {   
        sum += arr[pos];   
        pos -= LowBit(pos);   
    }   
    return sum;   
}   

The third function

void update(int pos , int val)   //对某个元素进行加法操作
{   
    while(pos <= n)   
    {   
          arr[pos] += val;   
          pos += LowBit(pos);   
    }   
}   

http://blog.csdn.net/qq_34374664/article/details/52787481 explain Fenwick tree collection




Problem solving process, we sometimes need to maintain a prefix array and S [n-] = A [. 1] + A [2] + ... + A [n-] .
          Difficult to find, however, if we modify any of the A [i], S [i ], S [i + 1] ... S [n] will change.
          It can be said, each modified A [i], and to adjust the prefix S [] in the worst case will require O (n) time.
          When n is very large, the program will run very slowly.
          So, here we introduce the "Fenwick tree", it changes the sum is O (logn) , the efficiency is very high.


As shown, an array of red rectangle represents the C [] is an array we maintain tree .

Characteristics of the image: to form a C [8] is the root node, other C [i], A [j] is the sub-tree nodes.

For array A [] index is odd, which are just only a C [] elements, i.e., C [1] = A [1], C [3] = A [3], C [5] = A [ 5] etc.

For array A [] index is an even number, which is a series of A [i] and.

C [1] = A [1]; jurisdiction C [1] is 1

C [2] = A [2] + A [1]; jurisdiction C [2] is 2

C [3] = A [3]; jurisdiction C [3] is 1

C [4] = A [4] + A [3] + A [2] + A [1]; jurisdiction C [4] is 4

C [5] = A [5]; jurisdiction C [5] is 1

C [6] = A [6] + A [5]; jurisdiction C [6] is 2

C [7] = A [7]; jurisdiction C [7] is 1

C[8] = A[8] + A[7] + A[6] + A[5] + A[4] + A[3] + A[2] + A[1] ;C[8]的管辖范围为8

这里的管辖范围就是说C[]由几个A[]组成。

由上可得(S[]由C[]来表示):

S[1] = C[1]

S[2] = C[2]

S[3] = C[3] + C[2]

S[4] = C[4]

S[5] = C[5] + C[4]

S[6] = C[6] + C[4]

S[7] = C[7] + C[6] + C[4]

S[8] = C[8]

通过观察图像:S[i]表示为i之前所有最大子树的根结点C[]累加的和

上面所说的管辖范围就是下面列出来的2^k所表示的数

i                    i所对应的二进制                 
2^k(k-->i的二进制的末尾0的个数)     
1 01 1
2 10 2
3 11 1
4 100 4
5 101 1
6 110 2
7 111 1
8 1000 8

2^k的求法:i 的二进制去掉所有高位的1,只保留最低位的1,如果只存在一个最高位的1,则 2^k 就是它本身。

如1(1),2(10),4(100),8(1000),16(10000)。即都是2的幂。

所以当我们判断一个数x是不是2的次幂时,只要判断x是否等于2^k就可以了,而2^k由位运算很容易求得(x & (-x))。


对于C[i]如何求得他的父节点?

由上表格推断,C[i]的父节点为C[i+2^k],也就是说将 i 的二进制的最末尾的1去掉,相邻的高位+1

对于C[i]如何求得他的下一个节点?

由上表格推断,C[i]的下一个节点是C[i-2^k],也就是说将i的二进制最末尾的1去掉。


由上:

C[i]表示A[i-2^k+1]到A[i]的和,而k则是i在二进制时末尾0的个数。

利用位运算,我们可以直接计算出2 ^ k = i & (i ^ (i - 1)) = (i & (-i)),所以C[i]的父节点为C[i + i & (-i)]。

这个k(i的二进制表示末尾0的个数)就是该节点在树中的高度,因而这个树的高度不会超过logn。
所以,当我们修改A[i]的值时,可以从C[i]往根节点一路上溯,调整这条路上的所有C[]即可,这个操作的复杂度在最坏情况下就是树的高度即O(logn)。  

而对于求数列的前n项和S[n],只需找到C[n]以前(包括C[n])的所有最大子树,把其根节点的C[]加起来即可。

不难发现,这些子树的数目是n在二进制表示时1的个数。因此,求和操作的复杂度也是O(logn)。








http://blog.csdn.net/DGY01/article/details/52298912

http://blog.csdn.net/x_iya/article/details/8943264

http://blog.csdn.net/int64ago/article/details/7429868

发布了37 篇原创文章 · 获赞 12 · 访问量 6534

Guess you like

Origin blog.csdn.net/weixin_38960774/article/details/79405167