Suitable for interval sum or prefix sum.
Interval sum is just difference of two prefix sums.
Compared with Segment Tree, Binary Indexed Tree requires less space and is easier to implement.
BIT needs exactly n extra spaces, but Segment Tree needs approximately 2n extra spaces.
https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
2D BIT
https://evanyang.gitbooks.io/leetcode/content/LeetCode/range_sum_query_2d_-_mutable.html
For BIT, the indexes should start at position 1, so that you can use i&(-i) to move.
When you want to get the prefix sum at position i, you should deduct i&(-i) iteratively, and it will move through all the same level nodes horizontally for you to sum.
When you want to update a value at position i, you should add i&(-i) iteratively, and it will move upwards throught all the parent nodes vertically towards the root.
本文介绍Binary Indexed Tree(BIT,也称Fenwick Tree)的基本原理及其在区间求和与前缀求和问题中的应用。相较于Segment Tree,BIT具有更小的空间需求且实现更为简单。文章解释了BIT的工作机制,包括如何通过迭代操作获取指定位置的前缀和以及如何更新特定位置的值。

5万+

被折叠的 条评论
为什么被折叠?



