-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
307.Range-Sum-Query-Mutable_BIT.cpp
76 lines (66 loc) · 1.63 KB
/
307.Range-Sum-Query-Mutable_BIT.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
class BIT{
public:
int N;
vector<long long>bitArr; // Note: all arrays are 1-index
vector<long long>nums;
long long M = 1e9+7;
void init(int N)
{
this->N = N;
bitArr.resize(N+1);
nums.resize(N+1);
}
// increase nums[i] by delta
void updateDelta(int i, long long delta) {
int idx = i;
while (idx <= N)
{
bitArr[idx]+=delta;
// bitArr[idx] %= M;
idx+=idx&(-idx);
}
}
// sum of a range nums[1:j] inclusively
long long queryPreSum(int idx){
long long result = 0;
while (idx){
result += bitArr[idx];
// result %= M;
idx-=idx&(-idx);
}
return result;
}
// sum of a range nums[i:j] inclusively
long long sumRange(int i, int j) {
return queryPreSum(j)-queryPreSum(i-1);
}
};
class NumArray {
BIT bit;
vector<int>nums;
public:
NumArray(vector<int>& nums)
{
this->nums = nums;
int n = nums.size();
bit.init(n+10);
for (int i=0; i<n; i++) {
bit.updateDelta(i+1, nums[i]);
}
}
void update(int index, int val)
{
bit.updateDelta(index+1, val-nums[index]);
nums[index] = val;
}
int sumRange(int left, int right)
{
return bit.sumRange(left+1, right+1);
}
};
/**
* 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);
*/