Skip to content

Commit 2728bc7

Browse files
committed
feat: add solutions to lc problem: No.0370
No.0370.Range Addition
1 parent 9568e38 commit 2728bc7

File tree

3 files changed

+385
-1
lines changed

3 files changed

+385
-1
lines changed

solution/0300-0399/0370.Range Addition/README.md

+204-1
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,29 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40-
差分数组。
40+
**方法一:差分数组**
41+
42+
时间复杂度 O(n)。
43+
44+
**方法二:树状数组 + 差分思想**
45+
46+
时间复杂度 O(nlogn)。
47+
48+
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
49+
50+
1. **单点更新** `update(x, delta)`: 把序列 x 位置的数加上一个值 delta;
51+
1. **前缀和查询** `query(x)`:查询序列 `[1,...x]` 区间的区间和,即位置 x 的前缀和。
52+
53+
这两个操作的时间复杂度均为 `O(log n)`
4154

4255
<!-- tabs:start -->
4356

4457
### **Python3**
4558

4659
<!-- 这里可写当前语言的特殊实现逻辑 -->
4760

61+
差分数组:
62+
4863
```python
4964
class Solution:
5065
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
@@ -58,10 +73,46 @@ class Solution:
5873
return delta
5974
```
6075

76+
树状数组:
77+
78+
```python
79+
class BinaryIndexedTree:
80+
def __init__(self, n):
81+
self.n = n
82+
self.c = [0] * (n + 1)
83+
84+
@staticmethod
85+
def lowbit(x):
86+
return x & -x
87+
88+
def update(self, x, delta):
89+
while x <= self.n:
90+
self.c[x] += delta
91+
x += BinaryIndexedTree.lowbit(x)
92+
93+
def query(self, x):
94+
s = 0
95+
while x:
96+
s += self.c[x]
97+
x -= BinaryIndexedTree.lowbit(x)
98+
return s
99+
100+
101+
class Solution:
102+
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
103+
tree = BinaryIndexedTree(length)
104+
for start, end, inc in updates:
105+
tree.update(start + 1, inc)
106+
tree.update(end + 2, -inc)
107+
return [tree.query(i + 1) for i in range(length)]
108+
```
109+
61110
### **Java**
62111

63112
<!-- 这里可写当前语言的特殊实现逻辑 -->
64113

114+
差分数组:
115+
65116
```java
66117
class Solution {
67118
public int[] getModifiedArray(int length, int[][] updates) {
@@ -80,8 +131,60 @@ class Solution {
80131
}
81132
```
82133

134+
树状数组:
135+
136+
```java
137+
class Solution {
138+
public int[] getModifiedArray(int length, int[][] updates) {
139+
BinaryIndexedTree tree = new BinaryIndexedTree(length);
140+
for (int[] e : updates) {
141+
int start = e[0], end = e[1], inc = e[2];
142+
tree.update(start + 1, inc);
143+
tree.update(end + 2, -inc);
144+
}
145+
int[] ans = new int[length];
146+
for (int i = 0; i < length; ++i) {
147+
ans[i] = tree.query(i + 1);
148+
}
149+
return ans;
150+
}
151+
}
152+
153+
class BinaryIndexedTree {
154+
private int n;
155+
private int[] c;
156+
157+
public BinaryIndexedTree(int n) {
158+
this.n = n;
159+
c = new int[n + 1];
160+
}
161+
162+
public void update(int x, int delta) {
163+
while (x <= n) {
164+
c[x] += delta;
165+
x += lowbit(x);
166+
}
167+
}
168+
169+
public int query(int x) {
170+
int s = 0;
171+
while (x > 0) {
172+
s += c[x];
173+
x -= lowbit(x);
174+
}
175+
return s;
176+
}
177+
178+
public static int lowbit(int x) {
179+
return x & -x;
180+
}
181+
}
182+
```
183+
83184
### **C++**
84185

186+
差分数组:
187+
85188
```cpp
86189
class Solution {
87190
public:
@@ -97,8 +200,60 @@ public:
97200
};
98201
```
99202
203+
树状数组:
204+
205+
```cpp
206+
class BinaryIndexedTree {
207+
public:
208+
int n;
209+
vector<int> c;
210+
211+
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
212+
213+
void update(int x, int delta) {
214+
while (x <= n)
215+
{
216+
c[x] += delta;
217+
x += lowbit(x);
218+
}
219+
}
220+
221+
int query(int x) {
222+
int s = 0;
223+
while (x > 0)
224+
{
225+
s += c[x];
226+
x -= lowbit(x);
227+
}
228+
return s;
229+
}
230+
231+
int lowbit(int x) {
232+
return x & -x;
233+
}
234+
};
235+
236+
class Solution {
237+
public:
238+
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
239+
BinaryIndexedTree* tree = new BinaryIndexedTree(length);
240+
for (auto& e : updates)
241+
{
242+
int start = e[0], end = e[1], inc = e[2];
243+
tree->update(start + 1, inc);
244+
tree->update(end + 2, -inc);
245+
}
246+
vector<int> ans;
247+
for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1));
248+
return ans;
249+
}
250+
};
251+
```
252+
100253
### **Go**
101254

255+
差分数组:
256+
102257
```go
103258
func getModifiedArray(length int, updates [][]int) []int {
104259
delta := make([]int, length)
@@ -115,6 +270,54 @@ func getModifiedArray(length int, updates [][]int) []int {
115270
}
116271
```
117272

273+
树状数组:
274+
275+
```go
276+
type BinaryIndexedTree struct {
277+
n int
278+
c []int
279+
}
280+
281+
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
282+
c := make([]int, n+1)
283+
return &BinaryIndexedTree{n, c}
284+
}
285+
286+
func (this *BinaryIndexedTree) lowbit(x int) int {
287+
return x & -x
288+
}
289+
290+
func (this *BinaryIndexedTree) update(x, delta int) {
291+
for x <= this.n {
292+
this.c[x] += delta
293+
x += this.lowbit(x)
294+
}
295+
}
296+
297+
func (this *BinaryIndexedTree) query(x int) int {
298+
s := 0
299+
for x > 0 {
300+
s += this.c[x]
301+
x -= this.lowbit(x)
302+
}
303+
return s
304+
}
305+
306+
func getModifiedArray(length int, updates [][]int) []int {
307+
tree := newBinaryIndexedTree(length)
308+
for _, e := range updates {
309+
start, end, inc := e[0], e[1], e[2]
310+
tree.update(start+1, inc)
311+
tree.update(end+2, -inc)
312+
}
313+
ans := make([]int, length)
314+
for i := range ans {
315+
ans[i] = tree.query(i + 1)
316+
}
317+
return ans
318+
}
319+
```
320+
118321
### **...**
119322

120323
```

0 commit comments

Comments
 (0)