当前位置:网站首页>Tree array + discretization
Tree array + discretization
2022-07-16 07:28:00 【Get an offer as soon as possible】
Tree array + discretization
Advantages of tree array
- Quickly modify a number O ( l o g n ) O(logn) O(logn)
- Quickly find the prefix sum O ( l o g n ) O(logn) O(logn)
Compare the original array :
- Modify a number O ( 1 ) O(1) O(1)
- Find the prefix and O ( n ) O(n) O(n)
Compare prefix and array
- Modify a number O ( n ) O(n) O(n)
- Find the prefix and O ( 1 ) O(1) O(1)
Implementation of tree array
Principle see blog : The main idea is to use binary , Divide the original array into blocks , Then modify and sum it
There are two main operations :
- Modify the operating : In a certain position +c, In the current block and subsequent blocks +c
public void add(int x, int c) {
for(int i = x; i <= n; i += lowbit(i)) {
tr[i] += c;
}
}
- Summation operation : take a [ 1 ∼ x ] a[1\sim x] a[1∼x] Sum of the elements of
public int sum(int x) {
int res = 0;
for(int i = x; i >= 1; i -= lowbit(i)) {
res += tr[i];
}
}
( among ,lowbit(x) Is to seek x The last bit in the binary representation of 1, namely x&(-x))
- But the topic often appears n A lot of things , for example n > 1 0 6 n>10^6 n>106, Or there are negative numbers , Then the tree array will be very large or the subscript will be out of bounds , Cause memory overflow . So at this time, we need to discretize the original array . Discretization has two steps , They are sort + duplicate removal , This can be achieved with the help of ordered sets . But after using ordered sets , The corresponding number subscript cannot be determined quickly , At this time, we need to use hash table , Map numbers in an ordered set to [1,n] On , At this point, the requirements of tree array are met .( Discrete implementation : Ordered set + Hashtable )
Example
LeetCode327. The number of interval sums
Give you an array of integers n u m s nums nums And two integers l o w e r lower lower and u p p e r upper upper. In the array , The value is in the range [ l o w e r , u p p e r ] [lower, upper] [lower,upper] ( contain l o w e r lower lower and u p p e r upper upper) The number of intervals and within .
Interval and S ( i , j ) S(i, j) S(i,j) It means that n u m s nums nums in , Location slave i i i To j j j The sum of the elements of , contain i i i and j j j ( i ≤ j i ≤ j i≤j).
Tips :
- 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
- − 2 31 < = n u m s [ i ] < = 2 31 − 1 -2^{31} <= nums[i] <= 2^{31} - 1 −231<=nums[i]<=231−1
- − 1 0 5 < = l o w e r < = u p p e r < = 1 0 5 -10^5 <= lower <= upper <= 10^5 −105<=lower<=upper<=105
- The question data guarantees that the answer is 32 position The integer of
Ideas :
Use prefix and find s u m [ i ] sum[i] sum[i], So for i i i, Can traverse violently j , ( 1 ≤ j ≤ i − 1 ) j,(1\leq j\le i-1) j,(1≤j≤i−1) To calculate s u m [ i ] − s u m [ j ] sum[i]-sum[j] sum[i]−sum[j] Get the satisfaction interval and [ l o w e r , u p p e r ] [lower,upper] [lower,upper] The number of . At this time, the whole time complexity O ( n 2 ) O(n^2) O(n2), It's bound to time out . At this point, I think I can use tree array to solve .
l o w e r ≤ s u m [ i ] − s u m [ j ] ≤ u p p e r ⇒ s u m [ i ] − u p p e r ≤ s u m [ j ] ≤ s u m [ i ] − l o w e r lower\leq sum[i]-sum[j]\leq upper\Rightarrow sum[i]-upper\leq sum[j]\leq sum[i]-lower lower≤sum[i]−sum[j]≤upper⇒sum[i]−upper≤sum[j]≤sum[i]−lower.
That is, calculate at [ s u m [ i ] − u p p e r , s u m [ i ] − l o w e r ] [sum[i]-upper,sum[i]-lower] [sum[i]−upper,sum[i]−lower] The number of . Using a tree array can quickly O ( l o g n ) O(logn) O(logn) I'll get . however s u m [ i ] sum[i] sum[i] There's a wide range of data , Corresponding tree array t r [ n + 1 ] tr[n+1] tr[n+1] Also great , And will cross the border . So we need to discretize . Discretization relies on ordered sets + Hash table . Discretization requires All the numbers used Are stored in an ordered set , Then map these numbers to [ 1 , m a x ] [1,max] [1,max]. The subsequent operations only need to go to the right [ 1 , m a x ] [1,max] [1,max] Just operate on .
class Solution {
int[] tr;
int max;
public int countRangeSum(int[] nums, int lower, int upper) {
int n = nums.length;
long[] sum = new long[n+1];
for(int i = 1; i <= n;i++) {
sum[i] = sum[i-1] + nums[i-1];
}
TreeSet<Long> set = new TreeSet<>();
set.add(0l);
for(long x : sum) {
set.add(x);
set.add(x - lower);
set.add(x - upper - 1);
}
Map<Long,Integer> map = new HashMap<>();
int idx = 1;
for(long x : set) {
map.put(x,idx++);
}
tr = new int[idx+1];
max = idx;
int res = 0;
for(long x : sum) {
res += sum(map.get(x - lower)) - sum(map.get(x - upper - 1));
add(map.get(x),1);
}
return res;
}
public int lowbit(int x) {
return x & (-x);
}
public void add(int x, int c) {
for(int i = x; i <= max;i += lowbit(i)) {
tr[i] += c;
}
}
public int sum(int x) {
int res = 0;
for(int i = x; i >= 1; i -= lowbit(i)) {
res += tr[i];
}
return res;
}
}
边栏推荐
- 2021-11-7bugku做题记录25——POST
- rocket目录
- Hardware course design: MP3 music playback of multi-function player based on stm32
- SAP tables transparent tables and views (continuously updated)
- SAP ABAP Smartforms 踩过的坑
- Hash table
- SAP ABAP selection screen selection screen is enough to read this article (continuous update)
- 【6月3号学习进度】
- Rocket directory
- MySQL operation
猜你喜欢

Week4

368. 最大整除子集 动态规划

五、Microsoft群集服务(MSCS)环境的搭建实验报告

Hardware course design: sensor device of multi-function player based on stm32

Mise en œuvre du rapport d'expérience RAID logiciel

Hardware course design: novel reading of multi-function player based on stm32

Leetcode lecture - 873 Length of the longest Fibonacci subsequence (difficulty: medium)

Leetcode lecture - 1217 Play chips (difficulty: simple)

rocket目录

SAP ABAP smartforms stepped on the pit
随机推荐
个人博客学习记录
Type erase & bridge method
Implementation method of three column layout (generally, it is required to write as much as possible)
SAP ABAP BAPI_MATERIAL_AVAILABILITY 查询可用库存
Headfirst state mode source code
Rapport d'essai du plan de mise en œuvre de la sécurité dans les environnements San et NAS
Mycli's multiline mode
2021-11-13攻防世界做题记录01MISC
Implementation of binarysearchtree (BST) class template for binary search tree
863. All Nodes Distance K in Binary Tree
I have added 101.132.179.94 to the white list of Oracle and still failed to Ping. What should I do
Go seckill system 3 -- project structure construction and commodity model development.
"The following signature cannot be verified because there is no public key" solution
[learning progress on June 3]
Title: the nearest common ancestor of binary tree
SAP OPEN SQL
5、 Experimental report on setting up Microsoft cluster service (MSCs) environment
Unity3d ray
【LeetCode】面试题 01.05. 一次编辑
6、 Configuration experiment report of data backup software