Skip to content

Commit 810e910

Browse files
authored
Update range-sum-query-mutable.cpp
1 parent a9893c7 commit 810e910

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

C++/range-sum-query-mutable.cpp

+55-56
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,62 @@
33
// query: O(logn)
44
// Space: O(n)
55

6-
// Segment Tree solution.
6+
// Binary Indexed Tree (BIT) solution.
77
class NumArray {
8+
public:
9+
NumArray(vector<int> &nums) : nums_(nums) {
10+
bit_ = vector<int>(nums_.size() + 1);
11+
for (int i = 1; i < bit_.size(); ++i) {
12+
bit_[i] = nums[i - 1] + bit_[i - 1];
13+
}
14+
for (int i = bit_.size() - 1; i >= 1; --i) {
15+
int last_i = i - lower_bit(i);
16+
bit_[i] -= bit_[last_i];
17+
}
18+
}
19+
20+
void update(int i, int val) {
21+
if (val - nums_[i]) {
22+
add(i, val - nums_[i]);
23+
nums_[i] = val;
24+
}
25+
}
26+
27+
int sumRange(int i, int j) {
28+
return sum(j) - sum(i - 1);
29+
}
30+
31+
private:
32+
vector<int> &nums_;
33+
vector<int> bit_;
34+
35+
int sum(int i) {
36+
++i;
37+
int sum = 0;
38+
for (; i > 0; i -= lower_bit(i)) {
39+
sum += bit_[i];
40+
}
41+
return sum;
42+
}
43+
44+
void add(int i, int val) {
45+
++i;
46+
for (; i <= nums_.size(); i += lower_bit(i)) {
47+
bit_[i] += val;
48+
}
49+
}
50+
51+
inline int lower_bit(int i) {
52+
return i & -i;
53+
}
54+
};
55+
56+
// Time: ctor: O(n),
57+
// update: O(logn),
58+
// query: O(logn)
59+
// Space: O(n)
60+
// Segment Tree solution.
61+
class NumArray2 {
862
public:
963
NumArray(vector<int> &nums) : nums_(nums) {
1064
root_ = buildHelper(nums, 0, nums.size() - 1);
@@ -100,61 +154,6 @@ class NumArray {
100154
}
101155
};
102156

103-
// Time: ctor: O(n),
104-
// update: O(logn),
105-
// query: O(logn)
106-
// Space: O(n)
107-
// Binary Indexed Tree (BIT) solution.
108-
class NumArray2 {
109-
public:
110-
NumArray(vector<int> &nums) : nums_(nums) {
111-
bit_ = vector<int>(nums_.size() + 1);
112-
for (int i = 1; i < bit_.size(); ++i) {
113-
bit_[i] = nums[i - 1] + bit_[i - 1];
114-
}
115-
for (int i = bit_.size() - 1; i >= 1; --i) {
116-
int last_i = i - lower_bit(i);
117-
bit_[i] -= bit_[last_i];
118-
}
119-
}
120-
121-
void update(int i, int val) {
122-
if (val - nums_[i]) {
123-
add(i, val - nums_[i]);
124-
nums_[i] = val;
125-
}
126-
}
127-
128-
int sumRange(int i, int j) {
129-
return sum(j) - sum(i - 1);
130-
}
131-
132-
private:
133-
vector<int> &nums_;
134-
vector<int> bit_;
135-
136-
int sum(int i) {
137-
++i;
138-
int sum = 0;
139-
for (; i > 0; i -= lower_bit(i)) {
140-
sum += bit_[i];
141-
}
142-
return sum;
143-
}
144-
145-
void add(int i, int val) {
146-
++i;
147-
for (; i <= nums_.size(); i += lower_bit(i)) {
148-
bit_[i] += val;
149-
}
150-
}
151-
152-
inline int lower_bit(int i) {
153-
return i & -i;
154-
}
155-
};
156-
157-
158157
// Your NumArray object will be instantiated and called as such:
159158
// NumArray numArray(nums);
160159
// numArray.sumRange(0, 1);

0 commit comments

Comments
 (0)