Skip to content

Commit 9ca3468

Browse files
committed
Time: 520 ms (63.68%), Space: 175.5 MB (20.91%) - LeetHub
1 parent 165f2df commit 9ca3468

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

0307-range-sum-query-mutable/0307-range-sum-query-mutable.cpp

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,115 @@
11
class NumArray {
22
public:
33

4+
public:
45
void buildTree(int idx, int low, int high)
56
{
67
if (low == high)
78
{
8-
seg[idx] = v[low];
9+
tree[idx] = arr[low];
910
return;
1011
}
1112

12-
int mid = (low + high) / 2;
13+
int mid = (low + high) >> 1;
1314
buildTree(2 * idx + 1, low, mid);
1415
buildTree(2 * idx + 2, mid + 1, high);
1516

16-
// update
17-
seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2];
18-
}
17+
// updation part
18+
19+
// sum
20+
tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2];
1921

20-
int query(int idx, int low, int high, int l, int r)
22+
// min
23+
// tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
24+
return;
25+
}
26+
27+
public:
28+
int queryTree(int idx, int low, int high, int l, int r)
2129
{
2230
// no overlap
2331
// l r low high or low high l r
2432
if (r < low or l > high)
2533
return 0;
34+
// return INT_MAX; // min
2635

2736
// complete overlap
2837
// [l low high r]
2938
if (low >= l and high <= r)
30-
return seg[idx];
39+
return tree[idx];
3140

3241
// partial overlap
3342
int mid = (low + high) / 2;
34-
int left = query(2 * idx + 1, low, mid, l, r);
35-
int right = query(2 * idx + 2, mid + 1, high, l, r);
43+
int left = queryTree(2 * idx + 1, low, mid, l, r);
44+
int right = queryTree(2 * idx + 2, mid + 1, high, l, r);
45+
46+
// updation part
47+
// sum
3648

37-
// update
3849
return left + right;
50+
51+
// min
52+
// return min(left, right);
3953
}
4054

41-
void update(int idx, int low, int high, int ind, int val)
55+
public:
56+
void updateTree(int idx, int low, int high, int ind, int val)
4257
{
4358
if (low == high)
4459
{
45-
seg[idx] = val;
60+
// sum ranges increment tree[idx] by val case
61+
// tree[idx] += val;
62+
63+
// min and update single value case
64+
tree[idx] = val;
4665
return;
4766
}
48-
49-
int mid = (low + high) / 2;
67+
int mid = (low + high) >> 1;
5068
if (ind <= mid)
51-
update(2 * idx + 1, low, mid, ind, val);
69+
updateTree(2 * idx + 1, low, mid, ind, val);
5270
else
53-
update(2 * idx + 2, mid + 1, high, ind, val);
71+
updateTree(2 * idx + 2, mid + 1, high, ind, val);
5472

55-
// update
56-
seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2];
73+
// updation Part
74+
// sum
75+
76+
tree[idx] = tree[2 * idx + 1] + tree[2 * idx + 2];
77+
78+
// min
79+
80+
// tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
81+
}
82+
83+
void build()
84+
{
85+
buildTree(0, 0, N - 1);
5786
}
5887

59-
vector<int> seg;
60-
vector<int> v;
88+
int query(int l, int r)
89+
{
90+
return queryTree(0, 0, N - 1, l, r);
91+
}
92+
93+
void Update(int ind, int val)
94+
{
95+
updateTree(0, 0, N - 1, ind, val);
96+
}
6197

98+
vector<int> tree, arr;
99+
int N;
62100
NumArray(vector<int>& nums) {
63-
int n = nums.size();
64-
seg.resize(n * 4 + 5);
65-
v = nums;
66-
buildTree(0,0,n-1);
101+
N = nums.size();
102+
arr = nums;
103+
tree.resize(N*4 + 5);
104+
build();
67105
}
68106

69107
void update(int index, int val) {
70-
update(0,0,v.size()-1,index,val);
108+
Update(index,val);
71109
}
72110

73111
int sumRange(int left, int right) {
74-
return query(0,0,v.size()-1,left,right);
112+
return query(left, right);
75113
}
76114
};
77115

0 commit comments

Comments
 (0)