2018-10-19 12:48:28 +00:00
|
|
|
import math
|
|
|
|
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
class SegmentTree:
|
2022-10-12 22:54:20 +00:00
|
|
|
def __init__(self, a):
|
2023-10-17 01:25:07 +00:00
|
|
|
self.A = a
|
|
|
|
self.N = len(self.A)
|
2019-10-05 05:14:13 +00:00
|
|
|
self.st = [0] * (
|
|
|
|
4 * self.N
|
|
|
|
) # approximate the overall size of segment tree with array N
|
2023-07-31 18:46:30 +00:00
|
|
|
if self.N:
|
|
|
|
self.build(1, 0, self.N - 1)
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
def left(self, idx):
|
2023-10-17 01:25:07 +00:00
|
|
|
"""
|
|
|
|
Returns the left child index for a given index in a binary tree.
|
|
|
|
|
|
|
|
>>> s = SegmentTree([1, 2, 3])
|
|
|
|
>>> s.left(1)
|
|
|
|
2
|
|
|
|
>>> s.left(2)
|
|
|
|
4
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
return idx * 2
|
|
|
|
|
|
|
|
def right(self, idx):
|
2023-10-17 01:25:07 +00:00
|
|
|
"""
|
|
|
|
Returns the right child index for a given index in a binary tree.
|
|
|
|
|
|
|
|
>>> s = SegmentTree([1, 2, 3])
|
|
|
|
>>> s.right(1)
|
|
|
|
3
|
|
|
|
>>> s.right(2)
|
|
|
|
5
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
return idx * 2 + 1
|
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
def build(self, idx, left, right):
|
|
|
|
if left == right:
|
|
|
|
self.st[idx] = self.A[left]
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2024-04-19 19:30:22 +00:00
|
|
|
mid = (left + right) // 2
|
|
|
|
self.build(self.left(idx), left, mid)
|
|
|
|
self.build(self.right(idx), mid + 1, right)
|
2019-10-05 05:14:13 +00:00
|
|
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
def update(self, a, b, val):
|
2023-10-17 01:25:07 +00:00
|
|
|
"""
|
|
|
|
Update the values in the segment tree in the range [a,b] with the given value.
|
|
|
|
|
|
|
|
>>> s = SegmentTree([1, 2, 3, 4, 5])
|
|
|
|
>>> s.update(2, 4, 10)
|
|
|
|
True
|
|
|
|
>>> s.query(1, 5)
|
|
|
|
10
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
def update_recursive(self, idx, left, right, a, b, val):
|
2020-05-22 06:10:11 +00:00
|
|
|
"""
|
|
|
|
update(1, 1, N, a, b, v) for update val v to [a,b]
|
|
|
|
"""
|
2024-04-19 19:30:22 +00:00
|
|
|
if right < a or left > b:
|
2018-10-19 12:48:28 +00:00
|
|
|
return True
|
2024-04-19 19:30:22 +00:00
|
|
|
if left == right:
|
2018-10-19 12:48:28 +00:00
|
|
|
self.st[idx] = val
|
|
|
|
return True
|
2024-04-19 19:30:22 +00:00
|
|
|
mid = (left + right) // 2
|
|
|
|
self.update_recursive(self.left(idx), left, mid, a, b, val)
|
|
|
|
self.update_recursive(self.right(idx), mid + 1, right, a, b, val)
|
2019-10-05 05:14:13 +00:00
|
|
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
2018-10-19 12:48:28 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def query(self, a, b):
|
2023-10-17 01:25:07 +00:00
|
|
|
"""
|
|
|
|
Query the maximum value in the range [a,b].
|
|
|
|
|
|
|
|
>>> s = SegmentTree([1, 2, 3, 4, 5])
|
|
|
|
>>> s.query(1, 3)
|
|
|
|
3
|
|
|
|
>>> s.query(1, 5)
|
|
|
|
5
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
|
|
|
|
|
2024-04-19 19:30:22 +00:00
|
|
|
def query_recursive(self, idx, left, right, a, b):
|
2020-05-22 06:10:11 +00:00
|
|
|
"""
|
|
|
|
query(1, 1, N, a, b) for query max of [a,b]
|
|
|
|
"""
|
2024-04-19 19:30:22 +00:00
|
|
|
if right < a or left > b:
|
2018-10-19 12:48:28 +00:00
|
|
|
return -math.inf
|
2024-04-19 19:30:22 +00:00
|
|
|
if left >= a and right <= b:
|
2018-10-19 12:48:28 +00:00
|
|
|
return self.st[idx]
|
2024-04-19 19:30:22 +00:00
|
|
|
mid = (left + right) // 2
|
|
|
|
q1 = self.query_recursive(self.left(idx), left, mid, a, b)
|
|
|
|
q2 = self.query_recursive(self.right(idx), mid + 1, right, a, b)
|
2018-10-19 12:48:28 +00:00
|
|
|
return max(q1, q2)
|
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def show_data(self):
|
|
|
|
show_list = []
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(1, N + 1):
|
2022-10-12 22:54:20 +00:00
|
|
|
show_list += [self.query(i, i)]
|
|
|
|
print(show_list)
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
|
2018-10-19 12:48:28 +00:00
|
|
|
N = 15
|
|
|
|
segt = SegmentTree(A)
|
2019-08-06 10:14:23 +00:00
|
|
|
print(segt.query(4, 6))
|
|
|
|
print(segt.query(7, 11))
|
|
|
|
print(segt.query(7, 12))
|
2019-10-05 05:14:13 +00:00
|
|
|
segt.update(1, 3, 111)
|
2019-08-06 10:14:23 +00:00
|
|
|
print(segt.query(1, 15))
|
2019-10-05 05:14:13 +00:00
|
|
|
segt.update(7, 8, 235)
|
2022-10-12 22:54:20 +00:00
|
|
|
segt.show_data()
|