📜  Python中的段树模块

📅  最后修改于: 2022-05-13 01:55:03.926000             🧑  作者: Mango

Python中的段树模块

先决条件:段树实现
段树是一种数据结构,允许程序员有效地解决给定数组的范围查询并修改数组值。基本上,段树是一种非常灵活和高效的数据结构,大量的问题可以借助段树来解决。

Python Segment tree Module也用于解决范围查询问题。它在给定范围内执行各种操作,如summaxmin 更新一个范围内的值。该模块有助于避免分割树的实现,因为我们可以直接使用分割树函数来执行所有操作。
它通常可以减少实现 Segment 树的压力。

安装库:

pip install segment-tree

段树的功能:

  • 查询:它是段树的主要函数,执行诸如查找范围内的最大数,查找范围内的最小数以及查找给定范围的总和等操作。它需要 3 个参数作为输入,它们是 start_index(即范围将从哪里开始)、End_index(即到哪个索引范围结束)和要执行的操作。
    句法:
obj.query(Start_index, End_index, operation_name)
  • 更新:段树的第二个主要函数是更新,它将更新范围内特定索引的值。它将用新值替换该索引处存在的现有值。
    句法:
obj.update(index, value)

示例 1:

Python3
from segment_tree import SegmentTree
 
# an array with some elements
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
 
# here we are fitting our array
# into segment tree where t is
# taken as object of segment tree
# t will be used for performing
# operations on that segmentTree
 
t = SegmentTree(arr)
 
# here we are finding value
# of maximum number in a range
a = t.query(2, 9, "max")
print("The maximum value of this range is : ", a)
 
 
# here we are finding the value
# of minimum number in a range
a = t.query(2, 9, "min")
print("The minimum value of this range is : ", a)
 
 
# here we are finding the value
# of sum of a range
a = t.query(2, 7, "sum")
print("The sum of this range is : ", a)
 
 
# here we are updating the value
# of a particular index
t.update(2, 25)
 
# it will replace the value of
# index '2' with 25
print("The updated array is : ", arr)


Python3
from segment_tree import SegmentTree
 
 
# an array with some elements
arr = [14, 28, 55, 105, 78, 4, 24, 99, 48, 200]
 
# here we are fitting our array
# into segment tree where t is
# taken as object of segment tree
# t will be used for performing
# operations on that segmentTree
 
t = SegmentTree(arr)
 
# here we are finding value of
# maximum number in a range
a = t.query(0, 9, "max")
print("The maximum value of this range is : ", a)
 
# here we are finding value of
# minimum number in a range
a = t.query(0, 9, "min")
print("The minimum value of this range is : ", a)
 
# here we are finding value
# of sum of a range
a = t.query(0, 9, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(5, 0)
print("The updated array is : ", arr)
 
 
# here we are finding value of
# sum of a range
a = t.query(1, 5, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(4, 10)
print("The updated array is : ", arr)


输出:

The maximum value of this range is :  10
The minimum value of this range is :  3
The sum of this range is :  33
The updated array is :  [1, 2, 25, 4, 5, 6, 7, 8, 9, 10, 11]

示例 2:

Python3

from segment_tree import SegmentTree
 
 
# an array with some elements
arr = [14, 28, 55, 105, 78, 4, 24, 99, 48, 200]
 
# here we are fitting our array
# into segment tree where t is
# taken as object of segment tree
# t will be used for performing
# operations on that segmentTree
 
t = SegmentTree(arr)
 
# here we are finding value of
# maximum number in a range
a = t.query(0, 9, "max")
print("The maximum value of this range is : ", a)
 
# here we are finding value of
# minimum number in a range
a = t.query(0, 9, "min")
print("The minimum value of this range is : ", a)
 
# here we are finding value
# of sum of a range
a = t.query(0, 9, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(5, 0)
print("The updated array is : ", arr)
 
 
# here we are finding value of
# sum of a range
a = t.query(1, 5, "sum")
print("The sum of this range is : ", a)
 
# here we are updating the value
# of a particular index
t.update(4, 10)
print("The updated array is : ", arr)

输出:

The maximum value of this range is :  200
The minimum value of this range is :  4
The sum of this range is :  655
The updated array is :  [14, 28, 55, 105, 78, 0, 24, 99, 48, 200]
The sum of this range is :  266
The updated array is :  [14, 28, 55, 105, 10, 0, 24, 99, 48, 200]