-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange_Sum_Query_Mutable.cpp
More file actions
56 lines (53 loc) · 1.24 KB
/
Range_Sum_Query_Mutable.cpp
File metadata and controls
56 lines (53 loc) · 1.24 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
class NumArray {
public:
vector<int> tree;
int n;
void build(vector<int>& nums){
for(int i=n,j=0; i<2*n; i++,j++)
tree[i] = nums[j];
for(int i=n-1;i>0;i--)
tree[i]=tree[i*2] + tree[1+ i*2];
}
NumArray(vector<int>& nums) {
if(nums.size()>0){
n = nums.size();
tree.clear();
tree.resize(2*n);
build(nums);
}
}
void update(int pos, int val) {
pos += n;
tree[pos] = val;
while(pos>0){
int left = pos;
int right = pos;
if(pos%2==0)
right = pos+1;
else
left = pos-1;
tree[pos/2] = tree[left] + tree[right];
pos /= 2;
}
}
int sumRange(int l, int r) {
l += n;
r += n;
int sum = 0;
while(l<=r){
if(l%2 == 1)
sum += tree[l++];
if(r%2 == 0)
sum += tree[r--];
l /= 2;
r /= 2;
}
return sum;
}
};
/**
* 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);
*/