Binary indexed tree

本文介绍Binary Indexed Tree(BIT,也称Fenwick Tree)的基本原理及其在区间求和与前缀求和问题中的应用。相较于Segment Tree,BIT具有更小的空间需求且实现更为简单。文章解释了BIT的工作机制,包括如何通过迭代操作获取指定位置的前缀和以及如何更新特定位置的值。

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值