-
Notifications
You must be signed in to change notification settings - Fork 30
/
SortedListWithSum.ts
404 lines (364 loc) · 12.1 KB
/
SortedListWithSum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
136
137
138
139
140
141
142
143
144
145
146
147
148
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
318
319
320
321
322
323
324
325
326
327
328
329
330
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
/* eslint-disable brace-style */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-inner-declarations */
/* eslint-disable generator-star-spacing */
/* eslint-disable prefer-destructuring */
// class SortedListWithSum
import { SortedListFast } from './SortedListFast'
interface IOptions<V> {
values?: Iterable<V>
compareFn?: (a: V, b: V) => number
abelGroup?: {
e: () => V
op: (a: V, b: V) => V
inv: (a: V) => V
}
}
interface ISortedListFastWithSum<V> {
sumSlice(start?: number, end?: number): V
sumRange(min: V, max: V): V
sumAll(): V
}
/**
* 支持区间求和的有序列表.
* {@link sumSlice} 和 {@link sumRange} 的时间复杂度为 `O(sqrt(n))`.
* {@link sumAll} 的时间复杂度为 `O(1)`.
*/
class SortedListFastWithSum<V = number> extends SortedListFast<V> implements ISortedListFastWithSum<V> {
private readonly _e: () => V
private readonly _op: (a: V, b: V) => V
private readonly _inv: (a: V) => V
private _allSum: V
private _sums: V[] = []
constructor(options?: IOptions<V>) {
super()
const {
values = [],
compareFn = (a: any, b: any) => a - b,
abelGroup = {
e: () => 0 as any,
op: (a: any, b: any) => a + b,
inv: (a: any) => -a as any
}
} = options ?? {}
this._e = abelGroup.e
this._op = abelGroup.op
this._inv = abelGroup.inv
this._allSum = this._e()
this._reBuild([...values], compareFn)
}
/**
* 返回区间 `[start, end)` 的和.
*/
sumSlice(start = 0, end = this.length): V {
if (start < 0) start += this._len
if (start < 0) start = 0
if (end < 0) end += this._len
if (end > this._len) end = this._len
if (start >= end) return this._e()
const { pos: bid1, index: startIndex1 } = this._findKth(start)
const { pos: bid2, index: startIndex2 } = this._findKth(end - 1)
start = startIndex1
end = startIndex2 + 1
let res = this._e()
if (bid1 === bid2) {
const block = this._blocks[bid1]
for (let i = start; i < end; i++) res = this._op(res, block[i])
} else {
if (start < this._blocks[bid1].length) {
const block1 = this._blocks[bid1]
for (let i = start; i < block1.length; i++) res = this._op(res, block1[i])
}
for (let i = bid1 + 1; i < bid2; i++) res = this._op(res, this._sums[i])
if (bid2 < this._blocks.length && end > 0) {
const block2 = this._blocks[bid2]
let tmp = this._e()
for (let i = 0; i < end; i++) tmp = this._op(tmp, block2[i])
res = this._op(res, tmp)
}
}
return res
}
/**
* 返回范围 `[min, max]` 的和.
*/
sumRange(min: V, max: V): V {
if (this._compareFn(min, max) > 0) return this._e()
let res = this._e()
let { pos, index: start } = this._locLeft(min)
for (let i = pos; i < this._blocks.length; i++) {
const block = this._blocks[i]
if (this._compareFn(max, block[0]) < 0) break
if (start === 0 && this._compareFn(block[block.length - 1], max) <= 0) {
res = this._op(res, this._sums[i])
} else {
for (let j = start; j < block.length; j++) {
const cur = block[j]
if (this._compareFn(cur, max) > 0) break
res = this._op(res, cur)
}
}
start = 0
}
return res
}
sumAll(): V {
return this._allSum
}
// eslint-disable-next-line class-methods-use-this
protected override _init(): void {
return undefined
}
protected override _reBuild(data: V[], compareFn: (a: V, b: V) => number): void {
data.sort(compareFn)
const n = data.length
const blocks = []
const sums = []
let allSum = this._e()
for (let i = 0; i < n; i += SortedListFast._LOAD) {
const newBlock = data.slice(i, i + SortedListFast._LOAD)
blocks.push(newBlock)
let cur = this._e()
for (let j = 0; j < newBlock.length; j++) cur = this._op(cur, newBlock[j])
sums.push(cur)
allSum = this._op(allSum, cur)
}
const mins = Array(blocks.length)
for (let i = 0; i < blocks.length; i++) {
const cur = blocks[i]
mins[i] = cur[0]
}
this._compareFn = compareFn
this._len = n
this._blocks = blocks
this._mins = mins
this._tree = []
this._shouldRebuildTree = true
this._sums = sums
this._allSum = allSum
}
override add(value: V): this {
const { _blocks, _mins, _sums } = this
this._len++
this._allSum = this._op(this._allSum, value)
if (!_blocks.length) {
_blocks.push([value])
_mins.push(value)
_sums.push(value)
this._shouldRebuildTree = true
return this
}
const load = SortedListFast._LOAD
const { pos, index } = this._locRight(value)
this._updateTree(pos, 1)
const block = _blocks[pos]
block.splice(index, 0, value)
_mins[pos] = block[0]
_sums[pos] = this._op(_sums[pos], value)
// !-> [x]*load + [x]*(block.length - load)
if (load + load < block.length) {
const oldSum = _sums[pos]
_blocks.splice(pos + 1, 0, block.slice(load))
_mins.splice(pos + 1, 0, block[load])
block.splice(load)
this._shouldRebuildTree = true
this._rebuildSum(pos)
this._sums.splice(pos + 1, 0, this._op(oldSum, this._inv(this._sums[pos])))
}
return this
}
override enumerate(start: number, end: number, f?: (value: V) => void, erase?: boolean): void {
if (start < 0) start = 0
if (end > this._len) end = this._len
if (start >= end) return
let { pos, index: startIndex } = this._findKth(start)
let count = end - start
for (; count && pos < this._blocks.length; pos++) {
const block = this._blocks[pos]
const endIndex = Math.min(block.length, startIndex + count)
if (f) {
for (let j = startIndex; j < endIndex; j++) f(block[j])
}
const deleted = endIndex - startIndex
if (erase) {
if (deleted === block.length) {
// !delete block
this._blocks.splice(pos, 1)
this._mins.splice(pos, 1)
this._allSum = this._op(this._allSum, this._inv(this._sums[pos]))
this._sums.splice(pos, 1)
this._shouldRebuildTree = true
pos--
} else {
// !delete [index, end)
for (let i = startIndex; i < endIndex; i++) {
const inv = this._inv(block[i])
this._allSum = this._op(this._allSum, inv)
this._sums[pos] = this._op(this._sums[pos], inv)
}
this._updateTree(pos, -deleted)
block.splice(startIndex, deleted)
this._mins[pos] = block[0]
}
this._len -= deleted
}
count -= deleted
startIndex = 0
}
}
override clear(): void {
super.clear()
this._sums = []
this._allSum = this._e()
}
protected override _delete(pos: number, index: number): void {
const { _blocks, _mins, _sums } = this
// !delete element
this._len--
this._updateTree(pos, -1)
const block = _blocks[pos]
const deleted = block[index]
block.splice(index, 1)
this._allSum = this._op(this._allSum, this._inv(deleted))
if (block.length) {
_mins[pos] = block[0]
_sums[pos] = this._op(_sums[pos], this._inv(deleted))
return
}
// !delete block
_blocks.splice(pos, 1)
_mins.splice(pos, 1)
_sums.splice(pos, 1)
this._shouldRebuildTree = true
}
private _rebuildSum(pos: number): void {
let cur = this._e()
const block = this._blocks[pos]
for (let i = 0; i < block.length; i++) cur = this._op(cur, block[i])
this._sums[pos] = cur
}
}
export { SortedListFastWithSum }
if (require.main === module) {
// 可用于维护topKSum
const sl = new SortedListFastWithSum<number>({ values: [1] })
const nums = [11, 2, 3, 4, 5, 6, 7, 8, 9]
for (const num of nums) sl.add(num)
console.log(sl.sumSlice(0, 3))
sl.erase(0, 3)
console.log(sl.sumSlice(0, 3))
console.log(sl.sumRange(0, 1000))
console.log('-'.repeat(20))
// https://leetcode.cn/problems/minimum-difference-in-sums-after-removal-of-elements/
function minimumDifference(nums: number[]): number {
const n = nums.length / 3
const pre = new SortedListFastWithSum({ values: nums.slice(0, n) })
const suf = new SortedListFastWithSum({ values: nums.slice(n) })
let res = pre.sumSlice(0, n) - suf.sumSlice(suf.length - n, suf.length)
for (let i = n; i < 2 * n; i++) {
pre.add(nums[i])
suf.discard(nums[i])
res = Math.min(res, pre.sumSlice(0, n) - suf.sumSlice(suf.length - n, suf.length))
}
return res
}
// 1825. 求出 MK 平均值
// https://leetcode.cn/problems/finding-mk-average/
class MKAverage {
private readonly _m: number
private readonly _k: number
private readonly _queue: number[] = []
private _head = 0
private _lastK = new SortedListFastWithSum<number>()
constructor(m: number, k: number) {
this._m = m
this._k = k
}
addElement(num: number): void {
this._queue.push(num)
if (this._queue.length === this._m) {
this._lastK = new SortedListFastWithSum({ values: this._queue.slice(-this._m) })
return
}
if (this._queue.length > this._m) {
this._lastK.add(num)
this._lastK.discard(this._queue[this._head])
this._head++
}
}
calculateMKAverage(): number {
if (this._queue.length < this._m) return -1
const midSum = this._lastK.sumSlice(this._k, -this._k)
return Math.floor(midSum / (this._m - 2 * this._k))
}
}
testSumSlice()
testSumRange()
function testSumSlice() {
const sl = new SortedListFastWithSum<number>()
let sortedNums: number[] = []
for (let i = 0; i < 10000; i++) {
const num = Math.floor(Math.random() * 10000)
sl.add(num)
sortedNums.push(num)
sortedNums.sort((a, b) => a - b)
const start = ~~(sl.length * Math.random())
const end = ~~(sl.length * Math.random())
const res1 = sl.sumSlice(start, end)
const res2 = sortedNums.slice(start, end).reduce((a, b) => a + b, 0)
if (res1 !== res2) {
console.log(res1, res2)
console.log(sl.slice(0, 10))
console.log(sortedNums.slice(0, 10))
throw new Error()
}
// discard/add
const willDiscard = Math.random() > 0.5
if (willDiscard) {
const discard = sortedNums[~~(Math.random() * sortedNums.length)]
sl.discard(discard)
const index = sortedNums.findIndex(num => num === discard)
sortedNums.splice(index, 1)
} else {
const add = Math.floor(Math.random() * 100)
sl.add(add)
sortedNums.push(add)
sortedNums.sort((a, b) => a - b)
}
}
}
function testSumRange() {
// SortedListFastWithSum.setLoadFactor(5)
const sl = new SortedListFastWithSum<number>()
const sortedNums = []
for (let i = 0; i < 10000; i++) {
const num = Math.floor(Math.random() * 10000)
sl.add(num)
sortedNums.push(num)
sortedNums.sort((a, b) => a - b)
const min = ~~(Math.random() * 10000)
const max = ~~(Math.random() * 10000)
const res1 = sl.sumRange(min, max)
const res2 = sortedNums.filter(num => num >= min && num <= max).reduce((a, b) => a + b, 0)
if (res1 !== res2) {
console.log(res1, res2)
console.log(sl.slice(0, 10))
console.log(sortedNums.slice(0, 10))
throw new Error()
}
const willDiscard = Math.random() > 0.5
if (willDiscard) {
const randint = sortedNums[~~(Math.random() * sortedNums.length)]
const index = sortedNums.findIndex(num => num === randint)
sl.discard(randint)
sortedNums.splice(index, 1)
} else {
const randint = Math.floor(Math.random() * 100)
sl.add(randint)
sortedNums.push(randint)
sortedNums.sort((a, b) => a - b)
}
}
}
console.log('ok')
}