当前位置:网站首页>[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

边栏推荐
- 云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
- 采坑记录:TypeError: 'module' object is not callable
- 2022 Henan Mengxin League game 2: Henan University of technology K - Rice
- Wine wechat initialization 96% stuck
- Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
- Implement a avatar looping control
- Use SQLite provided by the system
- Transmission download list, download file migration machine guide
- SQL file import database - Nanny level tutorial
- Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
猜你喜欢

Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station

Redis6.2 SYSTEMd startup prompt redis service: Failed with result ‘protocol‘.

C language program environment and preprocessing

Can Baidu network disk yundetectservice.exe be disabled and closed

Technical operation

salesforce零基础学习(一百一十六)workflow -&gt; flow浅谈

1、 MFC introduction

Heap sort summary

给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享

HTB-Aragog
随机推荐
[英雄星球七月集训LeetCode解题日报] 第24日 线段树
Yaml writing rules and comparison between yaml and JSON
HTB-Aragog
Use of serial queues
Google Earth engine - the use of the neighborhood tobands function
Optaplanner will abandon DRL (drools) scoring method!!!
Be an artistic test / development programmer and slowly change yourself
Use SQLite provided by the system
NFT chain game system development metauniverse gamefi construction
Install Kaspersky 2018 under win server 2012 R2
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
WP wechat export chat record backup to computer
Processing PDF and JPG files in VB6
Branch and loop statements in C language learning
Notes of Teacher Li Hongyi's 2020 in-depth learning series 5
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
Remember the problem of using redisson to step on the pit once
Grafana - influxdb visual K6 output
Bug summary
MATLAB basic grammar (II)