-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path307.cpp
More file actions
90 lines (81 loc) · 2.51 KB
/
Copy path307.cpp
File metadata and controls
90 lines (81 loc) · 2.51 KB
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
90
struct MyTreeNode{
int val;
int start;
int end;
MyTreeNode* left;
MyTreeNode* right;
MyTreeNode(int v, int s, int e): val(v), start(s), end(e), left(nullptr), right(nullptr) {}
};
class NumArray {
private:
MyTreeNode* root;
public:
NumArray(vector<int> nums) {
if(nums.empty()) root == nullptr;
else root = buildTree(nums, root, 0, nums.size()-1);
}
void update(int i, int val) {
if(!root) return;
updateVal(root, i, val);
}
int sumRange(int i, int j) {
if(!root) return 0;
//printTree(root);
return findSum(root, i, j);
}
MyTreeNode* buildTree(vector<int> nums, MyTreeNode* root, int start, int end){
if(start > end){
return nullptr;
}
if(start == end){
return new MyTreeNode(nums[start], start, end);
}
root = new MyTreeNode(0, start, end);
int mid = start + (end-start)/2;
root->left = buildTree(nums, root->left, start, mid);
root->right = buildTree(nums, root->right, mid+1, end);
root->val = root->left->val + root->right->val;
return root;
}
void printTree(MyTreeNode* curr){
if(curr == nullptr) return;
cout << curr->val;
cout << "[" ;
printTree(curr->left);
cout << "," ;
printTree(curr->right);
cout << "]" ;
}
int updateVal(MyTreeNode* current, int i, int val){
int start = current->start;
int end = current->end;
if(start == i && end == i){
int difference = val - current->val;
current->val = val;
return difference;
}
else if(start == end) return 0;
int mid = start + (end-start)/2;
int diff;
if(i > mid) diff = updateVal(current->right, i, val);
else diff = updateVal(current->left, i, val);
current->val += diff;
return diff;
}
int findSum(MyTreeNode* current, int start, int end){
if(current == nullptr) return 0;
int curr_s = current->start;
int curr_e = current->end;
if(curr_s == start && curr_e == end) return current->val;
int mid = curr_s + (curr_e - curr_s)/2;
if(end <= mid) return findSum(current->left, start, end);
else if(start > mid) return findSum(current->right, start, end);
else return findSum(current->left, start, mid)+findSum(current->right, mid+1, end);
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.update(i,val);
* int param_2 = obj.sumRange(i,j);
*/