-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path307. Range Sum Query - Mutable.cpp
More file actions
151 lines (124 loc) · 4.22 KB
/
307. Range Sum Query - Mutable.cpp
File metadata and controls
151 lines (124 loc) · 4.22 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <vector>
using namespace std;
struct SegmentTreeNode {
// when it's a leaf, the start == end == index, sum == val
int start;
int end;
int sum;
SegmentTreeNode* left;
SegmentTreeNode* right;
SegmentTreeNode(int start, int end, int sum, SegmentTreeNode* left, SegmentTreeNode* right):
start(start), end(end), sum(sum), left(left), right(right) {}
};
class NumArray {
public:
NumArray(vector<int>& nums) {
root_ = buildSegmentTree_(nums, 0, nums.size() - 1);
}
void update(int index, int val) {
updateSegmentTree_(root_, index, val);
}
int sumRange(int left, int right) {
return sumRange_(root_, left, right);
}
private:
SegmentTreeNode* root_;
// balanced binary tree
// T: O(n)
// S: O(n)
SegmentTreeNode* buildSegmentTree_(vector<int>& nums, int start, int end) {
if (nums.empty()) return nullptr;
// leaves
if (start == end) return new SegmentTreeNode(start, end, nums[start], nullptr, nullptr);
int mid = (start + end) / 2;
SegmentTreeNode* left = buildSegmentTree_(nums, start, mid);
SegmentTreeNode* right = buildSegmentTree_(nums, mid + 1, end);
return new SegmentTreeNode(start, end, left->sum + right->sum, left, right);
}
// T: O(logn)
// S: O(1)
void updateSegmentTree_(SegmentTreeNode* cur, int index, int val) {
int start = cur->start;
int end = cur->end;
if (start == end && start == index) {
cur->sum = val;
return;
}
int mid = (start + end) / 2;
if (index >= start && index <= mid) updateSegmentTree_(cur->left, index, val);
if (index >= mid + 1 && index <= end) updateSegmentTree_(cur->right, index, val);
// updates val
cur->sum = cur->left->sum + cur->right->sum;
return;
}
// T: O(logn + k) where k is the reported segments number
int sumRange_(SegmentTreeNode* cur, int left, int right) {
if (left == cur->start && right == cur->end) return cur->sum;
int mid = (cur->start + cur->end) / 2;
if (cur->start <= left && right <= mid) return sumRange_(cur->left, left, right);
if (mid + 1 <= left && right <= cur->end) return sumRange_(cur->right, left, right);
// spans across left and right subtrees
return sumRange_(cur->left, left, mid) + sumRange_(cur->right, mid + 1, right);
}
};
// FenwickTree, the same as BinaryIndexedTree(BIT)
// build tree T: O(nlogn), S: O(n)
// update one element T: O(logn)
// query T: O(logn)
class FenwickTree {
public:
FenwickTree(vector<int>& nums): partial_sums(nums.size() + 1, 0) {
for (int i = 0; i < nums.size(); ++i) {
int j = i + 1;
while (j < partial_sums.size()) {
partial_sums[j] += nums[i];
j += lowBit(j);
}
}
}
void update(int index, int val) {
// std::cout << "update val: " << val;
++index;
while (index < partial_sums.size()) {
// std::cout << " index: " << index << std::endl;
partial_sums[index] += val;
index += lowBit(index);
}
}
int query(int index) {
++index;
int sum = 0;
// std::cout << "index: " << index;
while (index > 0) {
sum += partial_sums[index];
index -= lowBit(index);
}
// std::cout << " sum: " << sum << std::endl;
return sum;
}
private:
vector<int> partial_sums;
inline int lowBit(int x) {
return x & (-x);
}
};
class NumArray2 {
public:
NumArray2(vector<int>& nums): bit_(nums), nums_(std::move(nums)) {}
void update(int index, int val) {
bit_.update(index, val - nums_[index]);
nums_[index] = val;
}
int sumRange(int left, int right) {
return bit_.query(right) - bit_.query(left - 1);
}
private:
FenwickTree bit_;
vector<int> nums_;
};
/**
* 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);
*/