当前位置:网站首页>[英雄星球七月集训LeetCode解题日报] 第24日 线段树
[英雄星球七月集训LeetCode解题日报] 第24日 线段树
2022-07-24 23:58:00 【七水shuliang】
日报
题目
一、 剑指 Offer 51. 数组中的逆序对
1. 题目描述

2. 思路分析
- 这题可以线段树、树状数组、归并、有序集合。
- 我测试有序集合最快。
- 没给数据范围因此要离散化。
- 如果不想离散化能做吗,也能做,下面骚一点:观察代码传入的是int,实际数据范围[-2^31 , 2^31-1],我们要用线段树,得把它们都变成[1,N]之间的数,因此我们全部加上 2^32, 线段树最大值开成2^33, 这样就可以动态开点了!
3. 代码实现
class IntervalTree:
def __init__(self, size,nums=None):
self.size = size
self.nums = nums
self.interval_tree = collections.defaultdict(int)
if nums:
self.build_tree(1,1,size)
def build_tree(self,p,l,r):
interval_tree = self.interval_tree
nums = self.nums
if l == r:
interval_tree[p] = nums[l-1]
return
mid = (l+r)//2
self.build_tree(p*2,l,mid)
self.build_tree(p*2+1,mid+1,r)
interval_tree[p] = interval_tree[p*2]+interval_tree[p*2+1]
def add_point(self,p,l,r,index,add):
if index < l or r < index:
return
interval_tree = self.interval_tree
interval_tree[p] += add
if l == r:
return
mid = (l+r)//2
if index <= mid:
self.add_point(p*2,l,mid,index,add)
else:
self.add_point(p*2+1,mid+1,r,index,add)
def sum_interval(self,p,l,r,x,y):
if y < l or r < x:
return 0
interval_tree = self.interval_tree
if x<=l and r<=y:
return interval_tree[p]
mid = (l+r)//2
s = 0
if x <= mid:
s += self.sum_interval(p*2,l,mid,x,y)
if mid < y:
s += self.sum_interval(p*2+1,mid+1,r,x,y)
return s
class Solution:
def reversePairs(self, nums: List[int]) -> int:
n = len(nums)
# 离散化
# hashed = sorted({*nums})
size = 2**33
# 线段树统计逆序对
tree = IntervalTree(size)
ans = 0
for i in range(n - 1, -1, -1):
# pos = bisect_left(hashed,nums[i])+1
ans += tree.sum_interval(1,1,size,1,nums[i]-1+2**32)
tree.add_point(1,1,size,nums[i]+2**32,1)
return ans

边栏推荐
- Architecture design of multi live shopping mall
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- I'd like to ask if the table creation DDL of ODPs can't be directly executed in MySQL. The string type is incompatible. Can you only adjust this by yourself
- Regular expression learning
- 1. Smoke test
- The idea of Google's "Ai awareness" event this month
- Use and partial explanation of QDIR class
- LeetCode_ 6124_ The first letter that appears twice
- Horizontally centered element
- Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points
猜你喜欢

Bug summary

Live broadcast preview | online seminar on open source security governance models and tools

Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现

Are you still using system. Currenttimemillis()? Take a look at stopwatch

@Mapkey usage instructions

【无标题】

How to put long links into Excel

数组中只出现一次的两个数字

你还在使用System.currentTimeMillis()?来看看StopWatch吧

WP wechat export chat record backup to computer
随机推荐
Discussion on line segment tree
2. Load test
MySQL common basic commands
Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
See project code Note 1
c语言:深度刨析函数栈帧
Install Kaspersky 2018 under win server 2012 R2
Architecture design of multi live shopping mall
Detailed explanation of zhanrui Huben T310: introduce the big core and dynamiq architecture into the entry-level market for the first time!
Zheng Huijuan: Research on application scenarios and evaluation methods of data assets based on the unified market
Advanced function of postman
Js----- Chapter 4 array
With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
Analysis of WPF multi finger application development
Processing of ffmpeg wasapi can't activate audio endpoint error
Use es to realize fuzzy search and search recommendation of personal blog
Qt | 事件系统 QEvent
你还在使用System.currentTimeMillis()?来看看StopWatch吧
Restructuredtext grammar summary for beginners
Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points