-
Notifications
You must be signed in to change notification settings - Fork 31
/
KruskalTree.go
1265 lines (1147 loc) · 30.5 KB
/
KruskalTree.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// reference: https://nyaannyaan.github.io/library/forest/process-of-merging-forest.hpp
// 表示合并过程的树,按照edges中边的顺序合并顶点.(kruskal重构树)
// process-of-merging-forest/KruskalTree
// 加入第i条边时其所在连通块大小就是权值为i的点所在子树的叶节点数量
//
//
// 例如 0和1合并,边权为2;0和2合并,边权为3,那么返回值为:
// forest: [[] [] [] [{0 1}] [{2 3}]] (森林的有向图邻接表)
// roots: [4] (新图中的各个根节点)
// values: [0 0 0 2 3] (每个结点的权值)
//
// 4(3)
// / \
// 3(2) 2
// / \
// 0 1
//
// 0-n-1 为原始顶点, n-2n-2 为辅助顶点
package main
import (
"bufio"
"fmt"
"math/bits"
"os"
"sort"
)
func main() {
luogu4197()
}
// https://atcoder.jp/contests/agc002/tasks/agc002_d
// 一张`连通`图,q 次询问从两个点 x 和 y 出发,
// 希望经过的点数量等于 z(每个点可以重复经过,但是重复经过只计算一次)
// 求经过的边最大编号最小是多少。
//
// !根据性质:kruskal重构树中两个点u,v路径上的最大边权等于lca(u,v)的点权; kruskal重构树是一个大根堆.
// 因此可以二分编号,对于询问(x,y,z),我们从x和y分别倍增向上跳到点权大于当前二分值的位置,
// 然后再判断此时跳到节点的子树中的叶子节点数量是否大于等于z.
func StampRally() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m int
fmt.Fscan(in, &n, &m)
edges := make([]Edge, m)
for i := 0; i < m; i++ {
var u, v int
fmt.Fscan(in, &u, &v)
u--
v--
edges[i] = Edge{u, v, i + 1}
}
// !注意图是连通的,因此只有一个根节点
forest, roots, values := KruskalTree(n, edges) // 已经按照边权排序
tree := _NewTree(forest, roots)
treeMonoid := NewStaticTreeMonoid(tree, values)
root := roots[0]
subTreeLeafCount := make([]int, len(forest)) // 子树中的叶子节点数量
var dfs func(int) int
dfs = func(cur int) int {
if cur < n { // 原始顶点
subTreeLeafCount[cur] = 1
}
for _, to := range forest[cur] {
subTreeLeafCount[cur] += dfs(to)
}
return subTreeLeafCount[cur]
}
dfs(root)
var q int
fmt.Fscan(in, &q)
for i := 0; i < q; i++ {
var x, y, z int
fmt.Fscan(in, &x, &y, &z)
x--
y--
check := func(mid int) bool {
to1 := treeMonoid.MaxPath(func(e E) bool { return e <= mid }, x, root)
to2 := treeMonoid.MaxPath(func(e E) bool { return e <= mid }, y, root)
if to1 == to2 {
return subTreeLeafCount[to1] >= z
} else {
return subTreeLeafCount[to1]+subTreeLeafCount[to2] >= z
}
}
left, right := 1, m
for left <= right {
mid := (left + right) / 2
if check(mid) {
right = mid - 1
} else {
left = mid + 1
}
}
fmt.Fprintln(out, left)
}
}
// https://www.luogu.com.cn/problem/CF1706E
// Qpwoeirut and Vertices
// 给出 n 个点, m 条边的不带权连通无向图, q 次询问至少要加完编号前多少的边,
// 才能使得 [start,end) 中的所有点两两连通。
//
// 考虑 Kruskal 重构树。
// 由于这题求的是按照边的顺序,
// 所以我们把边的权值赋为边的序号,排序后建出 Kruskal 重构树
// !由kruskal重构树性质,等价于求[start,end)中的所有点的lca.
func CF1706E() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var T int
fmt.Fscan(in, &T)
solve := func() {
var n, m, q int
fmt.Fscan(in, &n, &m, &q)
edges := make([]Edge, m)
for i := 0; i < m; i++ {
var u, v int
fmt.Fscan(in, &u, &v)
u, v = u-1, v-1
edges[i] = Edge{u, v, i + 1}
}
forest, roots, values := KruskalTree(n, edges)
tree := _NewTree(forest, roots)
order := make([]int, n)
for i := range order {
order[i] = i
}
rangeLca := RangeLCA(order, tree.LCA)
for i := 0; i < q; i++ {
var start, end int
fmt.Fscan(in, &start, &end)
start--
lca := rangeLca(start, end)
fmt.Fprintln(out, values[lca])
}
}
for t := 0; t < T; t++ {
solve()
}
}
// https://www.luogu.com.cn/problem/P4197
// 给定一张无向带权图和q个询问(u,limit,k),每个点有一个得分,每条边有一个边权。
// !每次查询从u出发,只经过边权小于等于limit的点中第k大的点的得分,如果不存在这样的点,输出-1。
// P4197 Peaks
func luogu4197() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m, q int
fmt.Fscan(in, &n, &m, &q)
scores := make([]int, n)
for i := range scores {
fmt.Fscan(in, &scores[i])
}
edges := make([]Edge, m)
for i := range edges {
var u, v, w int
fmt.Fscan(in, &u, &v, &w)
u, v = u-1, v-1
edges[i] = Edge{u, v, w}
}
sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
forest, roots, values := KruskalTree(n, edges)
tree := _NewTree(forest, roots)
treeMonoid := NewStaticTreeMonoid(tree, values)
belongRoot := make([]int, len(forest))
lid, rid := make([]int, len(forest)), make([]int, len(forest))
dfn := 0
var dfs func(int, int)
dfs = func(cur, root int) {
belongRoot[cur] = root
lid[cur] = dfn
// !注意kruskal中的dfs序带上了辅助结点,需要忽略这些结点
if cur < n {
dfn++
}
for _, to := range forest[cur] {
dfs(to, root)
}
rid[cur] = dfn
}
for _, root := range roots {
dfs(root, root)
}
newScores := make([]int, n)
for i, v := range scores {
newScores[lid[i]] = v
}
wm := NewWaveletMatrix(newScores)
for i := 0; i < q; i++ {
var u, limit, k int
fmt.Fscan(in, &u, &limit, &k)
u--
k--
curRoot := belongRoot[u]
to := treeMonoid.MaxPath(func(x int) bool { return x <= limit }, u, curRoot)
if to == -1 {
fmt.Fprintln(out, -1)
continue
}
lid, rid := lid[to], rid[to]
if k >= rid-lid {
fmt.Fprintln(out, -1)
continue
}
fmt.Fprintln(out, wm.KthMax(lid, rid, k))
}
}
// https://www.luogu.com.cn/problem/P1967
// P1967 [NOIP2013 提高组] 货车运输
// 给定一张无向带权图和q个询问(u,v), 每次查询从u到v的路径上的最小边权的最大值.如果不存在这样的路径,输出-1.
// 解:
// !从u到v最大边权最小值即为kruskal重构树中lca(u,v)的值.
func luogu1967() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m int
fmt.Fscan(in, &n, &m)
edges := make([]Edge, m)
uf := NewUf(n)
for i := range edges {
var u, v, w int
fmt.Fscan(in, &u, &v, &w)
u, v = u-1, v-1
edges[i] = Edge{u, v, w}
uf.Union(u, v, nil)
}
// 因为要求最大的最小边权,所以边权从大到小排序
sort.Slice(edges, func(i, j int) bool { return edges[i].weight > edges[j].weight })
forest, roots, values := KruskalTree(n, edges)
tree := _NewTree(forest, roots)
var q int
fmt.Fscan(in, &q)
for i := 0; i < q; i++ {
var u, v int
fmt.Fscan(in, &u, &v)
u, v = u-1, v-1
if uf.Find(u) != uf.Find(v) {
fmt.Fprintln(out, -1)
continue
}
fmt.Fprintln(out, values[tree.LCA(u, v)])
}
}
// https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths-ii/description/
// 1724. 检查边长度限制的路径是否存在
// 当存在一条从 p 到 q 的路径,且路径中每条边的距离都严格小于 limit 时,返回 true
type DistanceLimitedPathsExist struct {
uf *Uf
tree *_Tree
treeMonoid *StaticTreeMonoid
}
func Constructor(n int, edgeList [][]int) DistanceLimitedPathsExist {
uf := NewUf(n)
for _, e := range edgeList {
uf.Union(e[0], e[1], nil)
}
sort.Slice(edgeList, func(i, j int) bool { return edgeList[i][2] < edgeList[j][2] })
sortedEdges := make([]Edge, len(edgeList))
for i, e := range edgeList {
sortedEdges[i] = Edge{e[0], e[1], e[2]}
}
forest, roots, values := KruskalTree(n, sortedEdges)
tree := _NewTree(forest, roots)
treeMonoid := NewStaticTreeMonoid(tree, values)
return DistanceLimitedPathsExist{uf: uf, tree: tree, treeMonoid: treeMonoid}
}
func (this *DistanceLimitedPathsExist) Query(p int, q int, limit int) bool {
if this.uf.Find(p) != this.uf.Find(q) {
return false
}
return this.treeMonoid.QueryPath(p, q) < limit
}
// https://yukicoder.me/problems/no/1451
// 初始时有n个人
// 给定m个操作,每次将i和j所在的班级合并,大小相等时随机选取班长,否则选取较大的班级的班长作为新班级的班长
// 对每个人,问最后成为班长的概率
func yuki1451() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
const MOD int = 1e9 + 7
pow := func(base, exp, mod int) int {
base %= mod
res := 1 % mod
for ; exp > 0; exp >>= 1 {
if exp&1 == 1 {
res = res * base % mod
}
base = base * base % mod
}
return res
}
var INV2 = pow(2, MOD-2, MOD)
var n, m int
fmt.Fscan(in, &n, &m)
sortedEdges := make([]Edge, 0, m)
for i := 0; i < m; i++ {
var u, v int
fmt.Fscan(in, &u, &v)
u, v = u-1, v-1
sortedEdges = append(sortedEdges, Edge{u, v, 1})
}
forest, roots, _ := KruskalTree(n, sortedEdges)
subSize := make([]int, len(forest))
var getSubSize func(int) int
getSubSize = func(cur int) int {
if cur < n { // 原始顶点
subSize[cur] = 1
}
for _, to := range forest[cur] {
subSize[cur] += getSubSize(to)
}
return subSize[cur]
}
for _, root := range roots {
getSubSize(root)
}
res := make([]int, n)
var run func(int, int)
run = func(cur, p int) {
if cur < n { // 原始顶点
res[cur] = p
return
}
if len(forest[cur]) == 1 { // 只有一个子节点
run(forest[cur][0], p)
return
}
left, right := forest[cur][0], forest[cur][1] // 两个子节点
if subSize[left] > subSize[right] {
run(left, p)
} else if subSize[left] < subSize[right] {
run(right, p)
} else {
run(left, p*INV2%MOD)
run(right, p*INV2%MOD)
}
}
for _, root := range roots {
run(root, 1)
}
for _, v := range res {
fmt.Fprintln(out, v)
}
}
const INF int = 1e18
type E = int
const IS_COMMUTATIVE = true // 幺半群是否满足交换律
func e() E { return 0 }
func op(e1, e2 E) E { return max(e1, e2) }
type Edge struct{ from, to, weight int }
// 表示合并过程的树,按照edges中边的顺序合并顶点.
//
// 返回:
// forest: 森林的有向图邻接表
// roots: 新图中的各个根节点
// values: 每个辅助结点的权值(即对应边的权值,叶子结点权值为0).
func KruskalTree(n int, edges []Edge) (forest [][]int, roots []int, values []int) {
parent := make([]int32, 2*n-1)
for i := range parent {
parent[i] = int32(i)
}
forest = make([][]int, 2*n-1)
values = make([]int, 2*n-1)
uf := NewUf(n)
aux := int32(n)
for i := range edges {
e := &edges[i]
from, to := e.from, e.to
f := func(big, small int) {
w, p1, p2 := e.weight, int(parent[big]), int(parent[small])
forest[aux] = append(forest[aux], p1)
forest[aux] = append(forest[aux], p2)
parent[p1], parent[p2] = aux, aux
parent[big], parent[small] = aux, aux
values[aux] = w
aux++
}
uf.Union(from, to, f)
}
forest = forest[:aux]
values = values[:aux]
for i := int32(0); i < aux; i++ {
if parent[i] == i {
roots = append(roots, int(i))
}
}
return
}
type Uf struct {
data []int32
}
func NewUf(n int) *Uf {
data := make([]int32, n)
for i := 0; i < n; i++ {
data[i] = -1
}
return &Uf{data: data}
}
func (ufa *Uf) Union(key1, key2 int, f func(big, small int)) bool {
root1, root2 := ufa.Find(key1), ufa.Find(key2)
if root1 == root2 {
return false
}
if ufa.data[root1] > ufa.data[root2] {
root1, root2 = root2, root1
}
ufa.data[root1] += ufa.data[root2]
ufa.data[root2] = int32(root1)
if f != nil {
f(root1, root2)
}
return true
}
func (ufa *Uf) Find(key int) int {
if ufa.data[key] < 0 {
return key
}
ufa.data[key] = int32(ufa.Find(int(ufa.data[key])))
return int(ufa.data[key])
}
type StaticTreeMonoid struct {
tree *_Tree
n int
unit E
seg *DisjointSparseTable
segR *DisjointSparseTable
}
// 静态树的路径查询, 维护的量需要满足幺半群的性质.
//
// data: 顶点的值.
func NewStaticTreeMonoid(tree *_Tree, data []E) *StaticTreeMonoid {
n := len(tree.Tree)
res := &StaticTreeMonoid{tree: tree, n: n, unit: e()}
leaves := make([]E, n)
for v := range leaves {
leaves[tree.LID[v]] = data[v]
}
res.seg = NewDisjointSparse(leaves, e, op)
if !IS_COMMUTATIVE {
res.segR = NewDisjointSparse(leaves, e, func(e1, e2 E) E { return op(e2, e1) }) // opRev
}
return res
}
// 查询 start 到 target 的路径上的值.(点权/边权 由 isVertex 决定)
func (st *StaticTreeMonoid) QueryPath(start, target int) E {
path := st.tree.GetPathDecomposition(start, target)
val := st.unit
for _, ab := range path {
a, b := int(ab[0]), int(ab[1])
var x E
if a <= b {
x = st.seg.Query(a, b+1)
} else if IS_COMMUTATIVE {
x = st.seg.Query(b, a+1)
} else {
x = st.segR.Query(b, a+1)
}
val = op(val, x)
}
return val
}
// 找到路径上最后一个 x 使得 QueryPath(start,x) 满足check函数.不存在返回-1.
func (st *StaticTreeMonoid) MaxPath(check func(E) bool, start, target int) int {
if !check(st.QueryPath(start, start)) {
return -1
}
path := st.tree.GetPathDecomposition(start, target)
val := st.unit
for _, ab := range path {
a, b := int(ab[0]), int(ab[1])
x := st._getProd(a, b)
if tmp := op(val, x); check(tmp) {
val = tmp
start = int(st.tree.IdToNode[b])
continue
}
checkTmp := func(x E) bool {
return check(op(val, x))
}
if a <= b {
i := st.seg.MaxRight(a, checkTmp)
if i == a {
return start
}
return int(st.tree.IdToNode[i-1])
} else {
var i int
if IS_COMMUTATIVE {
i = st.seg.MinLeft(a+1, checkTmp)
} else {
i = st.segR.MinLeft(a+1, checkTmp)
}
if i == a+1 {
return start
}
return int(st.tree.IdToNode[i])
}
}
return target
}
func (st *StaticTreeMonoid) QuerySubtree(root int) E {
l, r := st.tree.LID[root], st.tree.RID[root]
return st.seg.Query(int(l), int(r))
}
func (st *StaticTreeMonoid) _getProd(a, b int) E {
if IS_COMMUTATIVE {
if a <= b {
return st.seg.Query(a, b+1)
}
return st.seg.Query(b, a+1)
} else {
if a <= b {
return st.seg.Query(a, b+1)
}
return st.segR.Query(b, a+1)
}
}
type _Tree struct {
Tree [][]int
Depth []int32
Parent []int32
LID, RID []int32 // 欧拉序[in,out)
IdToNode []int32
top, heavySon []int32
timer int32
}
func _NewTree(tree [][]int, roots []int) *_Tree {
n := len(tree)
lid := make([]int32, n)
rid := make([]int32, n)
IdToNode := make([]int32, n)
top := make([]int32, n) // 所处轻/重链的顶点(深度最小),轻链的顶点为自身
depth := make([]int32, n) // 深度
parent := make([]int32, n) // 父结点
heavySon := make([]int32, n) // 重儿子
for i := range parent {
parent[i] = -1
}
res := &_Tree{
Tree: tree,
Depth: depth,
Parent: parent,
LID: lid,
RID: rid,
IdToNode: IdToNode,
top: top,
heavySon: heavySon,
}
res._build(roots)
return res
}
// 返回 root 的欧拉序区间, 左闭右开, 0-indexed.
func (tree *_Tree) Id(root int) (int, int) {
return int(tree.LID[root]), int(tree.RID[root])
}
func (tree *_Tree) LCA(u, v int) int {
for {
if tree.LID[u] > tree.LID[v] {
u, v = v, u
}
if tree.top[u] == tree.top[v] {
return u
}
v = int(tree.Parent[tree.top[v]])
}
}
func (tree *_Tree) RootedLCA(u, v int, root int) int {
return tree.LCA(u, v) ^ tree.LCA(u, root) ^ tree.LCA(v, root)
}
func (tree *_Tree) Dist(u, v int) int {
return int(tree.Depth[u] + tree.Depth[v] - 2*tree.Depth[tree.LCA(u, v)])
}
// k: 0-based
//
// 如果不存在第k个祖先,返回-1
func (tree *_Tree) KthAncestor(root, k int) int {
root32 := int32(root)
k32 := int32(k)
if k32 > tree.Depth[root32] {
return -1
}
for {
u := tree.top[root32]
if tree.LID[root32]-k32 >= tree.LID[u] {
return int(tree.IdToNode[tree.LID[root32]-k32])
}
k32 -= tree.LID[root32] - tree.LID[u] + 1
root32 = tree.Parent[u]
}
}
// 从 from 节点跳向 to 节点,跳过 step 个节点(0-indexed)
//
// 返回跳到的节点,如果不存在这样的节点,返回-1
func (tree *_Tree) Jump(from, to, step int) int {
step32 := int32(step)
if step32 == 1 {
if from == to {
return -1
}
if tree.IsInSubtree(to, from) {
return tree.KthAncestor(to, int(tree.Depth[to]-tree.Depth[from]-1))
}
return int(tree.Parent[from])
}
c := tree.LCA(from, to)
dac := tree.Depth[from] - tree.Depth[c]
dbc := tree.Depth[to] - tree.Depth[c]
if step32 > dac+dbc {
return -1
}
if step32 <= dac {
return tree.KthAncestor(from, int(step32))
}
return tree.KthAncestor(to, int(dac+dbc-step32))
}
func (tree *_Tree) CollectChild(root int) []int {
res := []int{}
for _, next := range tree.Tree[root] {
if next != int(tree.Parent[root]) {
res = append(res, next)
}
}
return res
}
// 返回沿着`路径顺序`的 [起点,终点] 的 欧拉序 `左闭右闭` 数组.
//
// !eg:[[2 0] [4 4]] 沿着路径顺序但不一定沿着欧拉序.
func (tree *_Tree) GetPathDecomposition(u, v int) [][2]int32 {
u32, v32 := int32(u), int32(v)
up, down := [][2]int32{}, [][2]int32{}
for {
if tree.top[u32] == tree.top[v32] {
break
}
if tree.LID[u32] < tree.LID[v32] {
down = append(down, [2]int32{tree.LID[tree.top[v32]], tree.LID[v32]})
v32 = tree.Parent[tree.top[v32]]
} else {
up = append(up, [2]int32{tree.LID[u32], tree.LID[tree.top[u32]]})
u32 = tree.Parent[tree.top[u32]]
}
}
if tree.LID[u32] < tree.LID[v32] {
down = append(down, [2]int32{tree.LID[u32], tree.LID[v32]})
} else if tree.LID[v32] <= tree.LID[u32] {
up = append(up, [2]int32{tree.LID[u32], tree.LID[v32]})
}
for i := 0; i < len(down)/2; i++ {
down[i], down[len(down)-1-i] = down[len(down)-1-i], down[i]
}
return append(up, down...)
}
func (tree *_Tree) EnumeratePathDecomposition(u, v int, f func(a, b int)) {
u32, v32 := int32(u), int32(v)
down := [][2]int32{}
for {
if tree.top[u32] == tree.top[v32] {
break
}
if tree.LID[u32] < tree.LID[v32] {
down = append(down, [2]int32{tree.LID[tree.top[v32]], tree.LID[v32]})
v32 = tree.Parent[tree.top[v32]]
} else {
f(int(tree.LID[u32]), int(tree.LID[tree.top[u32]]))
u32 = tree.Parent[tree.top[u32]]
}
}
if tree.LID[u32] < tree.LID[v32] {
down = append(down, [2]int32{tree.LID[u32], tree.LID[v32]})
} else if tree.LID[v32] <= tree.LID[u32] {
f(int(tree.LID[u32]), int(tree.LID[v32]))
}
for i := len(down) - 1; i >= 0; i-- {
f(int(down[i][0]), int(down[i][1]))
}
}
func (tree *_Tree) GetPath(u, v int) []int {
res := []int{}
composition := tree.GetPathDecomposition(u, v)
for _, e := range composition {
a, b := e[0], e[1]
if a <= b {
for i := a; i <= b; i++ {
res = append(res, int(tree.IdToNode[i]))
}
} else {
for i := a; i >= b; i-- {
res = append(res, int(tree.IdToNode[i]))
}
}
}
return res
}
// 以root为根时,结点v的子树大小.
func (tree *_Tree) SubtreeSize(v, root int) int {
if root == -1 {
return int(tree.RID[v] - tree.LID[v])
}
if v == root {
return len(tree.Tree)
}
x := tree.Jump(v, root, 1)
if tree.IsInSubtree(v, x) {
return int(tree.RID[v] - tree.LID[v])
}
return len(tree.Tree) - int(tree.RID[x]) + int(tree.LID[x])
}
// child 是否在 root 的子树中 (child和root不能相等)
func (tree *_Tree) IsInSubtree(child, root int) bool {
return tree.LID[root] <= tree.LID[child] && tree.LID[child] < tree.RID[root]
}
func (tree *_Tree) ELID(u int) int {
return int(2*tree.LID[u] - tree.Depth[u])
}
func (tree *_Tree) ERID(u int) int {
return int(2*tree.RID[u] - tree.Depth[u] - 1)
}
func (tree *_Tree) build(cur, pre, dep int32) int {
subSize, heavySize, heavySon := 1, 0, int32(-1)
for _, next := range tree.Tree[cur] {
next32 := int32(next)
if next32 != pre {
nextSize := tree.build(next32, cur, dep+1)
subSize += nextSize
if nextSize > heavySize {
heavySize, heavySon = nextSize, next32
}
}
}
tree.Depth[cur] = dep
tree.heavySon[cur] = heavySon
tree.Parent[cur] = pre
return subSize
}
func (tree *_Tree) markTop(cur, top int32) {
tree.top[cur] = top
tree.LID[cur] = tree.timer
tree.IdToNode[tree.timer] = cur
tree.timer++
if tree.heavySon[cur] != -1 {
tree.markTop(tree.heavySon[cur], top)
for _, next := range tree.Tree[cur] {
next32 := int32(next)
if next32 != tree.heavySon[cur] && next32 != tree.Parent[cur] {
tree.markTop(next32, next32)
}
}
}
tree.RID[cur] = tree.timer
}
// root:0-based
func (tree *_Tree) _build(roots []int) {
for _, root := range roots {
root32 := int32(root)
tree.build(root32, -1, 0)
tree.markTop(root32, root32)
}
}
type DisjointSparseTable struct {
n, log int
data [][]E
unit E
op func(E, E) E
}
// DisjointSparseTable 支持幺半群的区间静态查询.
//
// eg: 区间乘积取模/区间仿射变换...
func NewDisjointSparse(leaves []E, e func() E, op func(E, E) E) *DisjointSparseTable {
res := &DisjointSparseTable{}
n := len(leaves)
log := 1
for (1 << log) < n {
log++
}
data := make([][]E, log)
data[0] = append(data[0], leaves...)
for i := 1; i < log; i++ {
data[i] = append(data[i], data[0]...)
v := data[i]
b := 1 << i
for m := b; m <= n; m += 2 * b {
l, r := m-b, min(m+b, n)
for j := m - 1; j >= l+1; j-- {
v[j-1] = op(v[j-1], v[j])
}
for j := m; j < r-1; j++ {
v[j+1] = op(v[j], v[j+1])
}
}
}
res.n = n
res.log = log
res.data = data
res.unit = e()
res.op = op
return res
}
func (ds *DisjointSparseTable) Query(start, end int) E {
if start == end {
return ds.unit
}
end--
if start == end {
return ds.data[0][start]
}
k := 31 - bits.LeadingZeros32(uint32(start^end))
return ds.op(ds.data[k][start], ds.data[k][end])
}
// 返回最大的 right 使得 [left,right) 内的值满足 check.
func (ds *DisjointSparseTable) MaxRight(left int, check func(e E) bool) int {
if left == ds.n {
return ds.n
}
ok, ng := left, ds.n+1
for ok+1 < ng {
mid := (ok + ng) >> 1
if check(ds.Query(left, mid)) {
ok = mid
} else {
ng = mid
}
}
return ok
}
// 返回最小的 left 使得 [left,right) 内的值满足 check.
func (ds *DisjointSparseTable) MinLeft(right int, check func(e E) bool) int {
if right == 0 {
return 0
}
ok, ng := right, -1
for ng+1 < ok {
mid := (ok + ng) >> 1
if check(ds.Query(mid, right)) {
ok = mid
} else {
ng = mid
}
}
return ok
}
// 给定非负整数数组 nums 构建一个 WaveletMatrix.
func NewWaveletMatrix(data []int) *WaveletMatrix {
dataCopy := make([]int, len(data))
max_ := 0
for i, v := range data {
if v > max_ {
max_ = v
}
dataCopy[i] = v
}
maxLog := bits.Len(uint(max_)) + 1
n := len(dataCopy)
mat := make([]*BitVector, maxLog)
zs := make([]int, maxLog)
buff1 := make([]int, maxLog)
buff2 := make([]int, maxLog)
ls, rs := make([]int, n), make([]int, n)
for dep := 0; dep < maxLog; dep++ {
mat[dep] = NewBitVector(n + 1)
p, q := 0, 0
for i := 0; i < n; i++ {
k := (dataCopy[i] >> uint(maxLog-dep-1)) & 1
if k == 1 {
rs[q] = dataCopy[i]
mat[dep].Set(i)
q++
} else {
ls[p] = dataCopy[i]
p++
}
}
zs[dep] = p
mat[dep].Build()
ls = dataCopy
for i := 0; i < q; i++ {
dataCopy[p+i] = rs[i]
}
}
return &WaveletMatrix{
n: n,
maxLog: maxLog,
mat: mat,
zs: zs,
buff1: buff1,
buff2: buff2,
}
}
type WaveletMatrix struct {
n int
maxLog int
mat []*BitVector
zs []int
buff1, buff2 []int
}
// [start, end) 内的 value 的個数.
func (w *WaveletMatrix) Count(start, end, value int) int {
return w.count(value, end) - w.count(value, start)
}
// [start, end) 内 [lower, upper) 的个数.
func (w *WaveletMatrix) CountRange(start, end, lower, upper int) int {
return w.freqDfs(0, start, end, 0, lower, upper)
}
// 第k(0-indexed)个value的位置.
func (w *WaveletMatrix) Index(value, k int) int {
w.count(value, w.n)
for dep := w.maxLog - 1; dep >= 0; dep-- {