-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
64 lines (56 loc) · 1.06 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class NumArray {
private:
vector<int> nums, bit;
int n;
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
public:
NumArray(vector<int> &nums) {
this->nums = nums;
this->n = nums.size();
for (int i = 0; i <= n; i++) {
bit.push_back(0);
}
for (int i = 0; i < n; i++) {
add(i + 1, nums[i]);
}
}
void update(int i, int val) {
add(i + 1, val - nums[i]);
nums[i] = val;
}
int sumRange(int i, int j) {
return sum(j + 1) - sum(i);
}
};
int main() {
vector<int> nums = {1, 3, 5};
NumArray numArray(nums);
cout << numArray.sumRange(0, 2) << endl;
numArray.update(1, 2);
cout << numArray.sumRange(0, 2) << endl;
return 0;
}