当前位置:网站首页>[英雄星球七月集训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
边栏推荐
- C language actual combat guessing game
- Are you still using your browser's own bookmarks? This bookmark plugin is awesome
- Binary tree - 404. Sum of left leaves
- 栈与队列——347. 前 K 个高频元素
- Leetcode169-多数元素详解
- 二叉树——530.二叉搜索树的最小绝对差
- Solve the problem of rapid index bar extrusion
- Sequence traversal II of leetcode107 binary tree
- 解决不挂载数据的页面刷新
- Stack and queue - 150. Inverse Polish expression evaluation
猜你喜欢

Leetcode200 - find detailed explanation of the number of islands

The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function

GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object

Unity—欧拉角,四元数

通货膨胀之下,后市如何操作?2021-05-14

MySQL——多版本并发控制(MVCC)

栈与队列——239. 滑动窗口最大值

Leetcode169-多数元素详解

二叉树——101. 对称二叉树

初阶C语言 - 分支语句(if、switch)
随机推荐
Under inflation, how to operate in the future? 2021-05-14
Weight file and pre training file of yolov3
Getaverse,走向Web3的远方桥梁
如何让你的 JS 代码写得更漂亮
关于拼多多根据关键词取商品列表 API 的使用说明
Binary tree -- 222. Number of nodes of a complete binary tree
回溯——17. 电话号码的字母组合
SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
Cherish time and improve efficiency
Binary tree - 530. Minimum absolute difference of binary search tree
Are you still using your browser's own bookmarks? This bookmark plugin is awesome
Elementary C language - branch statements (if, switch)
Unified handling of global exceptions
二叉树相关知识
Leetcode question brushing series -- 931. Minimum sum of descent path
Nest.js 用了 Express 但也没完全用
appium 从启动到测试再到结束流程梳理
"Demons dance", is the bull market over? 2021-05-13
06_ue4进阶_使用地形工具设置大地图
The bull market is not over yet, and there is still 2021-05-18 in the second half