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类型: 除最下面一层外, 每一 ...
随机推荐
- win10+UEFI下u盘安装ubuntu16.04
本人电脑是华硕,由于要求使用linux所以安装: 1.首先给linux划出一个大分区,感觉最少50G: win10下磁盘管理,在最后的分区中压缩出50g,空间,其他的不用问了,也不用继续分区,一个大的 ...
- 【学习笔记】快速傅里叶变换(FFT)
[学习笔记]快速傅里叶变换 学习之前先看懂这个 浅谈范德蒙德(Vandermonde)方阵的逆矩阵的求法以及快速傅里叶变换(FFT)中IDFT的原理--gzy hhh开个玩笑. 讲一下\(FFT\) ...
- Hadoop集群部署-Hadoop 运行集群后Live Nodes显示0
可以尝试以下步骤解决: 1 ,分别删除:主节点从节点的 /usr/local/hadoop-2.6.2/etc/tmp 下得所有文件; 2: 编辑cd usr/local/hadoop-2.6. ...
- Linux下/usr/bin/python被删除的后果
可能部分的人使用linux都有直接root登陆的习惯,这有很大的便利性,因为很多的命令不需要使用sudo请求root权限.但是使用root权限,所有的命令都会立即被执行,即使这个命令是对系统有害处的. ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- Android Weekly Notes Issue #290
Android Weekly Issue #290 December 31st, 2017 Android Weekly Issue #290 本期内容包括介绍Kotlin逆变协变的一篇(虽然没说清楚 ...
- pip 安装模块报错解决
系统版本 ubuntu Kylin 16.04 LTS 报错1:安装pip3 安装 Django 总是提示time out,无法安装. 改用国内源: 豆瓣源: sudo pip3 install - ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- 数据库,序列化数据为json字符串
create PROCEDURE [dbo].[usp_SerializeJSON] @ParameterSQL as varchar(max) AS BEGIN declare @SQL nvarc ...
- BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...