-
Notifications
You must be signed in to change notification settings - Fork 257
/
segment-tree-modify.cpp
46 lines (41 loc) · 1.25 KB
/
segment-tree-modify.cpp
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
// Time: O(h)
// Space: O(h)
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start;
* this->end = end;
* this->max = max;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
*@param root, index, value: The root of segment tree and
*@ change the node's value with [index, index] to the new given value
*@return: void
*/
void modify(SegmentTreeNode *root, int index, int value) {
// Out of range.
if (root == nullptr || root->start > index || root->end < index) {
return;
}
// Change the node's value with [index, index] to the new given value.
if (root->start == index && root->end == index) {
root->max = value;
return;
}
modify(root->left, index, value);
modify(root->right, index, value);
int left_max = root->left != nullptr? root->left->max : INT_MIN;
int right_max = root->right != nullptr? root->right->max : INT_MIN;
// Update max.
root->max = max(left_max, right_max);
}
};