当前位置:网站首页>[brother hero June training] day 25: tree array
[brother hero June training] day 25: tree array
2022-07-27 10:21:00 【If I were Wen Shuai】
Series articles
【 Brother hero training in June 】 The first 01 God : Array
【 Brother hero training in June 】 The first 02 God : character string
【 Brother hero training in June 】 The first 03 God : Sort
【 Brother hero training in June 】 The first 04 God : greedy
【 Brother hero training in June 】 The first 05 God : Double pointer
【 Brother hero training in June 】 The first 06 God : The sliding window
【 Brother hero training in June 】 The first 07 God : Hash
【 Brother hero training in June 】 The first 08 God : The prefix and
【 Brother hero training in June 】 The first 09 God : Two points search
【 Brother hero training in June 】 The first 10 God : An operation
【 Brother hero training in June 】 The first 11 God : matrix
【 Brother hero training in June 】 The first 12 God : Linked list
【 Brother hero training in June 】 The first 13 God : Double linked list
【 Brother hero training in June 】 The first 14 God : Stack
【 Brother hero training in June 】 The first 15 God : Binary tree
【 Brother hero training in June 】 The first 16 God : queue
【 Brother hero training in June 】 The first 17 God : Breadth first search
【 Brother hero training in June 】 The first 18 God : Trees
【 Brother hero training in June 】 The first 19 God : Binary tree
【 Brother hero training in June 】 The first 20 God : Binary search tree
【 Brother hero training in June 】 The first 21 God : Pile up ( Priority queue )
【 Brother hero training in June 】 The first 22 God : Ordered set
【 Brother hero training in June 】 The first 23 God : Dictionary tree
【 Brother hero training in June 】 The first 24 God : Line segment tree
【 Brother hero training in June 】 The first 25 God : Tree array
List of articles
Tree array
Tree array or binary index tree ( English :Binary Indexed Tree), Named after its inventor Fenwick Trees , The earliest by Peter M. Fenwick On 1994 In the past years A New Data Structure for Cumulative Frequency Tables The title is published in SOFTWARE PRACTICE AND EXPERIENCE. Its original intention is to solve the cumulative frequency in data compression (Cumulative Frequency) The calculation of , Now it is mostly used to efficiently calculate the prefix and , Interval and .
One 、 Flip it right
class Solution {
class BIT{
int[] tree;
int n;
public BIT(int n){
this.n = n;
this.tree = new int[n+1];
}
public static int lowbit(int x){
return x & (-x);
}
public void update(int x, int d){
while( x<=n){
tree[x] +=d;
x += lowbit(x);
}
}
public int query(int x){
int ans = 0;
while(x != 0){
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
}
public int reversePairs(int[] nums) {
Set<Long> allNumbers = new TreeSet<Long>();
for(int x:nums){
allNumbers.add((long) x);
allNumbers.add((long) x * 2);
}
Map<Long,Integer> values = new HashMap<Long, Integer>();
int idx = 0;
for(long x : allNumbers){
values.put(x,idx);
idx++;
}
int ret = 0;
BIT bit = new BIT(values.size());
for (int i = 0; i<nums.length; i++){
int left = values.get((long) nums[i]*2), right=values.size()-1;
ret +=bit.query(right+1)-bit.query(left+1);
bit.update(values.get((long) nums[i]) +1,1);
}
return ret;
}
}
Two 、 Reverse pairs in arrays
The finger of the sword Offer 51. Reverse pairs in arrays
class Solution {
class BIT{
int[] tree;
int n;
public BIT(int n){
this.n = n;
this.tree = new int[n+1];
}
public static int lowbit(int x){
return x & (-x);
}
public void update(int x){
int d=1;
while( x<=n){
tree[x] +=d;
x += lowbit(x);
}
}
public int query(int x){
int ans = 0;
while(x != 0){
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
}
public int reversePairs(int[] nums) {
int n=nums.length;
int[] tmp = new int[n];
System.arraycopy(nums,0,tmp,0,n);
Arrays.sort(tmp);
for(int i=0;i<n;i++){
nums[i]= Arrays.binarySearch(tmp,nums[i])+1;
}
BIT bit = new BIT(n);
int ans = 0;
for(int i = n-1;i>=0;--i){
ans += bit.query(nums[i]-1);
bit.update(nums[i]);
}
return ans;
}
}
summary
Copy an array
System.arraycopy(nums,0,tmp,0,n);
边栏推荐
- 超赞的卡尔曼滤波详解文章
- Decision tree principle and case application - Titanic survival prediction
- 3D修复论文:Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks
- Matlab的不同进制转换
- Configuration of pytorch deep learning environment based on cuda10.0
- Acl2021 best paper released, from ByteDance
- wind10配置adb命令
- SE(Squeeze and Excitation)模块的理解以及代码实现
- Overview of PCL modules (1.6)
- 线代004
猜你喜欢

Ant advanced task

Sub query of database performance series

【Flutter】SharedPreferences使用

Failure of CUDA installation nsight visual studio edition failed

Snowflake vs. databricks who is better? The latest war report in 2022

Shell综合应用案例,归档文件、发送消息

Metaaploit-后渗透技知识

Configuration of pytorch deep learning environment based on cuda10.0

卸载CUDA11.1

wind10配置adb命令
随机推荐
PCL的ICP配准示例
Word2vec principle and application and article similarity (recommended system method)
Snowflake vs. databricks who is better? The latest war report in 2022
Xiandai 004
Practice and exploration of overseas site Seata of ant group
wind10配置adb命令
Huawei switch dual uplink networking smart Link Configuration Guide
【英雄哥六月集训】第 23天: 字典树
Decision tree principle and case application - Titanic survival prediction
数学推理题:张王李赵陈五对夫妇聚会,见面握手
How does data analysis solve business problems? Here is a super detailed introduction
Overview of PCL modules (1.6)
Food safety | is sugar free really sugar free? These truths need to be known
线代003
StyleGAN论文笔记+修改代码尝试3D点云生成
邮件服务器
Oracle查看硬解析
使用 Kmeans聚类实现颜色的分割
matlab-绘制分叉与混沌分支图
Color segmentation using kmeans clustering