-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
70 lines (64 loc) · 1.45 KB
/
program.cpp
File metadata and controls
70 lines (64 loc) · 1.45 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "../include/pre.h"
class NumArray {
public:
NumArray(vector<int> &nums)
{
MakeBIT(nums);
}
void update(int i, int val)
{
val = val - GetN(i);
i += 1;
while (i < BIT_.size()) {
BIT_[i] += val;
i += i & (i ^ (i - 1));
}
}
int sumRange(int i, int j)
{
return sumToN(j + 1) - sumToN(i);
}
private:
int sumToN(int n)
{
int sum = 0;
while (n != 0)
{
sum += BIT_[n];
int k = n & (n ^ (n - 1));
n -= k;
}
return sum;
}
int GetN(int n)
{
return sumRange(n, n);
}
void MakeBIT(const vector<int>& nums)
{
BIT_.resize(nums.size() + 1, 0);
for (int i = 1; i != BIT_.size(); i++) {
int k = i & (i ^ (i - 1)); // 2^k, k : count of zero from end
int sum = nums[i - 1];
for (int j = 1; j != k; j *= 2) {
sum += BIT_[i - j];
}
BIT_[i] = sum;
}
}
vector<int> BIT_;
};
// 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);
int main()
{
NumArray p {vector<int>{1, 2, 3, 4, 5, 6, 7, }};
//cout << p.sumToN(1) << endl;
cout << p.sumRange(0, 5) << endl;
p.update(1, 10);
cout << p.sumRange(0, 5) << endl;
return 0;
}