-
Notifications
You must be signed in to change notification settings - Fork 31
/
SegmentTreeRangeUpdateRangeQuery.ts
284 lines (257 loc) · 8.45 KB
/
SegmentTreeRangeUpdateRangeQuery.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
/* eslint-disable no-cond-assign */
/* eslint-disable no-param-reassign */
// !区间修改 + 区间查询
class SegmentTreeRangeUpdateRangeQuery<E = number, Id = number> {
private readonly _n: number
private readonly _size: number
private readonly _height: number
private readonly _data: E[]
private readonly _lazy: Id[]
private readonly _e: () => E
private readonly _id: () => Id
private readonly _op: (a: E, b: E) => E
private readonly _mapping: (id: Id, data: E) => E
private readonly _composition: (id1: Id, id2: Id) => Id
private readonly _equalsToId: (o: Id) => boolean
/**
* 区间修改区间查询的懒标记线段树.维护幺半群.
* @param nOrLeaves 大小或叶子节点的值.
* @param operations 线段树的操作.
*/
constructor(
nOrLeaves: number | ArrayLike<E>,
operations: {
/**
* 线段树维护的值的幺元.
*/
e: () => E
/**
* 更新操作/懒标记的幺元.
*/
id: () => Id
/**
* 合并左右区间的值.
*/
op: (e1: E, e2: E) => E
/**
* 父结点的懒标记更新子结点的值.
*/
mapping: (lazy: Id, data: E) => E
/**
* 父结点的懒标记更新子结点的懒标记(合并).
*/
composition: (f: Id, g: Id) => Id
/**
* 判断两个懒标记是否相等.比较方式默认为`===`.
*/
equalsId?: (id1: Id, id2: Id) => boolean
} & ThisType<void>
) {
const n = typeof nOrLeaves === 'number' ? nOrLeaves : nOrLeaves.length
const { e, id, op, mapping, composition, equalsId } = operations
if (!equalsId && !SegmentTreeRangeUpdateRangeQuery._isPrimitive(id())) {
throw new Error('equalsId must be provided when id() returns an non-primitive value')
}
let size = 1
let height = 0
while (size < n) {
size <<= 1
height++
}
const data = Array(size << 1)
for (let i = 0; i < data.length; i++) data[i] = e()
const lazy = Array(size)
for (let i = 0; i < lazy.length; i++) lazy[i] = id()
this._n = n
this._size = size
this._height = height
this._data = data
this._lazy = lazy
this._e = e
this._id = id
this._op = op
this._mapping = mapping
this._composition = composition
const identity = id()
this._equalsToId = equalsId ? (o: Id) => equalsId(o, identity) : (o: Id) => o === identity
if (typeof nOrLeaves !== 'number') this._build(nOrLeaves)
}
set(index: number, value: E): void {
if (index < 0 || index >= this._n) return
index += this._size
for (let i = this._height; i > 0; i--) this._pushDown(index >> i)
this._data[index] = value
for (let i = 1; i <= this._height; i++) this._pushUp(index >> i)
}
get(index: number): E {
if (index < 0 || index >= this._n) return this._e()
index += this._size
for (let i = this._height; i > 0; i--) this._pushDown(index >> i)
return this._data[index]
}
/**
* 区间`[start,end)`的值与`lazy`进行作用.
* 0 <= start <= end <= n.
*/
update(start: number, end: number, lazy: Id): void {
if (start < 0) start = 0
if (end > this._n) end = this._n
if (start >= end) return
start += this._size
end += this._size
for (let i = this._height; i > 0; i--) {
if ((start >> i) << i !== start) this._pushDown(start >> i)
if ((end >> i) << i !== end) this._pushDown((end - 1) >> i)
}
let start2 = start
let end2 = end
for (; start < end; start >>= 1, end >>= 1) {
if (start & 1) this._propagate(start++, lazy)
if (end & 1) this._propagate(--end, lazy)
}
start = start2
end = end2
for (let i = 1; i <= this._height; i++) {
if ((start >> i) << i !== start) this._pushUp(start >> i)
if ((end >> i) << i !== end) this._pushUp((end - 1) >> i)
}
}
/**
* 查询区间`[start,end)`的聚合值.
* 0 <= start <= end <= n.
*/
query(start: number, end: number): E {
if (start < 0) start = 0
if (end > this._n) end = this._n
if (start >= end) return this._e()
start += this._size
end += this._size
for (let i = this._height; i > 0; i--) {
if ((start >> i) << i !== start) this._pushDown(start >> i)
if ((end >> i) << i !== end) this._pushDown((end - 1) >> i)
}
let leftRes = this._e()
let rightRes = this._e()
for (; start < end; start >>= 1, end >>= 1) {
if (start & 1) leftRes = this._op(leftRes, this._data[start++])
if (end & 1) rightRes = this._op(this._data[--end], rightRes)
}
return this._op(leftRes, rightRes)
}
queryAll(): E {
return this._data[1]
}
/**
* 树上二分查询最大的`end`使得`[start,end)`内的值满足`predicate`.
* @alias findFirst
*/
maxRight(start: number, predicate: (value: E) => boolean): number {
if (start < 0) start = 0
if (start >= this._n) return this._n
start += this._size
for (let i = this._height; i > 0; i--) this._pushDown(start >> i)
let res = this._e()
while (true) {
while (!(start & 1)) start >>= 1
if (!predicate(this._op(res, this._data[start]))) {
while (start < this._size) {
this._pushDown(start)
start <<= 1
const tmp = this._op(res, this._data[start])
if (predicate(tmp)) {
res = tmp
start++
}
}
return start - this._size
}
res = this._op(res, this._data[start])
start++
if ((start & -start) === start) break
}
return this._n
}
/**
* 树上二分查询最小的`start`使得`[start,end)`内的值满足`predicate`
* @alias findLast
*/
minLeft(end: number, predicate: (value: E) => boolean): number {
if (end > this._n) end = this._n
if (end <= 0) return 0
end += this._size
for (let i = this._height; i > 0; i--) this._pushDown((end - 1) >> i)
let res = this._e()
while (true) {
end--
while (end > 1 && end & 1) end >>= 1
if (!predicate(this._op(this._data[end], res))) {
while (end < this._size) {
this._pushDown(end)
end = (end << 1) | 1
const tmp = this._op(this._data[end], res)
if (predicate(tmp)) {
res = tmp
end--
}
}
return end + 1 - this._size
}
res = this._op(this._data[end], res)
if ((end & -end) === end) break
}
return 0
}
getAll(): E[] {
for (let i = 1; i < this._size; i++) {
this._pushDown(i)
}
return this._data.slice(this._size, this._size + this._n)
}
toString(): string {
const sb: string[] = []
sb.push('SegmentTreeRangeUpdateRangeQuery(')
for (let i = 0; i < this._n; i++) {
if (i) sb.push(', ')
sb.push(JSON.stringify(this.get(i)))
}
sb.push(')')
return sb.join('')
}
private _build(leaves: ArrayLike<E>): void {
if (leaves.length !== this._n) throw new RangeError(`length must be equal to ${this._n}`)
for (let i = 0; i < this._n; i++) this._data[this._size + i] = leaves[i]
for (let i = this._size - 1; i > 0; i--) this._pushUp(i)
}
private _pushUp(index: number): void {
this._data[index] = this._op(this._data[index << 1], this._data[(index << 1) | 1])
}
private _pushDown(index: number): void {
const lazy = this._lazy[index]
if (this._equalsToId(lazy)) return
this._propagate(index << 1, lazy)
this._propagate((index << 1) | 1, lazy)
this._lazy[index] = this._id()
}
private _propagate(index: number, lazy: Id): void {
this._data[index] = this._mapping(lazy, this._data[index])
if (index < this._size) this._lazy[index] = this._composition(lazy, this._lazy[index])
}
private static _isPrimitive(
o: unknown
): o is number | string | boolean | symbol | bigint | null | undefined {
return o === null || (typeof o !== 'object' && typeof o !== 'function')
}
}
export { SegmentTreeRangeUpdateRangeQuery }
if (require.main === module) {
const seg = new SegmentTreeRangeUpdateRangeQuery(10, {
e: () => 0,
id: () => 0,
op: (a, b) => Math.max(a, b),
mapping: (lazy, data) => Math.max(lazy, data),
composition: (f, g) => Math.max(f, g)
})
console.log(seg.getAll())
seg.update(0, 10, 1)
console.log(seg.getAll())
}