-
Notifications
You must be signed in to change notification settings - Fork 31
/
FullyRetroactiveQueue.go
349 lines (314 loc) · 6.93 KB
/
FullyRetroactiveQueue.go
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
// https://kopricky.github.io/code/Academic/fully_retroactive_deque.html
// 完全永続のように聞こえるが,
// !完全永続は過去のバージョンを更新した際にその更新を行わなかった世界と更新を行う世界を同時に保持するが,
// fully retroactive の方は更新を行った場合の 1つの世界のみを保持するという違いがある(更新は不可逆操作となる).
// 完全永続において更新は枝の分岐で表されるのに対して, fully retroactive において更新は過去の時刻での枝の挿入で表され,
// 常にバージョンは 1本のパスをなす.
// 每个操作都是均摊O(logn)的
// https://codeforces.com/gym/100451/problem/H
package main
import (
"fmt"
)
func main() {
qeue := NewRetroactiveDeque()
qeue.PushBack(1, 0)
fmt.Println(qeue.Size(0))
qeue.PushBack(2, 1)
fmt.Println(qeue.Size(1))
qeue.PushBack(3, 2)
fmt.Println(qeue.Query(0, 2))
fmt.Println(qeue.Size(3))
qeue.PopBack(1)
fmt.Println(qeue.Size(3))
}
type T = int
// 完全可追溯化双端队列.
// time: 0, 1, 2, 3, ...
type RetroactiveDeque struct {
lroot, rroot *_Node
}
func NewRetroactiveDeque() *RetroactiveDeque {
return &RetroactiveDeque{}
}
// 查询time时刻的双端队列的大小.
func (rd *RetroactiveDeque) Size(time int) int {
return rd._leftPos(time) + rd._rightPos(time)
}
// 查询time时刻的双端队列的左端元素.
func (rd *RetroactiveDeque) Front(time int) T {
return rd._queryImpl(0, time, false)
}
// 查询time时刻的双端队列的右端元素.
func (rd *RetroactiveDeque) Back(time int) T {
return rd._queryImpl(0, time, true)
}
func (rd *RetroactiveDeque) PushFront(data T, time int) {
rd._pushFrontImpl(data, time)
}
func (rd *RetroactiveDeque) PopFront(time int) {
rd._popFrontImpl(time)
}
func (rd *RetroactiveDeque) PushBack(data T, time int) {
rd._pushBackImpl(data, time)
}
func (rd *RetroactiveDeque) PopBack(time int) {
rd._popBackImpl(time)
}
// 查询time时刻的双端队列的第index个元素.
func (rd *RetroactiveDeque) Query(index int, time int) T {
return rd._queryImpl(index, time, false)
}
type _Node struct {
key int
val, sum, pmin, pmax int
data T
left, right, par *_Node
}
func _NewNode(key int, data T, val int) *_Node {
return &_Node{
key: key,
val: val,
sum: val,
pmin: min(val, 0),
pmax: max(val, 0),
data: data,
}
}
func (o *_Node) isRoot() bool {
return o.par == nil
}
func (o *_Node) eval() {
o.sum = 0
o.pmin = 0
o.pmax = 0
if o.left != nil {
o.sum += o.left.sum
o.pmin = min(o.pmin, o.left.pmin)
o.pmax = max(o.pmax, o.left.pmax)
}
o.sum += o.val
o.pmin = min(o.pmin, o.sum)
o.pmax = max(o.pmax, o.sum)
if o.right != nil {
o.pmin = min(o.pmin, o.sum+o.right.pmin)
o.pmax = max(o.pmax, o.sum+o.right.pmax)
o.sum += o.right.sum
}
}
func (o *_Node) rotate(right bool) {
p := o.par
g := p.par
if right {
if p.left = o.right; p.left != nil {
p.left.par = p
}
o.right = p
p.par = o
} else {
if p.right = o.left; p.right != nil {
p.right.par = p
}
o.left = p
p.par = o
}
p.eval()
o.eval()
o.par = g
if g == nil {
return
}
if g.left == p {
g.left = o
}
if g.right == p {
g.right = o
}
g.eval()
}
func _splay(u *_Node) *_Node {
if u == nil {
return nil
}
for !u.isRoot() {
p := u.par
gp := p.par
if p.isRoot() {
u.rotate(u == p.left)
} else {
flag := u == p.left
if flag == (p == gp.left) {
p.rotate(flag)
u.rotate(flag)
} else {
u.rotate(flag)
u.rotate(!flag)
}
}
}
return u
}
func _get(key int, root *_Node) (*_Node, bool) {
var cur, res *_Node
nx := root
for nx != nil {
cur = nx
if cur.key <= key {
nx = cur.right
res = cur
} else {
nx = cur.left
}
}
tmp := _splay(cur)
if res != nil {
return _splay(res), true
}
return tmp, false
}
func _insert(ver *_Node, root *_Node) *_Node {
if root == nil {
return ver
}
var cur *_Node
nx := root
for nx != nil {
cur = nx
if cur.key > ver.key {
nx = cur.left
} else {
nx = cur.right
}
}
if cur.key > ver.key {
cur.left = ver
ver.par = cur
} else {
cur.right = ver
ver.par = cur
}
cur.eval()
return _splay(ver)
}
func (rd *RetroactiveDeque) _leftPos(_time int) int {
first, second := _get(_time, rd.lroot)
rd.lroot = first
l := 0
if second {
if rd.lroot.left != nil {
l = rd.lroot.left.sum
}
l += rd.lroot.val
}
return l
}
func (rd *RetroactiveDeque) _rightPos(_time int) int {
first, second := _get(_time, rd.rroot)
rd.rroot = first
r := 0
if second {
if rd.rroot.left != nil {
r = rd.rroot.left.sum
}
r += rd.rroot.val
}
return r
}
func (rd *RetroactiveDeque) _pushFrontImpl(data T, _time int) {
newNode := _NewNode(_time, data, 1)
rd.lroot = _insert(newNode, rd.lroot)
}
func (rd *RetroactiveDeque) _popFrontImpl(_time int) {
var e T
newNode := _NewNode(_time, e, -1)
rd.lroot = _insert(newNode, rd.lroot)
}
func (rd *RetroactiveDeque) _pushBackImpl(data T, _time int) {
newNode := _NewNode(_time, data, 1)
rd.rroot = _insert(newNode, rd.rroot)
}
func (rd *RetroactiveDeque) _popBackImpl(_time int) {
var e T
newNode := _NewNode(_time, e, -1)
rd.rroot = _insert(newNode, rd.rroot)
}
func (rd *RetroactiveDeque) _find(index int, root *_Node) (*_Node, bool) {
if root == nil {
return nil, false
}
cur, nx := root, root.left
var res *_Node
if nx == nil && index == 0 {
return root, true
}
if nx == nil || index < nx.pmin || nx.pmax < index {
return root, false
}
for nx != nil {
cur = nx
curSum := 0
if cur.left != nil {
curSum += cur.left.sum
}
curSum += cur.val
if cur.right != nil {
if curSum+cur.right.pmin <= index && index <= curSum+cur.right.pmax {
nx = cur.right
index -= curSum
continue
}
}
if curSum == index {
res = cur
break
} else {
nx = cur.left
}
}
tmp := _splay(cur)
if res == nil {
return tmp, true
}
cur = _splay(res)
nx = cur.right
for nx != nil {
cur = nx
nx = cur.left
}
return _splay(cur), true
}
func (rd *RetroactiveDeque) _queryImpl(index int, _time int, back bool) T {
lpos := rd._leftPos(_time)
rpos := rd._rightPos(_time)
if back {
index = lpos + rpos - 1
}
lid := lpos - index
rid := index + 1 - lpos
lFirst, lSecond := rd._find(lid-1, rd.lroot)
rFirst, rSecond := rd._find(rid-1, rd.rroot)
rd.lroot = lFirst
rd.rroot = rFirst
if lSecond {
if rSecond {
if rd.lroot.key < rd.rroot.key {
return rd.rroot.data
}
return rd.lroot.data
}
return rd.lroot.data
}
return rd.rroot.data
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}