Fenwick Tree / Binary Indexed Tree
Motivation:
Given a 1D array of n elements. [2, 5, -1, 3, 6]
range sum query: what's the sum from 2nd element to 4th element query(2, 4)? 5 + (-1) + 3 = 7
Native implementation: O(n) per query.
Use DP to pre-compute the prefix sums in O(n), [2, 5, -1, 3, 6] -> [2, 7, 6, 9, 15]
reduce query to O(1). query(2, 4) = sums(n1....n4) - sums(n1....n1) = sums[4-1] - sums[1-1] = 9 - 2 = 7
what if the value of elements can change? O(n)
Fenwick tree was proposed to solve the prefix sum problem.
The idea is to store partial sum in each node and get total sum by traversing the tree from leaf to root. the tree has a height of log(n)
Query: O(log(n))
Update: O(log(n))
class FenwickTree {
public:
FenwickTree(int n): sums_(n+1, 0) {}
void update(int i, int delta) {
while (i < sums_.size()) {
sums_[i] += delta;
i += lowbit(i);
}
}
int query(int i) const {
int sum = 0;
while (i > 0) {
sum += sums_[i];
i -= lowbit(i);
}
return sum;
}
private:
static inline int lowbit(int x) { return x & (-x); }
vector<int> sums_;
};
Fenwick Tree / Binary Indexed Tree的更多相关文章
- Binary Indexed Tree (Fenwick Tree)
Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...
- Binary Indexed Tree
我借鉴了这个视频中的讲解的填坑法,我认为非常易于理解.有FQ能力和基本英语听力能力请直接去看视频,并不需要继续阅读. naive 算法 考虑一个这样的场景: 给定一个int数组, 我们想知道它的连续子 ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- SRM 627 D1L2GraphInversionsDFS查找指定长度的所有路径 Binary indexed tree (BIT)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13275&rd=16008 由于图中边数不多,选择DFS遍历 ...
- 树状数组(Binary Indexed Tree,BIT)
树状数组(Binary Indexed Tree) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...
- Hdu5921 Binary Indexed Tree
Hdu5921 Binary Indexed Tree 思路 计数问题,题目重点在于二进制下1的次数的统计,很多题解用了数位DP来辅助计算,定义g(i)表示i的二进制中1的个数, $ans = \su ...
- Binary Indexed Tree 总结
特点 1. 针对 数组连续子序列累加和 问题(需要进行频繁的 update.sum 操作): 2. 并非是树型结构,只是逻辑上层次分明: 3. 可以通过 填坑法 来理解: 4. 中心思想:每一个整数都 ...
- 树状数组(Binary Indexed Tree)
树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...
- CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...
随机推荐
- VC调用Delphi DLL
别的没什么,是一定可以调用成功的.但是意外的是,ShowMessage函数在DLL里也可以轻易被调用.此外,Delphi里的var 相当于VC里的引用,需要在函数原型里正确标识,否则传递普通变量甚至常 ...
- AsyncHttpClien访问网络案例分析
Android数据存储的四种方式分别是:SharedPreferences存储.File文件存储.Network网络存储和sqlite数据库存储,网络存储需要使用AsyncHttpClient发送请求 ...
- OpenGLES 与 WebGL 中顶点属性的组织格式的误解 - 一个不好笑的笑话
版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589.加好友请注明来意. https://blog.csdn.net/sleks/article/details/289 ...
- 友盟分享到微信的几点备忘(IOS)
1.下载最新的友盟分享版本,参考友盟官方的demo 2.注册微信开放平台用户,不是公众平台,注册应用 3.参考文档和demo,加入sdk包和相应的lib 4.在plist加入URL types.URL ...
- [2018-10-17]宁波dotnet社区(NBDNC)第一次问卷关于dotnet技术栈的小调查
最近(2018年10月7日至10月17日),为配合确定下一次社区线下活动主题,做了一次宁波dotnet社区(NBDNC)的本地dotnet技术栈调研,设计了一份问卷,在此做一次记录. 导出的问卷统计结 ...
- TopCoder<SRM>上的一道1100分的题目解析附代码
首先我们来简单看一下这道题的statement Problem Statement Note that in the following problem statement, all quo ...
- smokeping 出现的问题
Global symbol "%Config" requires explicit package name at /usr/lib64/perl5/lib.pm line 10. ...
- SpringBoot_01_正确、安全地停止SpringBoot应用服务
二.参考资料 1.正确.安全地停止SpringBoot应用服务
- listen 76
Flavors Fluctuate With Temperature Does an ice-cold drink actually taste better than the same bevera ...
- 【Shell】基础正则表示法及grep用法
——<鸟哥的私房菜> 正规表示法就是处理字串的方法,他是以行为单位来进行字串的处理行为:正规表示法透过一些特殊符号的辅助,可以让使用者轻易的达到『搜寻/删除/取代』某特定字串的处理程序:只 ...