data-structure-typed 2.6.0 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/package.json +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -29,7 +29,7 @@ import { ERR, raise } from '../../common';
|
|
|
29
29
|
*/
|
|
30
30
|
export class BinaryIndexedTree implements Iterable<number> {
|
|
31
31
|
protected readonly _size: number;
|
|
32
|
-
protected _tree: number[];
|
|
32
|
+
protected _tree: number[]; // 1-indexed BIT array
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Construct a BIT of given size (all zeros), or from an initial values array.
|
|
@@ -53,90 +53,18 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
53
53
|
|
|
54
54
|
// ─── Core operations ──────────────────────────────────────────
|
|
55
55
|
|
|
56
|
+
get size(): number {
|
|
57
|
+
return this._size;
|
|
58
|
+
}
|
|
59
|
+
|
|
56
60
|
/**
|
|
57
61
|
* Point update: add delta to the value at index (0-based).
|
|
58
62
|
* Time: O(log n)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
* @example
|
|
136
|
-
* // Add delta at index
|
|
137
|
-
* const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
138
|
-
* bit.update(2, 7);
|
|
139
|
-
* console.log(bit.get(2)); // 10;
|
|
63
|
+
* @example
|
|
64
|
+
* // Add delta at index
|
|
65
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
66
|
+
* bit.update(2, 7);
|
|
67
|
+
* console.log(bit.get(2)); // 10;
|
|
140
68
|
*/
|
|
141
69
|
update(index: number, delta: number): void {
|
|
142
70
|
this._checkIndex(index);
|
|
@@ -146,88 +74,11 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
146
74
|
/**
|
|
147
75
|
* Point set: set the value at index to an absolute value (0-based).
|
|
148
76
|
* Time: O(log n)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
* @example
|
|
227
|
-
* // Set value at index
|
|
228
|
-
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
229
|
-
* bit.set(1, 10);
|
|
230
|
-
* console.log(bit.get(1)); // 10;
|
|
77
|
+
* @example
|
|
78
|
+
* // Set value at index
|
|
79
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
80
|
+
* bit.set(1, 10);
|
|
81
|
+
* console.log(bit.get(1)); // 10;
|
|
231
82
|
*/
|
|
232
83
|
set(index: number, value: number): void {
|
|
233
84
|
this._checkIndex(index);
|
|
@@ -238,87 +89,11 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
238
89
|
/**
|
|
239
90
|
* Get the point value at index (0-based).
|
|
240
91
|
* Time: O(log n)
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
* @example
|
|
318
|
-
* // Get value at index
|
|
319
|
-
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
320
|
-
* console.log(bit.get(0)); // 1;
|
|
321
|
-
* console.log(bit.get(2)); // 3;
|
|
92
|
+
* @example
|
|
93
|
+
* // Get value at index
|
|
94
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
95
|
+
* console.log(bit.get(0)); // 1;
|
|
96
|
+
* console.log(bit.get(2)); // 3;
|
|
322
97
|
*/
|
|
323
98
|
get(index: number): number {
|
|
324
99
|
this._checkIndex(index);
|
|
@@ -328,176 +103,25 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
328
103
|
/**
|
|
329
104
|
* Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
|
|
330
105
|
* Time: O(log n)
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
* @example
|
|
409
|
-
* // Prefix sum
|
|
410
|
-
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
411
|
-
* console.log(bit.query(2)); // 6;
|
|
106
|
+
* @example
|
|
107
|
+
* // Prefix sum
|
|
108
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
109
|
+
* console.log(bit.query(2)); // 6;
|
|
412
110
|
*/
|
|
413
111
|
query(index: number): number {
|
|
414
112
|
this._checkIndex(index);
|
|
415
113
|
return this._prefixSum(index + 1);
|
|
416
114
|
}
|
|
417
115
|
|
|
116
|
+
// ─── Binary search ───────────────────────────────────────────
|
|
117
|
+
|
|
418
118
|
/**
|
|
419
119
|
* Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
|
|
420
120
|
* Time: O(log n)
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
* @example
|
|
498
|
-
* // Range sum
|
|
499
|
-
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
500
|
-
* console.log(bit.queryRange(1, 2)); // 5;
|
|
121
|
+
* @example
|
|
122
|
+
* // Range sum
|
|
123
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
124
|
+
* console.log(bit.queryRange(1, 2)); // 5;
|
|
501
125
|
*/
|
|
502
126
|
queryRange(start: number, end: number): number {
|
|
503
127
|
this._checkIndex(start);
|
|
@@ -507,95 +131,16 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
507
131
|
return this._prefixSum(end + 1) - this._prefixSum(start);
|
|
508
132
|
}
|
|
509
133
|
|
|
510
|
-
// ─── Binary search ───────────────────────────────────────────
|
|
511
|
-
|
|
512
134
|
/**
|
|
513
135
|
* Find the smallest index i such that prefix sum [0..i] >= sum.
|
|
514
136
|
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
515
137
|
* Returns size if no such index exists.
|
|
516
138
|
* Time: O(log n)
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
* @example
|
|
595
|
-
* // Find index with prefix sum ≥ target
|
|
596
|
-
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
597
|
-
* const idx = bit.lowerBound(4);
|
|
598
|
-
* console.log(idx); // >= 0;
|
|
139
|
+
* @example
|
|
140
|
+
* // Find index with prefix sum ≥ target
|
|
141
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
142
|
+
* const idx = bit.lowerBound(4);
|
|
143
|
+
* console.log(idx); // >= 0;
|
|
599
144
|
*/
|
|
600
145
|
lowerBound(sum: number): number {
|
|
601
146
|
let pos = 0;
|
|
@@ -613,49 +158,18 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
613
158
|
return pos; // 0-based
|
|
614
159
|
}
|
|
615
160
|
|
|
161
|
+
// ─── Standard interface ──────────────────────────────────────
|
|
162
|
+
|
|
616
163
|
/**
|
|
617
164
|
* Find the smallest index i such that prefix sum [0..i] > sum.
|
|
618
165
|
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
619
166
|
* Returns size if no such index exists.
|
|
620
167
|
* Time: O(log n)
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
* @example
|
|
655
|
-
* // Find index with prefix sum > target
|
|
656
|
-
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
657
|
-
* const idx = bit.upperBound(4);
|
|
658
|
-
* console.log(idx); // >= 0;
|
|
168
|
+
* @example
|
|
169
|
+
* // Find index with prefix sum > target
|
|
170
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
171
|
+
* const idx = bit.upperBound(4);
|
|
172
|
+
* console.log(idx); // >= 0;
|
|
659
173
|
*/
|
|
660
174
|
upperBound(sum: number): number {
|
|
661
175
|
let pos = 0;
|
|
@@ -673,12 +187,6 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
673
187
|
return pos; // 0-based
|
|
674
188
|
}
|
|
675
189
|
|
|
676
|
-
// ─── Standard interface ──────────────────────────────────────
|
|
677
|
-
|
|
678
|
-
get size(): number {
|
|
679
|
-
return this._size;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
190
|
isEmpty(): boolean {
|
|
683
191
|
return this._size === 0;
|
|
684
192
|
}
|
|
@@ -694,43 +202,10 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
694
202
|
/**
|
|
695
203
|
* Returns the point values as a plain array.
|
|
696
204
|
* Time: O(n log n)
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
* @example
|
|
731
|
-
* // Convert to array
|
|
732
|
-
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
733
|
-
* console.log(bit.toArray()); // [1, 2, 3];
|
|
205
|
+
* @example
|
|
206
|
+
* // Convert to array
|
|
207
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
208
|
+
* console.log(bit.toArray()); // [1, 2, 3];
|
|
734
209
|
*/
|
|
735
210
|
toArray(): number[] {
|
|
736
211
|
const result: number[] = [];
|
|
@@ -794,7 +269,7 @@ export class BinaryIndexedTree implements Iterable<number> {
|
|
|
794
269
|
/** 1-based point query: get exact value at pos. */
|
|
795
270
|
protected _pointQuery(pos: number): number {
|
|
796
271
|
let val = this._tree[pos];
|
|
797
|
-
const lca = pos - (pos & -pos);
|
|
272
|
+
const lca = pos - (pos & -pos); // parent in prefix-sum sense
|
|
798
273
|
pos--;
|
|
799
274
|
while (pos > lca) {
|
|
800
275
|
val -= this._tree[pos];
|