当前位置:网站首页>【英雄哥六月集训】第 25天: 树状数组
【英雄哥六月集训】第 25天: 树状数组
2022-07-27 10:01:00 【如果我是温帅帅】
系列文章
树状数组
树状数组或二叉索引树(英语:Binary Indexed Tree),又以其发明者命名为Fenwick树,最早由Peter M. Fenwick于1994年以A New Data Structure for Cumulative Frequency Tables为题发表在SOFTWARE PRACTICE AND EXPERIENCE。其初衷是解决数据压缩里的累积频率(Cumulative Frequency)的计算问题,现多用于高效计算数列的前缀和, 区间和。
一、 翻转对
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;
}
}
二、 数组中的逆序对
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;
}
}
总结
拷贝数组
System.arraycopy(nums,0,tmp,0,n);
边栏推荐
- Shell read read console input, use of read
- Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function
- LeetCode.565. 数组嵌套____暴力dfs->剪枝dfs->原地修改
- Switch port mirroring Configuration Guide
- 邮件服务器
- WGAN、WGAN-GP、BigGAN
- window平台下本地连接远程服务器数据库(一)
- When I went to oppo for an interview, I got numb
- ACL2021最佳论文出炉,来自字节跳动
- Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
猜你喜欢

SE(Squeeze and Excitation)模块的理解以及代码实现

活体检测综述

Food safety | are you still eating fermented rice noodles? Be careful these foods are poisonous!

wind10配置adb命令

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

Word2vec principle and application and article similarity (recommended system method)

3D face reconstruction and dense alignment with position map progression network
![Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号

RobotFramework+Eclispe环境安装篇

Shell integrated application cases, archiving files, sending messages
随机推荐
Matlab的不同进制转换
Matlab- draw superimposed ladder diagram and line diagram
Decision tree principle and case application - Titanic survival prediction
Anaconda installation (very detailed)
Metasploit-永恒之蓝攻击
使用 LSM-Tree 思想基于.NET 6.0 C# 写个 KV 数据库(案例版)
Matlab-离散事件系统仿真实验
Shell integrated application cases, archiving files, sending messages
Robotframework+eclispe environment installation
卸载CUDA11.1
Anaconda安装(非常详细)
Oracle调整数据文件大小杂谈
FTP 服务器
QT learning (II) -- a brief introduction to QT Creator
Visual slam lecture notes (I): Lecture 1 + Lecture 2
Discussion on a problem
语音数据采集-实时语音数据可视化
VS2019+CUDA11.1新建项目里没有CUDA选项
邮件服务器
Snowflake vs. Databricks谁更胜一筹?2022年最新战报