-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path307.cpp
More file actions
43 lines (39 loc) · 1.18 KB
/
307.cpp
File metadata and controls
43 lines (39 loc) · 1.18 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
class NumArray { //uses SegTree implementation from https://usaco.guide/gold/PURS?lang=cpp
private:
int comb(int a, int b) {return a + b;}
int len;
vector<int> segtree;
public:
NumArray(vector<int>& nums) { //O(N log N)
len = nums.size();
segtree = vector<int>(len * 2);
for(int i = 0; i < nums.size(); i++){
update(i, nums[i]);
}
}
void update(int ind, int val) { //O(log N)
ind += len;
segtree[ind] = val;
for (; ind > 1; ind /= 2) {
segtree[ind >> 1] = comb(segtree[ind], segtree[ind ^ 1]);
}
}
int sumRange(int start, int end) { //O(log N)
int sum = 0; end++;
for (start += len, end += len; start < end; start /= 2, end /= 2) {
if ((start & 1) != 0) {
sum = comb(sum, segtree[start++]);
}
if ((end & 1) != 0) {
sum = comb(sum, segtree[--end]);
}
}
return sum;
}
};
/**
* 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);
*/