-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0307-range-sum-query-mutable.cpp
More file actions
44 lines (41 loc) · 1.1 KB
/
0307-range-sum-query-mutable.cpp
File metadata and controls
44 lines (41 loc) · 1.1 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
class NumArray {
public:
int n;
vector<int> sums;
NumArray(vector<int>& nums) {
n = (int) nums.size();
sums.resize(2 * n, 0);
for (int i = 0; i < n; i++) {
sums[i + n] = nums[i];
}
for (int i = n - 1; i > 0; i--) {
sums[i] = sums[i << 1] + sums[i << 1 | 1];
}
}
void update(int index, int val) {
for (sums[index += n] = val; index > 1; index >>= 1) {
sums[index >> 1] = sums[index] + sums[index ^ 1];
}
}
int sumRange(int left, int right) {
right++;
int res = 0;
for (left += n, right += n; left < right; left >>= 1, right >>= 1) {
if (left & 1) {
res += sums[left];
left++;
}
if (right & 1) {
right--;
res += sums[right];
}
}
return res;
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* obj->update(index,val);
* int param_2 = obj->sumRange(left,right);
*/