LeetCode10 Indexed tree
Binary Indexed Tree(Fenwick tree):
是一个查询和修改复杂度都为log(n)的数据结构。主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值;经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询)
原理:
树状数组是不需要建树的,通过lowbit来建立一个sum数组(此处为c数组)来维护原来的数组
Lowbit讲解:
c[i]=sum(a[j]) i - lowbit(i) + 1 <= j <= i;
这里的 lowbit(i) 表示将i转换成二进制后的最右边的1到最后所形成的十进制的数。
例如 lowbit(6)=lowbit(110)2=(10)2=2
所以 c[6]=a[5]+a[6];
lowbit(4)=lowbit(100)2=(100)2=4
c[4]=a[1]+a[2]+a[3]+a[4]
lowbit[5]=lowbit(101) 2 =(1) 2=1;
c[5]=a[5]
Lowbit公式:
- lowbit(i)=i&(-i);
公式原理:
上面这个公式是非常重要的,就是通过求出一个数的 Lowbit 将这些数据建立成一个树的,父子节点和线段树关系类似,都是通过某种固定的方式建立起来的。而 lowbit 的原理就是利用了计算机里面的反码和补码(这世界本来就是二进制的!)
我们都知道在计算机中数使用二进制存储的,int 是 4 字节,例如我们把5换成二进制。 5=00000000 00000000 00000000 00000101 这同时也是5的原码。 原码:一个正数,按照值大小转换成的二进制数;一个负数按照绝对值大小转换成的二进制数,然后最高位补1,称为原码。
那么-5的原码就是 -5=10000000 00000000 00000000 00000101 ,其中,最高位的1是符号位。 在计算机中储存一个数用的是补码的形式,要了解补码需要先知道什么是反码。 反码:正数的反码与原码相同,负数的反码为对该数的原码除符号位外各位取反 -5的反码就是: 11111111 11111111 11111111 11111010
再说补码:正数的补码与原码相同,负数的补码是这个数的反码+1 -5:11111111 11111111 11111111 11111010+1= 11111111 11111111 11111111 11111011
那么我们利用这个公式 x&(-x) 的意义:
6&(-6):
6: 00000000 00000000 00000000 00000110
-6: 11111111 11111111 11111111 11111010
Ans: 00000000 00000000 00000000 00000010
所以说 lowbit(6) 的结果很明显就是2
节点i的父亲节点为i+lowbit(i) 若需改变a[i],则c[i]、c[i+lowbit(i)]、 c[i+lowbit(i)+lowbit(i+lowbit(i)]……一直加到上限,就是需要改变的c数组中的元素。
若需查询s[i],则 c[i] 、 c[i-lowbit(i)] 、c[i-lowbit(i)-lowbit(i-lowbit(i))] ……就是需要累加的c数组中的元素。 这样我们就让复杂度优化到了 O(logn)
public void change(int index, int val){
while (index <= maxValue){
sum[index] += val;
index += index & (- index);
}
}
public int sum(int index){
int sum = 0;
while( index >= 0){
sum += sum[index];
index -= index & (-index);
}
}
树状数组原理图:

herf: http://xorex.top/2017/05/20/二叉索引树-树状数组-——讲解/
2D Binary Indexed Tree (Fenwick Tree)
Algorithm:
We consider the below example. Suppose we have to find the sum of all numbers inside the highlighted area-
We assume the origin of the matrix at the bottom – O.Then a 2D BIT exploits the fact that-
Sum under the marked area = Sum(OB) - Sum(OD) - Sum(OA) + Sum(OC)


In our program, we use the getSum(x, y) function which finds the sum of the matrix from (0, 0) to (x, y).
Hence the below formula :Sum under the marked area = Sum(OB) - Sum(OD) - Sum(OA) + Sum(OC)
The above formula gets reduced to,
Query(x1,y1,x2,y2) = getSum(x2, y2) - getSum(x2, y1-1) - getSum(x1-1, y2) + getSum(x1-1, y1-1) ;
308. Range Sum Query 2D - Mutable
class NumMatrix {
int [][] tree;
int [][] matrix;
int m;
int n;
public NumMatrix(int[][] matrix) {
if(matrix.length == 0 || matrix[0].length == 0) return;// 先判断,再赋值。因为有可能,matrix.length = 0, 那么matrix[0]就会out index boundary。所以上述顺序也不能改变。
m = matrix.length;
n = matrix[0].length;
tree = new int[m + 1][n + 1];//对应的[m][n]的信息其实是存在[m+1][n+1]中
this.matrix = new int[m][n];
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
update(i, j, matrix[i][j]);
}
}
}
public void update(int row, int col, int val) {
if(m == 0 || n == 0) return;
int update = val - matrix[row][col];
matrix[row][col] = val;//必须要新立一个matrix,因为input只是一个variable,需要一个成员变量来保存
for(int i = row + 1; i <= m; i += i & (-i)){
for(int j = col + 1; j <= n; j += j & (-j)){
tree[i][j] += update;
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
if(m == 0 || n == 0) return 0;
return sum(row2 + 1, col2 + 1) + sum(row1, col1) - sum(row1, col2 + 1) - sum(row2 + 1, col1);
}
public int sum(int row, int col){
int sum = 0;
for(int i = row; i > 0; i -= i & (-i)){
for(int j = col; j > 0; j -= j & (-j)){
sum += tree[i][j];
}
}
return sum;
}
}
- Time Complexity :
- Both update and sumRegion function takes O(log(MN)) time.
- Building the 2D tree takes O(NM log(NM))
- Answering Q queries takes O(Qlog(NM))
- M = matrix.length N = matrix[0].length
LeetCode10 Indexed tree的更多相关文章
- 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) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...
- Binary Indexed Tree (Fenwick Tree)
Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...
- 【计数】hdu5921Binary Indexed Tree
二进制拆位计算贡献 题目描述 树状数组是一种常用的数据结构,下面是树状数组用于给区间 [1,x] 内的数加 t 的代码: void add(int x,int t){ for (int i=x;i;i ...
- 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
我借鉴了这个视频中的讲解的填坑法,我认为非常易于理解.有FQ能力和基本英语听力能力请直接去看视频,并不需要继续阅读. naive 算法 考虑一个这样的场景: 给定一个int数组, 我们想知道它的连续子 ...
- 树状数组(Binary Indexed Tree)
树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...
随机推荐
- 前端之CSS基础
前端之CSS 1. CSS CSS定义如何显示HTML元素. 当浏览器读到一个样式表,他就会按照这个样式表来对文档进行格式化(渲染). 3.CSS语法 1)CSS实例 每个CSS由两部分组成: 选择器 ...
- Docker环境下的前后端分离项目部署与运维
本教程将从零开始部署一个前后端分离的开源项目,利用docker虚拟机的容器技术,采用分布式集群部署,将项目转换成为高性能.高负载.高可用的部署方案.包括了MySQL集群.Redis集群.负载均衡.双机 ...
- 测试员小白必经之路----常见的linux操作命令
linux作为服务器操作系统 linux具有自己的优势,安全.稳定.免费.占有率高 操作系统发展历史 unix>minix>linux linux命令的基本使用 查阅命令帮助信息 comm ...
- 学Python的第六天
今天发愤图强,死学Python!!!! 为此小编还买了自己喜欢的McPython! 从小老师就说兴趣是最好的老师,所以呢买这本书是想通过我的世界这款游戏来提高自己的学习兴趣!!! 当然也有私心啦... ...
- 基础入门Bootstrap
一.CSS样式 1.图片 2.布局.排版(之全局显示) 3.容器-网格-栅格系统 搭建的格式如下 <!DOCTYPE html> <html> <head> < ...
- IPC之套接字
IPC(Inter-Process Communication,进程间通信)实现方式 1)管道: - 管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程之间使用(进程的亲缘关系 ...
- hdu 4082 Hou Yi's secret(暴力枚举)
Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 1269 Intersecting Lines(直线相交)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 391 ...
- Redis 复制功能详解
Redis 复制功能的几个重要方面: 1. 一个Master可以有多个Slave:2. Redis使用异步复制.从2.8版本开始,Slave会周期性(每秒一次)发起一个Ack确认复制流(replica ...
- [python 学习] requests 库的使用
1.get请求 # -*- coding: utf-8 -*- import requests URL_IP = "http://b.com/index.php" pyload = ...