-
Notifications
You must be signed in to change notification settings - Fork 22
/
203.segment-tree-modify.py
89 lines (83 loc) · 3.3 KB
/
203.segment-tree-modify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Tag: Segment Tree
# Time: O(logN)
# Space: O(1)
# Ref: -
# Note: -
# For a `Maximum Segment Tree`, which each node has an extra value `max` to store the maximum value in this node's interval.
#
# Implement a `modify` function with three parameter `root`, `index` and `value` to change the node's change the node's value with _**[start, end] = [index, index]**_ to the new given value in segment tree with root `root`.
# Guarantee every node in segment tree still has the **max** attribute with the correct value after change.
#
# **Example 1:**
# ```
# Input:"[1,4,max=3][1,2,max=2][3,4,max=3][1,1,max=2][2,2,max=1][3,3,max=0][4,4,max=3]",2,4
# Output:"[1,4,max=4][1,2,max=4][3,4,max=3][1,1,max=2][2,2,max=4][3,3,max=0][4,4,max=3]"
# Explanation:
# For segment tree:
#
# [1, 4, max=3]
# / \
# [1, 2, max=2] [3, 4, max=3]
# / \ / \
# [1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=3]
#
# if call modify(root, 2, 4), we can get:
#
# [1, 4, max=4]
# / \
# [1, 2, max=4] [3, 4, max=3]
# / \ / \
# [1, 1, max=2], [2, 2, max=4], [3, 3, max=0], [4, 4, max=3]
# ```
#
#
# **Example 2:**
# ```
# Input:"[1,4,max=3][1,2,max=2][3,4,max=3][1,1,max=2][2,2,max=1][3,3,max=0][4,4,max=3]",4,0
# Output:"[1,4,max=4][1,2,max=4][3,4,max=0][1,1,max=2][2,2,max=4][3,3,max=0][4,4,max=0]"
# Explanation:
# For segment tree:
#
# [1, 4, max=3]
# / \
# [1, 2, max=2] [3, 4, max=3]
# / \ / \
# [1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=3]
# if call modify(root, 4, 0), we can get:
#
# [1, 4, max=2]
# / \
# [1, 2, max=2] [3, 4, max=0]
# / \ / \
# [1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=0]
# ```
#
# We suggest you finish problem [Segment Tree Build](http://www.lintcode.com/problem/segment-tree-build/ "Segment Tree Build") and [Segment Tree Query](http://www.lintcode.com/problem/segment-tree-query/ "Segment Tree Query") first.
from lintcode import (
SegmentTreeNode,
)
"""
Definition of SegmentTreeNode:
class SegmentTreeNode:
def __init__(self, start, end, max):
self.start, self.end, self.max = start, end, max
self.left, self.right = None, None
"""
class Solution:
"""
@param root: The root of segment tree.
@param index: index.
@param value: value
@return: nothing
"""
def modify(self, root: SegmentTreeNode, index: int, value: int):
# write your code here
if index == root.start and index == root.end:
root.max = value
return
mid = (root.start + root.end) // 2
if index <= mid:
self.modify(root.left, index, value)
else:
self.modify(root.right, index, value)
root.max = max(root.left.max, root.right.max)