-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path307. Range Sum Query - Mutable.cpp
173 lines (150 loc) · 4.37 KB
/
307. Range Sum Query - Mutable.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Yes, it passed the judge.
class NumArray {
public:
NumArray(vector<int> nums) {
this->nums = nums;
}
void update(int i, int val) {
nums[i] = val;
}
int sumRange(int i, int j) {
int sum = 0;
for(int a = i; a <= j; a++) sum += nums[a];
return sum;
}
private:
vector<int> nums;
};
// Segment Tree
class NumArray {
struct SegmentTreeNode{
int start;
int end;
int sum;
SegmentTreeNode* left;
SegmentTreeNode* right;
SegmentTreeNode(int l, int r):start(l), end(r), sum(0), left(NULL), right(NULL){}
};
SegmentTreeNode* root;
SegmentTreeNode* buildTree(vector<int>& nums, int start, int end){
if(start > end) return NULL;
SegmentTreeNode* p = new SegmentTreeNode(start, end);
if(start == end){
p->sum = nums[start];
}
else{
int mid = start + (end - start) / 2;
p->left = buildTree(nums, start, mid);
p->right = buildTree(nums, mid + 1, end);
p->sum = p->left->sum + p->right->sum;
}
return p;
}
void update(SegmentTreeNode* p, int i, int val){
if(p->start == i && p->end == i){
p->sum = val;
}
else{
int mid = p->start + (p->end - p->start) / 2;
if(i <= mid) update(p->left, i, val);
else update(p->right, i, val);
p->sum = p->left->sum + p->right->sum;
}
}
int sumRange(SegmentTreeNode* p, int i, int j){
if(p->start == i && p->end == j){
return p->sum;
}
int mid = p->start + (p->end - p->start) / 2;
if(j <= mid)
return sumRange(p->left, i, j);
else if(i > mid)
return sumRange(p->right, i, j);
else
return sumRange(p->left, i, mid) + sumRange(p->right, mid + 1, j);
}
public:
NumArray(vector<int> nums) {
root = buildTree(nums, 0, nums.size() - 1);
}
void update(int i, int val) {
update(root, i, val);
}
int sumRange(int i, int j) {
return sumRange(root, i, j);
}
};
/**
* 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);
*/
class NumArray {
struct TreeNode {
int start;
int end;
int sum;
TreeNode* left;
TreeNode* right;
TreeNode(int x, int y):start(x), end(y), sum(0), left(NULL), right(NULL) {}
};
public:
NumArray(vector<int> nums) {
root = buildTree(nums, 0, nums.size() - 1);
}
void update(int i, int val) {
dfs(root, i, val);
}
int sumRange(int i, int j) {
return i == 0 ? getSum(root, j) : getSum(root, j) - getSum(root, i - 1);
}
int getSum(TreeNode* root, int idx) {
if (root->end == idx) {
return root->sum;
}
int mid = root->start + (root->end - root->start)/2;
if (idx <= mid) {
return getSum(root->left, idx);
} else {
return root->left->sum + getSum(root->right, idx);
}
}
void dfs(TreeNode* root, int i, int val) {
if (root->start == i && root->end == i) {
root->sum = val;
return;
}
int mid = root->start + (root->end - root->start)/2;
if (i <= mid) {
dfs(root->left, i, val);
} else {
dfs(root->right, i, val);
}
root->sum = root->left->sum + root->right->sum;
}
TreeNode* buildTree(vector<int>& nums, int start, int end) {
if (start > end) {
return NULL;
}
if (start == end) {
TreeNode* node = new TreeNode(start, end);
node->sum = nums[start];
return node;
}
TreeNode* node = new TreeNode(start, end);
int mid = start + (end - start)/2;
node->left = buildTree(nums, start, mid);
node->right = buildTree(nums, mid + 1, end);
node->sum = node->left->sum + node->right->sum;
return node;
}
private:
TreeNode* root;
};
/**
* 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);
*/