Skip to content

Commit 0b3fc2b

Browse files
authored
feat: add solutions to lc problem: No.0307 (#1959)
No.0307.Range Sum Query - Mutable
1 parent b51a566 commit 0b3fc2b

File tree

8 files changed

+1118
-352
lines changed

8 files changed

+1118
-352
lines changed

solution/0300-0399/0307.Range Sum Query - Mutable/README.md

+443-63
Large diffs are not rendered by default.

solution/0300-0399/0307.Range Sum Query - Mutable/README_EN.md

+427-57
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
1-
class BinaryIndexedTree {
2-
public:
3-
int n;
4-
vector<int> c;
5-
6-
BinaryIndexedTree(int _n)
7-
: n(_n)
8-
, c(_n + 1) {}
9-
10-
void update(int x, int delta) {
11-
while (x <= n) {
12-
c[x] += delta;
13-
x += lowbit(x);
14-
}
15-
}
16-
17-
int query(int x) {
18-
int s = 0;
19-
while (x > 0) {
20-
s += c[x];
21-
x -= lowbit(x);
22-
}
23-
return s;
24-
}
25-
26-
int lowbit(int x) {
27-
return x & -x;
28-
}
29-
};
30-
31-
class NumArray {
32-
public:
33-
BinaryIndexedTree* tree;
34-
35-
NumArray(vector<int>& nums) {
36-
int n = nums.size();
37-
tree = new BinaryIndexedTree(n);
38-
for (int i = 0; i < n; ++i) tree->update(i + 1, nums[i]);
39-
}
40-
41-
void update(int index, int val) {
42-
int prev = sumRange(index, index);
43-
tree->update(index + 1, val - prev);
44-
}
45-
46-
int sumRange(int left, int right) {
47-
return tree->query(right + 1) - tree->query(left);
48-
}
49-
};
50-
51-
/**
52-
* Your NumArray object will be instantiated and called as such:
53-
* NumArray* obj = new NumArray(nums);
54-
* obj->update(index,val);
55-
* int param_2 = obj->sumRange(left,right);
1+
class BinaryIndexedTree {
2+
public:
3+
int n;
4+
vector<int> c;
5+
6+
BinaryIndexedTree(int _n)
7+
: n(_n)
8+
, c(_n + 1) {}
9+
10+
void update(int x, int delta) {
11+
while (x <= n) {
12+
c[x] += delta;
13+
x += x & -x;
14+
}
15+
}
16+
17+
int query(int x) {
18+
int s = 0;
19+
while (x > 0) {
20+
s += c[x];
21+
x -= x & -x;
22+
}
23+
return s;
24+
}
25+
};
26+
27+
class NumArray {
28+
public:
29+
BinaryIndexedTree* tree;
30+
31+
NumArray(vector<int>& nums) {
32+
int n = nums.size();
33+
tree = new BinaryIndexedTree(n);
34+
for (int i = 0; i < n; ++i) tree->update(i + 1, nums[i]);
35+
}
36+
37+
void update(int index, int val) {
38+
int prev = sumRange(index, index);
39+
tree->update(index + 1, val - prev);
40+
}
41+
42+
int sumRange(int left, int right) {
43+
return tree->query(right + 1) - tree->query(left);
44+
}
45+
};
46+
47+
/**
48+
* Your NumArray object will be instantiated and called as such:
49+
* NumArray* obj = new NumArray(nums);
50+
* obj->update(index,val);
51+
* int param_2 = obj->sumRange(left,right);
5652
*/
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,53 @@
1-
public class NumArray {
2-
private int[] sums;
3-
private int numsCount;
4-
private int sumsCount;
1+
class BinaryIndexedTree {
2+
private int n;
3+
private int[] c;
54

6-
public NumArray(int[] nums) {
7-
numsCount = nums.Length;
8-
sumsCount = 1;
9-
var x = numsCount;
10-
while (x > 1)
11-
{
12-
x /= 2;
13-
sumsCount *= 2;
5+
public BinaryIndexedTree(int n) {
6+
this.n = n;
7+
c = new int[n + 1];
8+
}
9+
10+
public void Update(int x, int delta) {
11+
while (x <= n) {
12+
c[x] += delta;
13+
x += x & -x;
1414
}
15-
sumsCount = sumsCount * 2 - 1 + numsCount;
16-
sums = new int[sumsCount + 1];
15+
}
1716

18-
for (var i = sumsCount; i > 0; --i)
19-
{
20-
if (i - sumsCount + numsCount - 1 >= 0)
21-
{
22-
sums[i] = nums[i - sumsCount + numsCount - 1];
23-
}
24-
else
25-
{
26-
var l = i * 2;
27-
var r = l + 1;
28-
if (l <= sumsCount)
29-
{
30-
sums[i] += sums[l];
31-
if (r <= sumsCount)
32-
{
33-
sums[i] += sums[r];
34-
}
35-
}
36-
}
17+
public int Query(int x) {
18+
int s = 0;
19+
while (x > 0) {
20+
s += c[x];
21+
x -= x & -x;
3722
}
38-
//System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(sums));
23+
return s;
3924
}
25+
}
4026

41-
public void Update(int i, int val) {
42-
var j = sumsCount - numsCount + i + 1;
43-
sums[j] = val;
44-
for (j /= 2; j > 0; j /= 2)
45-
{
46-
var l = j * 2;
47-
var r = l + 1;
48-
sums[j] = sums[l];
49-
if (r <= sumsCount)
50-
{
51-
sums[j] += sums[r];
52-
}
27+
public class NumArray {
28+
private BinaryIndexedTree tree;
29+
30+
public NumArray(int[] nums) {
31+
int n = nums.Length;
32+
tree = new BinaryIndexedTree(n);
33+
for (int i = 0; i < n; ++i) {
34+
tree.Update(i + 1, nums[i]);
5335
}
54-
//System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(sums));
5536
}
5637

57-
public int SumRange(int i, int j) {
58-
return SumFromStart(j) - SumFromStart(i - 1);
38+
public void Update(int index, int val) {
39+
int prev = SumRange(index, index);
40+
tree.Update(index + 1, val - prev);
5941
}
6042

61-
private int SumFromStart(int i)
62-
{
63-
if (i < 0) return 0;
64-
var j = sumsCount - numsCount + i + 1;
65-
var sum = sums[j];
66-
for (; j / 2 > 0; j /= 2)
67-
{
68-
if (j % 2 != 0)
69-
{
70-
sum += sums[j - 1];
71-
}
72-
}
73-
return sum;
43+
public int SumRange(int left, int right) {
44+
return tree.Query(right + 1) - tree.Query(left);
7445
}
75-
}
46+
}
47+
48+
/**
49+
* Your NumArray object will be instantiated and called as such:
50+
* NumArray obj = new NumArray(nums);
51+
* obj.Update(index,val);
52+
* int param_2 = obj.SumRange(left,right);
53+
*/

solution/0300-0399/0307.Range Sum Query - Mutable/Solution.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
88
return &BinaryIndexedTree{n, c}
99
}
1010

11-
func (this *BinaryIndexedTree) lowbit(x int) int {
12-
return x & -x
13-
}
14-
15-
func (this *BinaryIndexedTree) update(x, delta int) {
16-
for x <= this.n {
17-
this.c[x] += delta
18-
x += this.lowbit(x)
11+
func (t *BinaryIndexedTree) update(x, delta int) {
12+
for ; x <= t.n; x += x & -x {
13+
t.c[x] += delta
1914
}
2015
}
2116

22-
func (this *BinaryIndexedTree) query(x int) int {
23-
s := 0
24-
for x > 0 {
25-
s += this.c[x]
26-
x -= this.lowbit(x)
17+
func (t *BinaryIndexedTree) query(x int) (s int) {
18+
for ; x > 0; x -= x & -x {
19+
s += t.c[x]
2720
}
2821
return s
2922
}
@@ -40,13 +33,13 @@ func Constructor(nums []int) NumArray {
4033
return NumArray{tree}
4134
}
4235

43-
func (this *NumArray) Update(index int, val int) {
44-
prev := this.SumRange(index, index)
45-
this.tree.update(index+1, val-prev)
36+
func (t *NumArray) Update(index int, val int) {
37+
prev := t.SumRange(index, index)
38+
t.tree.update(index+1, val-prev)
4639
}
4740

48-
func (this *NumArray) SumRange(left int, right int) int {
49-
return this.tree.query(right+1) - this.tree.query(left)
41+
func (t *NumArray) SumRange(left int, right int) int {
42+
return t.tree.query(right+1) - t.tree.query(left)
5043
}
5144

5245
/**
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,53 @@
1-
class BinaryIndexedTree {
2-
private int n;
3-
private int[] c;
4-
5-
public BinaryIndexedTree(int n) {
6-
this.n = n;
7-
c = new int[n + 1];
8-
}
9-
10-
public void update(int x, int delta) {
11-
while (x <= n) {
12-
c[x] += delta;
13-
x += lowbit(x);
14-
}
15-
}
16-
17-
public int query(int x) {
18-
int s = 0;
19-
while (x > 0) {
20-
s += c[x];
21-
x -= lowbit(x);
22-
}
23-
return s;
24-
}
25-
26-
public static int lowbit(int x) {
27-
return x & -x;
28-
}
29-
}
30-
31-
class NumArray {
32-
private BinaryIndexedTree tree;
33-
34-
public NumArray(int[] nums) {
35-
int n = nums.length;
36-
tree = new BinaryIndexedTree(n);
37-
for (int i = 0; i < n; ++i) {
38-
tree.update(i + 1, nums[i]);
39-
}
40-
}
41-
42-
public void update(int index, int val) {
43-
int prev = sumRange(index, index);
44-
tree.update(index + 1, val - prev);
45-
}
46-
47-
public int sumRange(int left, int right) {
48-
return tree.query(right + 1) - tree.query(left);
49-
}
50-
}
51-
52-
/**
53-
* Your NumArray object will be instantiated and called as such:
54-
* NumArray obj = new NumArray(nums);
55-
* obj.update(index,val);
56-
* int param_2 = obj.sumRange(left,right);
1+
class BinaryIndexedTree {
2+
private int n;
3+
private int[] c;
4+
5+
public BinaryIndexedTree(int n) {
6+
this.n = n;
7+
c = new int[n + 1];
8+
}
9+
10+
public void update(int x, int delta) {
11+
while (x <= n) {
12+
c[x] += delta;
13+
x += x & -x;
14+
}
15+
}
16+
17+
public int query(int x) {
18+
int s = 0;
19+
while (x > 0) {
20+
s += c[x];
21+
x -= x & -x;
22+
}
23+
return s;
24+
}
25+
}
26+
27+
class NumArray {
28+
private BinaryIndexedTree tree;
29+
30+
public NumArray(int[] nums) {
31+
int n = nums.length;
32+
tree = new BinaryIndexedTree(n);
33+
for (int i = 0; i < n; ++i) {
34+
tree.update(i + 1, nums[i]);
35+
}
36+
}
37+
38+
public void update(int index, int val) {
39+
int prev = sumRange(index, index);
40+
tree.update(index + 1, val - prev);
41+
}
42+
43+
public int sumRange(int left, int right) {
44+
return tree.query(right + 1) - tree.query(left);
45+
}
46+
}
47+
48+
/**
49+
* Your NumArray object will be instantiated and called as such:
50+
* NumArray obj = new NumArray(nums);
51+
* obj.update(index,val);
52+
* int param_2 = obj.sumRange(left,right);
5753
*/

0 commit comments

Comments
 (0)