-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path307.cpp
More file actions
61 lines (49 loc) · 1.03 KB
/
Copy path307.cpp
File metadata and controls
61 lines (49 loc) · 1.03 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
class NumArray {
public:
int* a;
int n;
NumArray(vector<int>& nums) {
n = nums.size();
a = new int[2*n];
for(int i = n; i < 2*n; ++i)
{
a[i] = nums[i-n];
}
for(int i = n-1; i >= 1; --i)
{
a[i] = a[2*i] + a[2*i+1];
}
}
void update(int i, int val) {
int p = n + i;
int c = val - a[p];
a[p] = val;
p /= 2;
while(p >= 1)
{
a[p] += c;
p /= 2;
}
}
int sumRange(int i, int j) {
int sum = 0;
i += n;
j += n;
while(i <= j)
{
if(i % 2 == 1)
{
sum += a[i];
++i;
}
if(j % 2 == 0)
{
sum += a[j];
--j;
}
i /= 2;
j /= 2;
}
return sum;
}
};