37
37
38
38
<!-- 这里可写通用的实现逻辑 -->
39
39
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) ` 。
41
54
42
55
<!-- tabs:start -->
43
56
44
57
### ** Python3**
45
58
46
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
60
61
+ 差分数组:
62
+
48
63
``` python
49
64
class Solution :
50
65
def getModifiedArray (self , length : int , updates : List[List[int ]]) -> List[int ]:
@@ -58,10 +73,46 @@ class Solution:
58
73
return delta
59
74
```
60
75
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
+
61
110
### ** Java**
62
111
63
112
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
113
114
+ 差分数组:
115
+
65
116
``` java
66
117
class Solution {
67
118
public int [] getModifiedArray (int length , int [][] updates ) {
@@ -80,8 +131,60 @@ class Solution {
80
131
}
81
132
```
82
133
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
+
83
184
### ** C++**
84
185
186
+ 差分数组:
187
+
85
188
``` cpp
86
189
class Solution {
87
190
public:
@@ -97,8 +200,60 @@ public:
97
200
};
98
201
```
99
202
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
+
100
253
### ** Go**
101
254
255
+ 差分数组:
256
+
102
257
``` go
103
258
func getModifiedArray (length int , updates [][]int ) []int {
104
259
delta := make ([]int , length)
@@ -115,6 +270,54 @@ func getModifiedArray(length int, updates [][]int) []int {
115
270
}
116
271
```
117
272
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
+
118
321
### ** ...**
119
322
120
323
```
0 commit comments