2018-10-19 20:48:28 +08:00
|
|
|
import math
|
2020-08-21 12:54:34 +08:00
|
|
|
from typing import List
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2019-10-05 13:14:13 +08:00
|
|
|
class SegmentTree:
|
2020-08-21 12:54:34 +08:00
|
|
|
def __init__(self, N: int) -> None:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.N = N
|
2020-08-21 12:54:34 +08:00
|
|
|
# approximate the overall size of segment tree with array N
|
|
|
|
self.st: List[int] = [0 for i in range(0, 4 * N)]
|
|
|
|
# create array to store lazy update
|
|
|
|
self.lazy: List[int] = [0 for i in range(0, 4 * N)]
|
|
|
|
self.flag: List[int] = [0 for i in range(0, 4 * N)] # flag for lazy update
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
def left(self, idx: int) -> int:
|
|
|
|
"""
|
|
|
|
>>> segment_tree = SegmentTree(15)
|
|
|
|
>>> segment_tree.left(1)
|
|
|
|
2
|
|
|
|
>>> segment_tree.left(2)
|
|
|
|
4
|
|
|
|
>>> segment_tree.left(12)
|
|
|
|
24
|
|
|
|
"""
|
2019-10-05 13:14:13 +08:00
|
|
|
return idx * 2
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
def right(self, idx: int) -> int:
|
|
|
|
"""
|
|
|
|
>>> segment_tree = SegmentTree(15)
|
|
|
|
>>> segment_tree.right(1)
|
|
|
|
3
|
|
|
|
>>> segment_tree.right(2)
|
|
|
|
5
|
|
|
|
>>> segment_tree.right(12)
|
|
|
|
25
|
|
|
|
"""
|
2019-10-05 13:14:13 +08:00
|
|
|
return idx * 2 + 1
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
def build(
|
|
|
|
self, idx: int, left_element: int, right_element: int, A: List[int]
|
|
|
|
) -> None:
|
|
|
|
if left_element == right_element:
|
|
|
|
self.st[idx] = A[left_element - 1]
|
2019-10-05 13:14:13 +08:00
|
|
|
else:
|
2020-08-21 12:54:34 +08:00
|
|
|
mid = (left_element + right_element) // 2
|
|
|
|
self.build(self.left(idx), left_element, mid, A)
|
|
|
|
self.build(self.right(idx), mid + 1, right_element, A)
|
2019-10-05 13:14:13 +08:00
|
|
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
def update(
|
|
|
|
self, idx: int, left_element: int, right_element: int, a: int, b: int, val: int
|
|
|
|
) -> bool:
|
2020-05-22 14:10:11 +08:00
|
|
|
"""
|
2020-08-21 12:54:34 +08:00
|
|
|
update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
|
|
|
|
for each update)
|
|
|
|
|
2020-05-22 14:10:11 +08:00
|
|
|
update(1, 1, N, a, b, v) for update val v to [a,b]
|
|
|
|
"""
|
|
|
|
if self.flag[idx] is True:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.st[idx] = self.lazy[idx]
|
|
|
|
self.flag[idx] = False
|
2020-08-21 12:54:34 +08:00
|
|
|
if left_element != right_element:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.lazy[self.left(idx)] = self.lazy[idx]
|
|
|
|
self.lazy[self.right(idx)] = self.lazy[idx]
|
|
|
|
self.flag[self.left(idx)] = True
|
|
|
|
self.flag[self.right(idx)] = True
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
if right_element < a or left_element > b:
|
2018-10-19 20:48:28 +08:00
|
|
|
return True
|
2020-08-21 12:54:34 +08:00
|
|
|
if left_element >= a and right_element <= b:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.st[idx] = val
|
2020-08-21 12:54:34 +08:00
|
|
|
if left_element != right_element:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.lazy[self.left(idx)] = val
|
|
|
|
self.lazy[self.right(idx)] = val
|
|
|
|
self.flag[self.left(idx)] = True
|
|
|
|
self.flag[self.right(idx)] = True
|
|
|
|
return True
|
2020-08-21 12:54:34 +08:00
|
|
|
mid = (left_element + right_element) // 2
|
|
|
|
self.update(self.left(idx), left_element, mid, a, b, val)
|
|
|
|
self.update(self.right(idx), mid + 1, right_element, a, b, val)
|
2019-10-05 13:14:13 +08:00
|
|
|
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
|
2018-10-19 20:48:28 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
# query with O(lg N)
|
2020-08-21 12:54:34 +08:00
|
|
|
def query(
|
|
|
|
self, idx: int, left_element: int, right_element: int, a: int, b: int
|
|
|
|
) -> int:
|
2020-05-22 14:10:11 +08:00
|
|
|
"""
|
|
|
|
query(1, 1, N, a, b) for query max of [a,b]
|
2020-08-21 12:54:34 +08:00
|
|
|
>>> A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
|
|
|
|
>>> segment_tree = SegmentTree(15)
|
|
|
|
>>> segment_tree.build(1, 1, 15, A)
|
|
|
|
>>> segment_tree.query(1, 1, 15, 4, 6)
|
|
|
|
7
|
|
|
|
>>> segment_tree.query(1, 1, 15, 7, 11)
|
|
|
|
14
|
|
|
|
>>> segment_tree.query(1, 1, 15, 7, 12)
|
|
|
|
15
|
2020-05-22 14:10:11 +08:00
|
|
|
"""
|
|
|
|
if self.flag[idx] is True:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.st[idx] = self.lazy[idx]
|
|
|
|
self.flag[idx] = False
|
2020-08-21 12:54:34 +08:00
|
|
|
if left_element != right_element:
|
2018-10-19 20:48:28 +08:00
|
|
|
self.lazy[self.left(idx)] = self.lazy[idx]
|
|
|
|
self.lazy[self.right(idx)] = self.lazy[idx]
|
|
|
|
self.flag[self.left(idx)] = True
|
|
|
|
self.flag[self.right(idx)] = True
|
2020-08-21 12:54:34 +08:00
|
|
|
if right_element < a or left_element > b:
|
2018-10-19 20:48:28 +08:00
|
|
|
return -math.inf
|
2020-08-21 12:54:34 +08:00
|
|
|
if left_element >= a and right_element <= b:
|
2018-10-19 20:48:28 +08:00
|
|
|
return self.st[idx]
|
2020-08-21 12:54:34 +08:00
|
|
|
mid = (left_element + right_element) // 2
|
|
|
|
q1 = self.query(self.left(idx), left_element, mid, a, b)
|
|
|
|
q2 = self.query(self.right(idx), mid + 1, right_element, a, b)
|
2019-10-05 13:14:13 +08:00
|
|
|
return max(q1, q2)
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2020-08-21 12:54:34 +08:00
|
|
|
def show_data(self) -> None:
|
2018-10-19 20:48:28 +08:00
|
|
|
showList = []
|
2019-10-05 13:14:13 +08:00
|
|
|
for i in range(1, N + 1):
|
2018-10-19 20:48:28 +08:00
|
|
|
showList += [self.query(1, 1, self.N, i, i)]
|
2019-08-06 18:14:23 +08:00
|
|
|
print(showList)
|
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
|
2019-10-05 13:14:13 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
|
2018-10-19 20:48:28 +08:00
|
|
|
N = 15
|
|
|
|
segt = SegmentTree(N)
|
2019-10-05 13:14:13 +08:00
|
|
|
segt.build(1, 1, N, A)
|
|
|
|
print(segt.query(1, 1, N, 4, 6))
|
|
|
|
print(segt.query(1, 1, N, 7, 11))
|
|
|
|
print(segt.query(1, 1, N, 7, 12))
|
|
|
|
segt.update(1, 1, N, 1, 3, 111)
|
|
|
|
print(segt.query(1, 1, N, 1, 15))
|
|
|
|
segt.update(1, 1, N, 7, 8, 235)
|
2020-08-21 12:54:34 +08:00
|
|
|
segt.show_data()
|