Fenwick Tree/Binary Indexed Tree/树状数组
观看花花酱 Fenwick Tree / Binary Indexed Tree的视频,结合Visualizer即可理解Fenwick Tree。
Fenwick Tree is mainly designed for solving the single point update range sum problems. e.g. what’s the sum between i-th and j-th element while the values of the elements are mutable.
Init the tree (include building all prefix sums) takes O(nlogn)
Update the value of an element takes O(logn)
Query the range sum takes O(logn)
Space complexity: O(n)
Using Binary Indexed Tree, we can do both tasks in O(Logn) time. The advantages of Binary Indexed Tree over Segment are, requires less space and very easy to implement..
模板:
1 | class FenwickTree { |
参考资料: