forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeSumQueryMutable.cpp
75 lines (66 loc) · 2.04 KB
/
RangeSumQueryMutable.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
// Source : https://leetcode.com/problems/range-sum-query-mutable/
// Author : Hao Chen
// Date : 2015-11-24
/***************************************************************************************
*
* Given an integer array nums, find the sum of the elements between indices i and j (i
* ≤ j), inclusive.
*
* The update(i, val) function modifies nums by updating the element at index i to val.
*
* Example:
*
* Given nums = [1, 3, 5]
*
* sumRange(0, 2) -> 9
* update(1, 2)
* sumRange(0, 2) -> 8
*
* Note:
*
* The array is only modifiable by the update function.
* You may assume the number of calls to update and sumRange function is distributed
* evenly.
*
***************************************************************************************/
// The following idea is using `Binary Index Tree`
// There are two articles explaine this technique quite well:
// 1) http://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
// 2) http://cs.stackexchange.com/questions/10538/bit-what-is-the-intuition-behind-a-binary-indexed-tree-and-how-was-it-thought-a
class NumArray {
private:
int _sz;
vector<int> _nums;
vector<int> _sums;
public:
NumArray(vector<int> &nums) {
_sz = nums.size();
_nums.resize(_sz+1, 0);
_sums.resize(_sz+1, 0);
for(int i=0; i< _sz; i++) {
update(i, nums[i]);
}
}
void update(int i, int val) {
int oldv = _nums[i+1];
for(int idx = i+1; idx <= _sz; idx += (idx & (-idx)) ) {
_sums[idx] = _sums[idx] - oldv + val;
}
_nums[i+1] = val;
}
int sumRange(int i, int j) {
return sumRange(j+1) - sumRange(i);
}
int sumRange(int i) {
int ret = 0;
for(int idx=i; idx>0; idx -= (idx & (-idx)) ) {
ret += _sums[idx];
}
return ret;
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);