当前位置:网站首页>[hero planet July training leetcode problem solving daily] 24th line segment tree
[hero planet July training leetcode problem solving daily] 24th line segment tree
2022-07-25 00:02:00 【Seven water Shuliang】
[ Hero planet July training LeetCode Problem solving daily ] The first 24 Japan Line segment tree
daily
subject
One 、 The finger of the sword Offer 51. Reverse pairs in arrays
link : The finger of the sword Offer 51. Reverse pairs in arrays
1. Title Description

2. Thought analysis
- This problem can be line segment tree 、 Tree array 、 Merger 、 Ordered set .
- I test the fastest ordered set .
- No data range is given, so it needs to be discretized .
- Can you do it if you don't want to discretize , Can also do , Be coquettish : Observe what the code passes in int, Actual data range [-2^31 , 2^31-1], We'll use a segment tree , We have to turn them all into [1,N] Number between , So we all add 2^32, The maximum value of the segment tree is 2^33, In this way, it can be opened dynamically !
3. Code implementation
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)
# discretization
# hashed = sorted({*nums})
size = 2**33
# Segment tree statistical reverse pair
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

边栏推荐
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- 【无标题】
- Use of serial queues
- Routing policy in republishing
- UART
- 多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
- Branch and loop statements in C language learning
- QT learning - using database singleton to complete login matching + registration function
- Shell echo command
- Pain and happiness -nio programming
猜你喜欢

Go基础笔记_4_map

Use es to realize fuzzy search and search recommendation of personal blog

The new version of SSM video tutorial in shangsilicon valley was released

Notes of Teacher Li Hongyi's 2020 in-depth learning series 5

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge

Let me introduce you to the partition automatic management of data warehouse

c语言:深度刨析函数栈帧

做一个文艺的测试/开发程序员,慢慢改变自己......

在混合云中管理数据库:八个关键注意事项

Notes of Teacher Li Hongyi's 2020 in-depth learning series 6
随机推荐
Flash send email
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
@Mapkey usage instructions
Qt | 事件系统 QEvent
WP wechat export chat record backup to computer
Kubernetes application design guide
云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
采坑记录:TypeError: 'module' object is not callable
SQL rewriting Series 6: predicate derivation
3. Pressure test
Log4j configuration file
Upgrade the jdbc driver to version 8.0.28 and connect to the pit record of MySQL
Live broadcast preview | online seminar on open source security governance models and tools
50 places are limited to open | with the news of oceanbase's annual press conference coming!
Notes of Teacher Li Hongyi's 2020 in-depth learning series 3
Coding builds an image, inherits the self built basic image, and reports an error unauthorized: invalid credential Please confirm that you have entered the correct user name and password.
Browser cache
QT | event system qevent
Can Baidu network disk yundetectservice.exe be disabled and closed
Install software on kubernetes cluster using helm 3 package manager