@@ -19,68 +19,72 @@ def left(self, idx: int) -> int:
19
19
def right (self , idx : int ) -> int :
20
20
return idx * 2 + 1
21
21
22
- def build (self , idx : int , l : int , r : int , A : List [int ]) -> None : # noqa: E741
23
- if l == r : # noqa: E741
24
- self .st [idx ] = A [l - 1 ]
22
+ def build (
23
+ self , idx : int , left_element : int , right_element : int , A : List [int ]
24
+ ) -> None :
25
+ if left_element == right_element :
26
+ self .st [idx ] = A [left_element - 1 ]
25
27
else :
26
- mid = (l + r ) // 2
27
- self .build (self .left (idx ), l , mid , A )
28
- self .build (self .right (idx ), mid + 1 , r , A )
28
+ mid = (left_element + right_element ) // 2
29
+ self .build (self .left (idx ), left_element , mid , A )
30
+ self .build (self .right (idx ), mid + 1 , right_element , A )
29
31
self .st [idx ] = max (self .st [self .left (idx )], self .st [self .right (idx )])
30
32
31
33
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
32
34
# for each update)
33
35
def update (
34
- self , idx : int , l : int , r : int , a : int , b : int , val : int
35
- ) -> bool : # noqa: E741
36
+ self , idx : int , left_element : int , right_element : int , a : int , b : int , val : int
37
+ ) -> bool :
36
38
"""
37
39
update(1, 1, N, a, b, v) for update val v to [a,b]
38
40
"""
39
41
if self .flag [idx ] is True :
40
42
self .st [idx ] = self .lazy [idx ]
41
43
self .flag [idx ] = False
42
- if l != r : # noqa: E741
44
+ if left_element != right_element :
43
45
self .lazy [self .left (idx )] = self .lazy [idx ]
44
46
self .lazy [self .right (idx )] = self .lazy [idx ]
45
47
self .flag [self .left (idx )] = True
46
48
self .flag [self .right (idx )] = True
47
49
48
- if r < a or l > b :
50
+ if right_element < a or left_element > b :
49
51
return True
50
- if l >= a and r <= b : # noqa: E741
52
+ if left_element >= a and right_element <= b :
51
53
self .st [idx ] = val
52
- if l != r : # noqa: E741
54
+ if left_element != right_element :
53
55
self .lazy [self .left (idx )] = val
54
56
self .lazy [self .right (idx )] = val
55
57
self .flag [self .left (idx )] = True
56
58
self .flag [self .right (idx )] = True
57
59
return True
58
- mid = (l + r ) // 2
59
- self .update (self .left (idx ), l , mid , a , b , val )
60
- self .update (self .right (idx ), mid + 1 , r , a , b , val )
60
+ mid = (left_element + right_element ) // 2
61
+ self .update (self .left (idx ), left_element , mid , a , b , val )
62
+ self .update (self .right (idx ), mid + 1 , right_element , a , b , val )
61
63
self .st [idx ] = max (self .st [self .left (idx )], self .st [self .right (idx )])
62
64
return True
63
65
64
66
# query with O(lg N)
65
- def query (self , idx : int , l : int , r : int , a : int , b : int ) -> int : # noqa: E741
67
+ def query (
68
+ self , idx : int , left_element : int , right_element : int , a : int , b : int
69
+ ) -> int :
66
70
"""
67
71
query(1, 1, N, a, b) for query max of [a,b]
68
72
"""
69
73
if self .flag [idx ] is True :
70
74
self .st [idx ] = self .lazy [idx ]
71
75
self .flag [idx ] = False
72
- if l != r : # noqa: E741
76
+ if left_element != right_element :
73
77
self .lazy [self .left (idx )] = self .lazy [idx ]
74
78
self .lazy [self .right (idx )] = self .lazy [idx ]
75
79
self .flag [self .left (idx )] = True
76
80
self .flag [self .right (idx )] = True
77
- if r < a or l > b :
81
+ if right_element < a or left_element > b :
78
82
return - math .inf
79
- if l >= a and r <= b : # noqa: E741
83
+ if left_element >= a and right_element <= b :
80
84
return self .st [idx ]
81
- mid = (l + r ) // 2
82
- q1 = self .query (self .left (idx ), l , mid , a , b )
83
- q2 = self .query (self .right (idx ), mid + 1 , r , a , b )
85
+ mid = (left_element + right_element ) // 2
86
+ q1 = self .query (self .left (idx ), left_element , mid , a , b )
87
+ q2 = self .query (self .right (idx ), mid + 1 , right_element , a , b )
84
88
return max (q1 , q2 )
85
89
86
90
def showData (self ) -> None :
0 commit comments