-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path307.cpp
52 lines (44 loc) · 1.17 KB
/
307.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
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using std::vector;
class NumArray {
public:
NumArray(vector<int>& nums) {
// 建立树
int n = nums.size();
this->n = n;
t = new int[n+1];
_nums = new int[n];
std::fill(t, t+n+1, 0);
std::copy(nums.begin(), nums.end(), _nums);
for (int i = 0; i < n; i++) {
int num = nums[i];
for (int x = i+1; x<=n; x += bitlow(x)) t[x] += num;
}
// std::copy(t+1, t+n+1, std::ostream_iterator<int>(std::cout, " "));
}
void update(int i, int val) {
int diff = val - _nums[i];
_nums[i] = val;
for (int x = i+1; x<=n; x += bitlow(x)) t[x] += diff;
}
int sumRange(int i, int j) {
int ans_i = 0, ans_j = 0;
for (int t_i = i; t_i; t_i -= bitlow(t_i)) ans_i += t[t_i]; // ask(i-1)
for (int t_j = j + 1; t_j; t_j -= bitlow(t_j)) ans_j += t[t_j]; // ask(j)
return ans_j - ans_i;
}
int bitlow(int x) {
return x & (-x);
}
~NumArray() {
if (t)
delete[] t;
}
private:
int* _nums;
int* t;
int n;
};