Python/data_structures/binary_tree/lazy_segment_tree.py

100 lines
3.4 KiB
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
import math
2019-10-05 05:14:13 +00:00
class SegmentTree:
2018-10-19 12:48:28 +00:00
def __init__(self, N):
self.N = N
2019-10-05 05:14:13 +00:00
self.st = [
0 for i in range(0, 4 * N)
] # approximate the overall size of segment tree with array N
self.lazy = [0 for i in range(0, 4 * N)] # create array to store lazy update
self.flag = [0 for i in range(0, 4 * N)] # flag for lazy update
2018-10-19 12:48:28 +00:00
def left(self, idx):
2019-10-05 05:14:13 +00:00
return idx * 2
2018-10-19 12:48:28 +00:00
def right(self, idx):
2019-10-05 05:14:13 +00:00
return idx * 2 + 1
2018-10-19 12:48:28 +00:00
def build(self, idx, l, r, A): # noqa: E741
if l == r: # noqa: E741
2019-10-05 05:14:13 +00:00
self.st[idx] = A[l - 1]
else:
mid = (l + r) // 2
self.build(self.left(idx), l, mid, A)
self.build(self.right(idx), mid + 1, r, A)
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
2018-10-19 12:48:28 +00:00
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
# for each update)
def update(self, idx, l, r, a, b, val): # noqa: E741
"""
update(1, 1, N, a, b, v) for update val v to [a,b]
"""
if self.flag[idx] is True:
2018-10-19 12:48:28 +00:00
self.st[idx] = self.lazy[idx]
self.flag[idx] = False
if l != r: # noqa: E741
2018-10-19 12:48:28 +00: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
2018-10-19 12:48:28 +00:00
if r < a or l > b:
return True
if l >= a and r <= b: # noqa: E741
2018-10-19 12:48:28 +00:00
self.st[idx] = val
if l != r: # noqa: E741
2018-10-19 12:48:28 +00: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
2019-10-05 05:14:13 +00:00
mid = (l + r) // 2
self.update(self.left(idx), l, mid, a, b, val)
self.update(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
# query with O(lg N)
def query(self, idx, l, r, a, b): # noqa: E741
"""
query(1, 1, N, a, b) for query max of [a,b]
"""
if self.flag[idx] is True:
2018-10-19 12:48:28 +00:00
self.st[idx] = self.lazy[idx]
self.flag[idx] = False
if l != r: # noqa: E741
2018-10-19 12:48:28 +00: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
if r < a or l > b:
return -math.inf
if l >= a and r <= b: # noqa: E741
2018-10-19 12:48:28 +00:00
return self.st[idx]
2019-10-05 05:14:13 +00:00
mid = (l + r) // 2
q1 = self.query(self.left(idx), l, mid, a, b)
q2 = self.query(self.right(idx), mid + 1, r, a, b)
return max(q1, q2)
2018-10-19 12:48:28 +00:00
def showData(self):
showList = []
2019-10-05 05:14:13 +00:00
for i in range(1, N + 1):
2018-10-19 12:48:28 +00:00
showList += [self.query(1, 1, self.N, i, i)]
print(showList)
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(N)
2019-10-05 05:14:13 +00: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)
2018-10-19 12:48:28 +00:00
segt.showData()