当前位置:网站首页>IndexTree2D
IndexTree2D
2022-07-24 22:14:00 【Harrison who likes to knock code】
A two-dimensional IndexTree Problem solved

A one-dimensional IndexTree The problem to deal with is in a one-dimensional array , A two-dimensional IndexTree The problem to deal with is in a two-dimensional array .help [ i ] [ j ] Indicates that the original array is made in the upper left corner from the first row and the first column ,i,j Make the lower right corner , The accumulation and filling of this whole area is help [ i ] [ j ] In this grid .

without doubt , It is much more complicated than one-dimensional structure , If the value of a certain position of the original array changes , that help The positions affected by the array may be quite many .
for instance , The row number in the original array is 0110100, The column number is 0111000, The value of this position has changed , that help Which corresponding values in the array will change ?
It's easy to push from one dimension to two dimensions , stay help Array , Line number from 0110001 ~ 0110100、 Column number from 0110001 ~ 0111000, This whole combination area is all affected .

Again , 3D can also be pushed like this , This is IndexTree The advantages of , The line segment tree can also be changed into two-dimensional 、 The three dimensional , But it's troublesome

What if you want to find the cumulative sum of any region in a two-dimensional array ?(help Array support is from [ 1,1 ] To [ i , j ] The cumulative sum of positions )
such as , Want to ask for [ 3,3 ] To [ 4, 4 ] The cumulative sum of the whole , It can be calculated in blocks

The cumulative sum of any piece , Can be in help Take four values from the array and process them

Time complexity

LeetCode 308. Two dimensional region and retrieval - variable ( The prefix and )
Code
package com.harrison.class22;
/** * @author Harrison * @create 2022-04-02-9:58 * @motto looking for him for thousand times in the crowd , Suddenly look back , The man was in the dim light . */
// Test link :https://leetcode.com/problems/range-sum-query-2d-mutable
// But this question is a paid question
// Submit the class name 、 Constructor name from Code02_IndexTree2D Change to NumMatrix
public class Code02_IndexTree2D {
private int[][] tree;
private int[][] nums;
private int N;
private int M;
public Code02_IndexTree2D(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return;
}
N = matrix.length;
M = matrix[0].length;
tree = new int[N + 1][M + 1];
nums = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
update(i, j, matrix[i][j]);
}
}
}
private int sum(int row, int col) {
int sum = 0;
for (int i = row + 1; i > 0; i -= i & (-i)) {
for (int j = col + 1; j > 0; j -= j & (-j)) {
sum += tree[i][j];
}
}
return sum;
}
// [row,col] The value of the location is updated to val Equivalent to adding one add( Subtract the old value from the current value )
public void update(int row, int col, int val) {
if (N == 0 || M == 0) {
return;
}
// The added value == Subtract the old value from the current value
int add = val - nums[row][col];
nums[row][col] = val;
for (int i = row + 1; i <= N; i += i & (-i)) {
for (int j = col + 1; j <= M; j += j & (-j)) {
tree[i][j] += add;
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
if (N == 0 || M == 0) {
return 0;
}
return sum(row2, col2) + sum(row1 - 1, col1 - 1) - sum(row1 - 1, col2) - sum(row2, col1 - 1);
}
}

边栏推荐
- Morris遍历
- Pyqt5 uses qfile and qdatastream to read and write binary files
- ASP.NET Core 6.0 基于模型验证的数据验证
- SVM——针对线性可分(下)
- Web3安全 Go+Security
- Feeding Program Source Code to ZK VMs
- 《ArchSummit:珍爱微服务底层框架演进》
- Leetcode: the shortest dice sequence impossible to get [thinking questions + grouping ideas]
- 微机原理:CPU架构详解
- Mathematical derivation in [pumpkin Book ml] (task4) neural network
猜你喜欢

Ue5 reports an error using the plug-in quixel Bridge

RISC0:Towards a Unified Compilation Framework for Zero Knowledge

模板的使用
![[database learning] redis parser & single thread & Model](/img/70/c84eb02d45e35fede4dd1b1ff04392.png)
[database learning] redis parser & single thread & Model

通讯异常判断之通讯心跳信号应用编程

Visual studio input! No prompt

From A76 to A78 -- learning arm microarchitecture in change

Im instant messaging develops ten million level concurrent long connection Gateway Technology

PCL点云处理之平面规则化(五十五)

LED digital display driver IC and anti-interference LED digital tube display driver ic-vk1s68c ssop24 are applicable to finger clip pulse oximeter, arm electronic sphygmomanometer, thermometer, fetal
随机推荐
PCL点云处理之ply文件读取与保存(五十四)
Kubernetes v1.24 is deployed based on containerd
Helm —— 强大的 Kubernetes 应用的包管理工具
PCL point cloud processing ply file reading and saving (54)
PCL点云处理之移动最小二乘拟合实验(六十二)
一键编译安装redis6.2.4
Gradle学习集合整合
Easily make 2D tile map with unity tilemap - Basics
Pyqt5 uses qfile and qdatastream to read and write binary files
How to adjust the default output of vscode to the debugging console to the terminal and the problem of garbled code in both
Win10 解base64
Moving least squares fitting experiment of PCL point cloud processing (62)
VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
从A76到A78——在变化中学习ARM微架构
Local data enhancement method of icml2022 | graph neural network
【南瓜书ML】(task4)神经网络中的数学推导
PCL点云处理之找到直线点集的两个端点(五十七)
CAD text styles
Gradle learning - integration of gradle and idea
Projection regularization of line point set in PCL point cloud processing (56)