forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay.go
282 lines (252 loc) · 6.45 KB
/
splay.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
package copypasta
import (
. "fmt"
"strings"
)
/*
伸展树 splay
https://oi-wiki.org/ds/splay/
https://www.cnblogs.com/cjyyb/p/7499020.html
普通平衡树 https://www.luogu.com.cn/problem/P3369 https://www.luogu.com.cn/problem/P6136
文艺平衡树 https://www.luogu.com.cn/problem/P3391
有关 Link Cut Tree 的部分见 link_cut_tree.go
*/
// 下面的代码参考了刘汝佳的实现,即不使用父节点指针的方案
// 若想看使用父节点指针的方案,可以见 link_cut_tree.go
type spKeyType int
type spValueType int
type spNode struct {
lr [2]*spNode
sz int
key spKeyType
val spValueType
}
// 设置如下返回值是为了方便使用 spNode 中的 lr 数组
func (o *spNode) cmpKth(k int) int {
switch d := k - o.lr[0].size() - 1; {
case d < 0:
return 0 // 左儿子
case d > 0:
return 1 // 右儿子
default:
return -1
}
}
func (o *spNode) size() int {
if o != nil {
return o.sz
}
return 0
}
// 对于取名叫 maintain 还是 pushUp,由于操作的对象是当前节点,个人认为取名 maintain 更为准确
func (o *spNode) maintain() {
o.sz = 1 + o.lr[0].size() + o.lr[1].size()
}
func (o *spNode) pushDown() {
// custom ...
}
// 旋转,并维护子树大小
// d=0:左旋,返回 o 的右儿子
// d=1:右旋,返回 o 的左儿子
func (o *spNode) rotate(d int) *spNode {
x := o.lr[d^1]
o.lr[d^1] = x.lr[d]
x.lr[d] = o
// x.sz = o.sz; o.maintain()
o.maintain()
x.maintain()
return x
}
// 将子树 o 的第 k 小节点伸展到 o,返回该节点
// k 必须为正
func (o *spNode) splay(k int) (kth *spNode) {
o.pushDown()
d := o.cmpKth(k)
if d < 0 {
return o
}
k -= d * (o.lr[0].size() + 1)
c := o.lr[d]
c.pushDown()
if d2 := c.cmpKth(k); d2 >= 0 {
c.lr[d2] = c.lr[d2].splay(k - d2*(c.lr[0].size()+1))
if d2 == d {
o = o.rotate(d ^ 1)
} else {
o.lr[d] = c.rotate(d)
}
}
return o.rotate(d ^ 1)
}
func (o *spNode) splayMin() *spNode { return o.splay(1) }
func (o *spNode) splayMax() *spNode { return o.splay(o.size()) }
// 分裂子树 o,把 o 的前 k 小个节点放在 lo 子树,其他的放在 ro 子树(lo 节点为 o 的第 k 小节点)
// 0 < k <= o.size(),取等号时 ro 为 nil
func (o *spNode) split(k int) (lo, ro *spNode) {
lo = o.splay(k)
ro = lo.lr[1]
lo.lr[1] = nil
lo.maintain()
return
}
// 把子树 ro 合并进子树 o,返回合并前 o 的最大节点
// 子树 o 的所有元素比子树 ro 中的小
// o != nil
func (o *spNode) merge(ro *spNode) *spNode {
// 把最大节点伸展上来,这样会空出一个右儿子用来合并 ro
o = o.splayMax()
o.lr[1] = ro
o.maintain()
return o
}
type splay struct{ root *spNode }
const (
splayMin spKeyType = -2e9
splayMax spKeyType = 2e9
)
func newSplay() *splay {
// 放入两个哨兵节点 min max,以简化 put delete 的逻辑
// 注意哨兵对 size() 的影响
root := &spNode{key: splayMin} // value: 1
root.lr[1] = &spNode{key: splayMax} // value: 1
t := &splay{root}
t.maintain()
return t
}
func (t *splay) maintain() {
t.root.lr[1].maintain()
t.root.maintain()
}
// <= key 的元素个数
func (t *splay) rank(key spKeyType) (kth int) {
for o := t.root; o != nil; {
switch {
case key < o.key:
o = o.lr[0]
case key > o.key:
kth += 1 + o.lr[0].size()
o = o.lr[1]
default:
kth += 1 + o.lr[0].size()
return
}
}
return
}
func (t *splay) put(key spKeyType, value spValueType) {
t.root = t.root.splay(t.rank(key))
if t.root.key == key {
t.root.val += value
} else {
// 把右子树的最小节点伸展上来,这样它就会空出一个左儿子用来插入
t.root.lr[1] = t.root.lr[1].splayMin()
t.root.lr[1].lr[0] = &spNode{sz: 1, key: key, val: value}
}
t.maintain()
}
func (t *splay) delete(key spKeyType) {
t.root = t.root.splay(t.rank(key))
if t.root.key != key {
return
}
if t.root.val > 1 {
t.root.val--
} else {
// 把右子树的最小节点伸展上来,这样它就会空出一个左儿子用来插入
t.root.lr[1] = t.root.lr[1].splayMin()
t.root.lr[1].lr[0] = t.root.lr[0]
t.root = t.root.lr[1]
}
t.root.maintain()
}
// 其余和 BST 有关的方法见 bst.go
// 注意每次调用之前或之后都要执行一下 t.root = t.root.splay(t.rank(key)),以确保均摊复杂度为 O(logn)
// 注意 min max 哨兵对 rank() kth() 等方法的影响
//
func (o *spNode) String() (s string) {
if o.key == splayMin {
return "-∞"
}
if o.key == splayMax {
return "+∞"
}
//return strconv.Itoa(int(o.key))
if o.val == 1 {
s = Sprintf("%v", o.key)
} else {
s = Sprintf("%v(%v)", o.key, o.val)
}
s += Sprintf("[sz:%d]", o.sz)
return
}
/* 逆时针旋转 90° 打印这棵树:根节点在最左侧,右子树在上侧,左子树在下侧
效果如下(只打印 key)
Root
│ ┌── +∞
│ ┌── 96
│ ┌── 92
│ ┌── 90
│ │ └── 78
│ ┌── 77
│ │ └── 70
│ │ └── 62
│ ┌── 58
│ │ │ ┌── 55
│ │ └── 53
│ │ └── 51
│ │ └── 48
│ ┌── 47
└── 43
│ ┌── 40
│ ┌── 39
│ │ │ ┌── 37
│ │ │ ┌── 31
│ │ │ ┌── 30
│ │ └── 27
└── 25
│ ┌── 17
└── 10
│ ┌── 9
└── 8
└── -∞
*/
func (o *spNode) draw(treeSB, prefixSB *strings.Builder, isTail bool) {
prefix := prefixSB.String()
if o.lr[1] != nil {
newPrefixSB := &strings.Builder{}
newPrefixSB.WriteString(prefix)
if isTail {
newPrefixSB.WriteString("│ ")
} else {
newPrefixSB.WriteString(" ")
}
o.lr[1].draw(treeSB, newPrefixSB, false)
}
treeSB.WriteString(prefix)
if isTail {
treeSB.WriteString("└── ")
} else {
treeSB.WriteString("┌── ")
}
treeSB.WriteString(o.String())
treeSB.WriteByte('\n')
if o.lr[0] != nil {
newPrefixSB := &strings.Builder{}
newPrefixSB.WriteString(prefix)
if isTail {
newPrefixSB.WriteString(" ")
} else {
newPrefixSB.WriteString("│ ")
}
o.lr[0].draw(treeSB, newPrefixSB, true)
}
}
func (t *splay) String() string {
if t.root == nil {
return "Empty\n"
}
treeSB := &strings.Builder{}
treeSB.WriteString("Root\n")
t.root.draw(treeSB, &strings.Builder{}, true)
return treeSB.String()
}