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):
|
|
|
|
self.N = len(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
|
2018-10-19 12:48:28 +00:00
|
|
|
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):
|
|
|
|
return idx * 2
|
|
|
|
|
|
|
|
def right(self, idx):
|
|
|
|
return idx * 2 + 1
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
def build(self, idx, l, r): # noqa: E741
|
2022-11-29 15:56:41 +00:00
|
|
|
if l == r:
|
2018-10-19 12:48:28 +00:00
|
|
|
self.st[idx] = A[l]
|
|
|
|
else:
|
|
|
|
mid = (l + r) // 2
|
|
|
|
self.build(self.left(idx), l, mid)
|
|
|
|
self.build(self.right(idx), mid + 1, r)
|
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):
|
|
|
|
return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val)
|
2019-08-06 10:14:23 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
def update_recursive(self, idx, l, r, a, b, val): # noqa: E741
|
|
|
|
"""
|
|
|
|
update(1, 1, N, a, b, v) for update val v to [a,b]
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
if r < a or l > b:
|
|
|
|
return True
|
2022-11-29 15:56:41 +00:00
|
|
|
if l == r:
|
2018-10-19 12:48:28 +00:00
|
|
|
self.st[idx] = val
|
|
|
|
return True
|
2019-10-05 05:14:13 +00:00
|
|
|
mid = (l + r) // 2
|
2018-10-19 12:48:28 +00:00
|
|
|
self.update_recursive(self.left(idx), l, mid, a, b, val)
|
2019-10-05 05:14:13 +00:00
|
|
|
self.update_recursive(self.right(idx), mid + 1, r, a, b, val)
|
|
|
|
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):
|
|
|
|
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
def query_recursive(self, idx, l, r, a, b): # noqa: E741
|
|
|
|
"""
|
|
|
|
query(1, 1, N, a, b) for query max of [a,b]
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
if r < a or l > b:
|
|
|
|
return -math.inf
|
2022-11-29 15:56:41 +00:00
|
|
|
if l >= a and r <= b:
|
2018-10-19 12:48:28 +00:00
|
|
|
return self.st[idx]
|
2019-10-05 05:14:13 +00:00
|
|
|
mid = (l + r) // 2
|
2018-10-19 12:48:28 +00:00
|
|
|
q1 = self.query_recursive(self.left(idx), l, mid, a, b)
|
|
|
|
q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b)
|
|
|
|
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()
|