当前位置:网站首页>[英雄星球七月集训LeetCode解题日报] 第25日 树状数组
[英雄星球七月集训LeetCode解题日报] 第25日 树状数组
2022-07-26 00:06:00 【七水shuliang】
日报
- 今天的题很多种解法。还是有序集合快。
题目
一、 327. 区间和的个数
链接: 1327. 区间和的个数
1. 题目描述

2. 思路分析
- 多种解法,本质是转化为前缀和后统计s[j]-upper<=s[i]<=s[j]-lower,统计这些i的数量。
- 因此可以用PUIQ模型统计。
3. 代码实现
树状数组
class BinIndexTree:
def __init__(self, size):
self.size = size
self.bin_tree = [0 for _ in range(size*4)]
def add(self,i,v):
while i<=self.size :
self.bin_tree[i] += v
i += self.lowbit(i)
def sum(self,i):
s = 0
while i >= 1:
s += self.bin_tree[i]
i -= self.lowbit(i)
return s
def lowbit(self,x):
return x&-x
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
s = list(accumulate(nums,initial=0))
hashes = s + [ x-lower for x in s] + [ x-upper for x in s]
hashes = sorted(list(set(hashes)))
# 生成前缀和,问题转化为,对于每个j,找左边的i,判断 s[j]-upper<=s[i]<=s[j]-lower,统计这些i的数量
# 把所有前缀和数组中的数字插入线段树,并对这些数字划分区间,线段树维护当前区间数字数量,
# 所以需要对这些数字都散列化
# 这里用树状数组实现上述操作
# 树状数组也是维护每个数字出现的次数
tree_size = len(hashes)
tree = BinIndexTree(tree_size)
cnt = 0
for i in s:
x = bisect_left(hashes,i-upper)
y = bisect_left(hashes,i-lower)
j = bisect_left(hashes,i)
c = tree.sum(y+1) - tree.sum(x)
cnt += c
tree.add(j+1,1)
return cnt
SortedList
from sortedcontainers import SortedList
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
# 生成前缀和,问题转化为,对于每个j,找左边的i,判断 s[j]-upper<=s[i]<=s[j]-lower,统计这些i的数量
# 把所有前缀和数组中的数字插入有序列表,查询这个范围内的数字数量
arr = SortedList()
ans = 0
for i in accumulate(nums,initial=0):
ans += arr.bisect_right(i-lower)-arr.bisect_left(i-upper)
arr.add(i)
return ans
边栏推荐
- Pyqt5 rapid development and actual combat.pdf sharing
- 本轮牛市还能持续多久?|疑问解答 2021-05-11
- SSM environment integration
- Js理解之路:Js常见的6中继承方式
- Binary tree - 112. Path sum
- Binary tree -- 111. Minimum depth of binary tree
- MPLS中的包交换和标签交换
- “动物币”凶猛,陷阱还是机遇?2021-05-12
- Binary tree 101. Symmetric binary tree
- FreeMarker view integration
猜你喜欢

The bull market will continue. Take your money 2021-05-08

SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
![[one library] mapbox GL! A map engine out of the box](/img/50/70e4679e74b2a53a3011e5a8e847da.jpg)
[one library] mapbox GL! A map engine out of the box

URL address mapping configuration

回溯——17. 电话号码的字母组合

Leetcode200 - find detailed explanation of the number of islands

MPLS实验

How to use yolov5 as an intelligent transportation system for red light running monitoring (1)

Old laptop becomes server (laptop + intranet penetration)

Js理解之路:Js常见的6中继承方式
随机推荐
C语言实战之猜拳游戏
GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
STM32 pit encountered when using timer to do delay function
FreeRTOS个人笔记-消息队列
获得JD商品详情原数据 API
LeetCode_55_跳跃游戏
栈与队列——347. 前 K 个高频元素
Binary tree - 110. Balanced binary tree
Leetcode107-二叉树的层序遍历II详解
Js理解之路:写一个比较完美的组合继承(ES5)
The mobile version of Duoyu security browser will add new functions to make users browse more personalized
初阶C语言 - 分支语句(if、switch)
Pytorch学习记录(一):Pytorch 简介
如何让你的 JS 代码写得更漂亮
Binary tree - 530. Minimum absolute difference of binary search tree
Observer model of behavioral model
回溯——77. 组合
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理
bond网卡模式配置