-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
lazy_segment_tree.py-static-type-checking #2303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
aefe913
e676647
6310890
0439180
5005cbc
039cdb3
f1c86af
052f408
d5a446e
40371aa
8682782
15b1f62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,93 @@ | ||
import math | ||
from typing import List | ||
|
||
|
||
class SegmentTree: | ||
def __init__(self, N): | ||
def __init__(self, N: int) -> None: | ||
self.N = N | ||
self.st = [ | ||
self.st: List[int] = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
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 | ||
self.lazy: List[int] = [ | ||
0 for i in range(0, 4 * N) | ||
] # create array to store lazy update | ||
self.flag: List[int] = [0 for i in range(0, 4 * N)] # flag for lazy update | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly are we flagging with this variable? This type hint is not required. The typing system can already figure out that a list comprehension creates a list and that 0 is an int. This type hint just creates cognitive clutter for the reader. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, the changes I made in this pr were already included in pr#2329 which was merged recently, so I closed this pr. I'll make these changes separately and send out a new pr. Thanks |
||
|
||
def left(self, idx): | ||
def left(self, idx: int) -> int: | ||
return idx * 2 | ||
|
||
def right(self, idx): | ||
def right(self, idx: int) -> int: | ||
return idx * 2 + 1 | ||
|
||
def build(self, idx, l, r, A): # noqa: E741 | ||
if l == r: # noqa: E741 | ||
self.st[idx] = A[l - 1] | ||
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] | ||
else: | ||
mid = (l + r) // 2 | ||
self.build(self.left(idx), l, mid, A) | ||
self.build(self.right(idx), mid + 1, r, A) | ||
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) | ||
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) | ||
|
||
# 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 | ||
def update( | ||
self, idx: int, left_element: int, right_element: int, a: int, b: int, val: int | ||
) -> bool: | ||
""" | ||
update(1, 1, N, a, b, v) for update val v to [a,b] | ||
""" | ||
if self.flag[idx] is True: | ||
self.st[idx] = self.lazy[idx] | ||
self.flag[idx] = False | ||
if l != r: # noqa: E741 | ||
if left_element != right_element: | ||
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: | ||
if right_element < a or left_element > b: | ||
return True | ||
if l >= a and r <= b: # noqa: E741 | ||
if left_element >= a and right_element <= b: | ||
self.st[idx] = val | ||
if l != r: # noqa: E741 | ||
if left_element != right_element: | ||
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 | ||
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) | ||
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) | ||
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) | ||
return True | ||
|
||
# query with O(lg N) | ||
def query(self, idx, l, r, a, b): # noqa: E741 | ||
def query( | ||
self, idx: int, left_element: int, right_element: int, a: int, b: int | ||
) -> int: | ||
""" | ||
query(1, 1, N, a, b) for query max of [a,b] | ||
""" | ||
if self.flag[idx] is True: | ||
self.st[idx] = self.lazy[idx] | ||
self.flag[idx] = False | ||
if l != r: # noqa: E741 | ||
if left_element != right_element: | ||
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: | ||
if right_element < a or left_element > b: | ||
return -math.inf | ||
if l >= a and r <= b: # noqa: E741 | ||
if left_element >= a and right_element <= b: | ||
return self.st[idx] | ||
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) | ||
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) | ||
return max(q1, q2) | ||
|
||
def showData(self): | ||
def showData(self) -> None: | ||
showList = [] | ||
for i in range(1, N + 1): | ||
showList += [self.query(1, 1, self.N, i, i)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
N
in uppercase? Uppercase is reserved for constants in Python naming. Could we come up with a more self-documenting name than a single character?