-
Notifications
You must be signed in to change notification settings - Fork 30
/
SegmentTree01.ts
293 lines (260 loc) · 9.24 KB
/
SegmentTree01.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
/* eslint-disable no-inner-declarations */
/* eslint-disable no-param-reassign */
// more details:https://github.dev/EndlessCheng/codeforces-go/tree/master/copypasta
/**
* 01线段树,支持 flip/indexOf/lastIndexOf/onesCount/kth,可用于模拟Bitset.
*/
class SegmentTree01 {
private readonly _n: number
private readonly _ones: Uint32Array
private readonly _lazyFlip: Uint8Array
/**
* little-endian
* @param bitsOrLength 0/1数组或者是长度.注意必须要是正整数.
* @example
* ```ts
* const seg01 = new SegmentTree01([1, 0, 1, 1, 0, 1])
* seg01.toString() // 101101
* ```
*/
constructor(bitsOrLength: ArrayLike<number> | number) {
if (typeof bitsOrLength === 'number') bitsOrLength = Array(bitsOrLength).fill(0)
if (bitsOrLength.length === 0) throw new Error('empty bits')
this._n = bitsOrLength.length
const log = 32 - Math.clz32(this._n - 1)
const size = 1 << log
this._ones = new Uint32Array(size << 1)
this._lazyFlip = new Uint8Array(size) // 叶子结点不需要更新lazy (composition)
this._build(1, 1, this._n, bitsOrLength)
}
/**
* 0 <= start <= end <= n
* 翻转[start,end)区间的bit.
*/
flip(start: number, end: number): void {
if (start < 0) start = 0
if (end > this._n) end = this._n
if (start >= end) return
start++
this._flip(1, start, end, 1, this._n)
}
/**
* 0 <= position <= n-1.
* @param searchDigit 0/1
* @param position 查找的起始位置, 0 <= position < n.
*/
indexOf(searchDigit: 0 | 1, position = 0): number {
position++
if (position > this._n) return -1
if (searchDigit === 0) {
const cand = this._indexofZero(1, position, 1, this._n)
return cand === -1 ? cand : cand - 1
}
const cand = this._indexofOne(1, position, 1, this._n)
return cand === -1 ? cand : cand - 1
}
/**
* 0 <= position <= n-1.
* @param searchDigit 0/1
* @param position 查找的起始位置, 0 <= position < n.
*/
lastIndexOf(searchDigit: 0 | 1, position = this._n - 1): number {
position++
if (position < 1) return -1
if (searchDigit === 0) {
const cand = this._lastIndexOfZero(1, position, 1, this._n)
return cand === -1 ? cand : cand - 1
}
const cand = this._lastIndexOfOne(1, position, 1, this._n)
return cand === -1 ? cand : cand - 1
}
/**
* 0 <= left <= right <= n
* 返回[left,right)区间内1的个数.
*/
onesCount(start: number, end: number): number {
if (start < 0) start = 0
if (end > this._n) end = this._n
if (start >= end) return 0
start++
return this._onesCount(1, start, end, 1, this._n)
}
/**
* 树上二分查询第k个0/1的位置.如果不存在第k个0/1,返回-1.
* !k >= 1
* @returns -1<=pos<n.
*/
kth(searchDigit: 0 | 1, k: number): number {
if (searchDigit === 0) {
if (k > this._n - this._ones[1]) return -1
return this._kthZero(1, k, 1, this._n) - 1
}
if (k > this._ones[1]) return -1
return this._kthOne(1, k, 1, this._n) - 1
}
toString(): string {
const sb: string[] = []
this._toString(1, 1, this._n, sb)
return sb.join('')
}
private _flip(root: number, L: number, R: number, l: number, r: number): void {
if (L <= l && r <= R) {
this._propagateFlip(root, l, r)
return
}
this._pushDown(root, l, r)
const mid = (l + r) >>> 1
if (L <= mid) this._flip(root << 1, L, R, l, mid)
if (mid < R) this._flip((root << 1) | 1, L, R, mid + 1, r)
this._pushUp(root)
}
private _indexofOne(root: number, position: number, left: number, right: number): number {
if (left === right) {
if (this._ones[root] > 0) return left
return -1
}
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
if (position <= mid && this._ones[root << 1] > 0) {
const leftPos = this._indexofOne(root << 1, position, left, mid)
if (leftPos > 0) return leftPos
}
return this._indexofOne((root << 1) | 1, position, mid + 1, right)
}
private _indexofZero(root: number, position: number, left: number, right: number): number {
if (left === right) {
if (this._ones[root] === 0) return left
return -1
}
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
if (position <= mid && this._ones[root << 1] < mid - left + 1) {
const leftPos = this._indexofZero(root << 1, position, left, mid)
if (leftPos > 0) return leftPos
}
return this._indexofZero((root << 1) | 1, position, mid + 1, right)
}
private _lastIndexOfOne(root: number, position: number, left: number, right: number): number {
if (left === right) {
if (this._ones[root] > 0) return left
return -1
}
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
if (position > mid && this._ones[(root << 1) | 1] > 0) {
const rightPos = this._lastIndexOfOne((root << 1) | 1, position, mid + 1, right)
if (rightPos > 0) return rightPos
}
return this._lastIndexOfOne(root << 1, position, left, mid)
}
private _lastIndexOfZero(root: number, position: number, left: number, right: number): number {
if (left === right) {
if (this._ones[root] === 0) return left
return -1
}
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
if (position > mid && this._ones[(root << 1) | 1] < right - mid) {
const rightPos = this._lastIndexOfZero((root << 1) | 1, position, mid + 1, right)
if (rightPos > 0) return rightPos
}
return this._lastIndexOfZero(root << 1, position, left, mid)
}
private _onesCount(root: number, L: number, R: number, l: number, r: number): number {
if (L <= l && r <= R) return this._ones[root]
this._pushDown(root, l, r)
const mid = (l + r) >>> 1
let res = 0
if (L <= mid) res += this._onesCount(root << 1, L, R, l, mid)
if (mid < R) res += this._onesCount((root << 1) | 1, L, R, mid + 1, r)
return res
}
private _kthOne(root: number, k: number, left: number, right: number): number {
if (left === right) return left
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
if (this._ones[root << 1] >= k) return this._kthOne(root << 1, k, left, mid)
return this._kthOne((root << 1) | 1, k - this._ones[root << 1], mid + 1, right)
}
private _kthZero(root: number, k: number, left: number, right: number): number {
if (left === right) return left
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
const leftZero = mid - left + 1 - this._ones[root << 1]
if (leftZero >= k) return this._kthZero(root << 1, k, left, mid)
return this._kthZero((root << 1) | 1, k - leftZero, mid + 1, right)
}
private _toString(root: number, left: number, right: number, sb: string[]): void {
if (left === right) {
sb.push(this._ones[root] === 1 ? '1' : '0')
return
}
this._pushDown(root, left, right)
const mid = (left + right) >>> 1
this._toString(root << 1, left, mid, sb)
this._toString((root << 1) | 1, mid + 1, right, sb)
}
private _build(root: number, left: number, right: number, leaves: ArrayLike<number>): void {
if (left === right) {
this._ones[root] = leaves[left - 1]
return
}
const mid = (left + right) >>> 1
this._build(root << 1, left, mid, leaves)
this._build((root << 1) | 1, mid + 1, right, leaves)
this._pushUp(root)
}
private _pushUp(root: number): void {
this._ones[root] = this._ones[root << 1] + this._ones[(root << 1) | 1]
}
private _pushDown(root: number, left: number, right: number): void {
if (this._lazyFlip[root] !== 0) {
const mid = (left + right) >>> 1
this._propagateFlip(root << 1, left, mid)
this._propagateFlip((root << 1) | 1, mid + 1, right)
this._lazyFlip[root] = 0
}
}
private _propagateFlip(root: number, left: number, right: number): void {
this._ones[root] = right - left + 1 - this._ones[root]
if (root < this._lazyFlip.length) {
this._lazyFlip[root] ^= 1
}
}
}
if (require.main === module) {
// 01线段树模拟位集
// https://leetcode.cn/problems/design-bitset/
class Bitset {
private readonly size: number
private readonly tree01: SegmentTree01
constructor(size: number) {
this.size = size
this.tree01 = new SegmentTree01(new Uint8Array(size))
}
fix(idx: number): void {
if (this.tree01.onesCount(idx, idx + 1) === 1) return
this.tree01.flip(idx, idx + 1)
}
unfix(idx: number): void {
if (this.tree01.onesCount(idx, idx + 1) === 0) return
this.tree01.flip(idx, idx + 1)
}
flip(): void {
this.tree01.flip(0, this.size)
}
all(): boolean {
return this.tree01.onesCount(0, this.size) === this.size
}
one(): boolean {
return this.tree01.onesCount(0, this.size) > 0
}
count(): number {
return this.tree01.onesCount(0, this.size)
}
toString(): string {
return this.tree01.toString()
}
}
}
export { SegmentTree01 }