-
Notifications
You must be signed in to change notification settings - Fork 31
/
ODT-treemap.go
930 lines (827 loc) · 18.7 KB
/
ODT-treemap.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// func init() {
// debug.SetGCPercent(-1)
// }
func UnionOfInterval() {
// https://atcoder.jp/contests/abc256/tasks/abc256_d
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n int
fmt.Fscan(in, &n)
odt := NewIntervals(-1, -INF, INF)
for i := 0; i < n; i++ {
var l, r int
fmt.Fscan(in, &l, &r)
odt.Set(l, r, 1)
}
odt.EnumerateAll(func(l, r int, x int) {
if x == 1 {
fmt.Fprintln(out, l, r)
}
})
}
func main() {
UnionOfInterval()
}
const INF int = 1e18
type Value = int
type Intervals struct {
Len int // 区间数
Count int // 区间元素个数之和
noneValue Value
mp *TreeMap
leftLimit, rightLimit int
}
func NewIntervals(noneValue Value, leftLimit, rightLimit int) *Intervals {
res := &Intervals{
noneValue: noneValue,
mp: NewTreeMap(),
leftLimit: leftLimit,
rightLimit: rightLimit,
}
res.mp.Set(res.leftLimit, noneValue)
res.mp.Set(res.rightLimit, noneValue)
return res
}
// 返回包含 x 的区间的信息.
func (odt *Intervals) Get(x int, erase bool) (start, end int, value Value) {
iter := odt.mp.UpperBound(x)
end = iter.Value()
iter.Prev()
start, value = iter.Key(), iter.Value()
if erase && value != odt.noneValue {
odt.Len--
odt.Count -= end - start
odt.mp.Set(start, odt.noneValue)
odt.mergeAt(start)
odt.mergeAt(end)
}
return
}
func (odt *Intervals) Set(start, end int, value Value) {
odt.EnumerateRange(start, end, func(l, r int, x Value) {}, true)
odt.mp.Set(start, value)
if value != odt.noneValue {
odt.Len++
odt.Count += end - start
}
odt.mergeAt(start)
odt.mergeAt(end)
}
func (odt *Intervals) EnumerateAll(f func(start, end int, value Value)) {
odt.EnumerateRange(odt.leftLimit, odt.rightLimit, f, false)
}
// 遍历范围 [L, R) 内的所有数据.
func (odt *Intervals) EnumerateRange(start, end int, f func(start, end int, value Value), erase bool) {
if !(odt.leftLimit <= start && start <= end && end <= odt.rightLimit) {
panic(fmt.Sprintf("invalid range [%d, %d)", start, end))
}
NONE := odt.noneValue
if !erase {
it1 := odt.mp.UpperBound(start)
it1.Prev()
for {
key1, val1 := it1.Key(), it1.Value()
if key1 >= end {
break
}
it1.Next()
key2 := it1.Key()
f(max(key1, start), min(key2, end), val1)
}
return
}
iter1 := odt.mp.UpperBound(start)
iter1.Prev()
if key1, val1 := iter1.Key(), iter1.Value(); key1 < start {
odt.mp.Set(start, val1)
if val1 != NONE {
odt.Len++
}
}
// 分割区间
iter1 = odt.mp.LowerBound(end)
if key1 := iter1.Key(); key1 > end {
iter1.Prev()
val2 := iter1.Value()
odt.mp.Set(end, val2)
if val2 != NONE {
odt.Len++
}
}
iter1 = odt.mp.LowerBound(start)
for {
key1, val1 := iter1.Key(), iter1.Value()
if key1 >= end {
break
}
iter1 = odt.mp.Erase(iter1)
key2 := iter1.Key()
f(key1, key2, val1)
if val1 != NONE {
odt.Len--
odt.Count -= key2 - key1
}
}
odt.mp.Set(start, NONE)
}
func (odt *Intervals) String() string {
sb := []string{}
odt.EnumerateAll(func(start, end int, value Value) {
var v interface{} = value
if value == odt.noneValue {
v = "nil"
}
sb = append(sb, fmt.Sprintf("[%d,%d):%v", start, end, v))
})
return fmt.Sprintf("ODT{%v}", strings.Join(sb, ", "))
}
func (odt *Intervals) mergeAt(p int) {
if p == odt.leftLimit || p == odt.rightLimit {
return
}
iter1 := odt.mp.LowerBound(p)
val1 := iter1.Value()
iter1.Prev()
val2 := iter1.Value()
if val1 == val2 {
if val1 != odt.noneValue {
odt.Len--
}
iter1.Next()
odt.mp.Erase(iter1)
}
}
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
}
type TreeMap struct {
tree *_Tree
}
type TreeMapIterator struct {
iterator Iterator
}
func NewTreeMap() *TreeMap {
return &TreeMap{tree: _NewRedBlackTree()}
}
func (m *TreeMap) Iterator() Iterator {
return m.tree.Iterator()
}
func (m *TreeMap) Set(key int, value Value) {
m.tree.Put(key, value)
}
func (m *TreeMap) Get(key int) (value Value, found bool) {
return m.tree.Get(key)
}
func (m *TreeMap) Discard(key int) bool {
return m.tree.Discard(key)
}
func (m *TreeMap) Size() int {
return m.tree.Size()
}
func (m *TreeMap) Floor(key int) (foundkey int, foundvalue Value, ok bool) {
node, found := m.tree.Floor(key)
if found {
return node.Key, node.Value, true
}
return
}
func (m *TreeMap) Ceiling(key int) (foundkey int, foundvalue Value, ok bool) {
node, found := m.tree.Ceiling(key)
if found {
return node.Key, node.Value, true
}
return
}
func (m *TreeMap) Lower(key int) (foundkey int, foundvalue Value, ok bool) {
node, found := m.tree.Lower(key)
if found {
return node.Key, node.Value, true
}
return
}
func (m *TreeMap) Higher(key int) (foundkey int, foundvalue Value, ok bool) {
node, found := m.tree.Higher(key)
if found {
return node.Key, node.Value, true
}
return
}
// 返回删除元素的后继元素的迭代器,如果删除的是最后一个元素,则返回end()迭代器。
func (m *TreeMap) Erase(it Iterator) Iterator {
node := it.node
it.Next()
m.tree.DiscardNode(node)
return it
}
// 返回一个迭代器,指向键值>= key的第一个元素。
func (m *TreeMap) LowerBound(key int) Iterator {
lower, ok := m.tree.Ceiling(key)
if !ok {
it := m.tree.Iterator()
it.End()
return it
}
return m.tree.IteratorAt(lower)
}
// 返回一个迭代器,指向键值> key的第一个元素。
func (m *TreeMap) UpperBound(key int) Iterator {
upper, ok := m.tree.Higher(key)
if !ok {
it := m.tree.Iterator()
it.End()
return it
}
return m.tree.IteratorAt(upper)
}
func (m *TreeMap) String() string {
str := "TreeMap\nmap["
it := m.Iterator()
for it.Next() {
str += fmt.Sprintf("%v:%v ", it.Key(), it.Value())
}
return strings.TrimRight(str, " ") + "]"
}
// !region RedBlackTree
//
type _color bool
const (
_black, _red _color = true, false
)
type _Tree struct {
Root *_RBNode
size int
}
type _RBNode struct {
Key int
Value Value
color _color
Left *_RBNode
Right *_RBNode
Parent *_RBNode
}
func _NewRedBlackTree() *_Tree {
return &_Tree{}
}
func (tree *_Tree) Put(key int, value Value) {
var insertedNode *_RBNode
if tree.Root == nil {
tree.Root = &_RBNode{Key: key, Value: value, color: _red}
insertedNode = tree.Root
} else {
node := tree.Root
loop := true
for loop {
compare := key - node.Key
switch {
case compare == 0:
node.Key = key
node.Value = value
return
case compare < 0:
if node.Left == nil {
node.Left = &_RBNode{Key: key, Value: value, color: _red}
insertedNode = node.Left
loop = false
} else {
node = node.Left
}
case compare > 0:
if node.Right == nil {
node.Right = &_RBNode{Key: key, Value: value, color: _red}
insertedNode = node.Right
loop = false
} else {
node = node.Right
}
}
}
insertedNode.Parent = node
}
tree.insertCase1(insertedNode)
tree.size++
}
func (tree *_Tree) Get(key int) (value Value, found bool) {
node := tree.lookup(key)
if node != nil {
return node.Value, true
}
return
}
func (tree *_Tree) Discard(key int) bool {
node := tree.lookup(key)
return tree.DiscardNode(node)
}
func (tree *_Tree) DiscardNode(node *_RBNode) bool {
if node == nil {
return false
}
var child *_RBNode
if node.Left != nil && node.Right != nil {
pred := node.Left.maximumNode()
node.Key = pred.Key
node.Value = pred.Value
node = pred
}
if node.Left == nil || node.Right == nil {
if node.Right == nil {
child = node.Left
} else {
child = node.Right
}
if node.color == _black {
node.color = nodeColor(child)
tree.deleteCase1(node)
}
tree.replaceNode(node, child)
if node.Parent == nil && child != nil {
child.color = _black
}
}
tree.size--
return true
}
func (tree *_Tree) Empty() bool {
return tree.size == 0
}
func (tree *_Tree) Size() int {
return tree.size
}
func (node *_RBNode) Size() int {
if node == nil {
return 0
}
size := 1
if node.Left != nil {
size += node.Left.Size()
}
if node.Right != nil {
size += node.Right.Size()
}
return size
}
// Left returns the left-most (min) node or nil if tree is empty.
func (tree *_Tree) Left() *_RBNode {
var parent *_RBNode
current := tree.Root
for current != nil {
parent = current
current = current.Left
}
return parent
}
// Right returns the right-most (max) node or nil if tree is empty.
func (tree *_Tree) Right() *_RBNode {
var parent *_RBNode
current := tree.Root
for current != nil {
parent = current
current = current.Right
}
return parent
}
func (tree *_Tree) Floor(key int) (floor *_RBNode, found bool) {
found = false
node := tree.Root
for node != nil {
compare := key - node.Key
switch {
case compare == 0:
return node, true
case compare < 0:
node = node.Left
case compare > 0:
floor, found = node, true
node = node.Right
}
}
if found {
return floor, true
}
return nil, false
}
func (tree *_Tree) Lower(key int) (lower *_RBNode, found bool) {
found = false
node := tree.Root
for node != nil {
compare := key - node.Key
switch {
case compare <= 0:
node = node.Left
case compare > 0:
lower, found = node, true
node = node.Right
}
}
if found {
return lower, true
}
return nil, false
}
func (tree *_Tree) Ceiling(key int) (ceiling *_RBNode, found bool) {
found = false
node := tree.Root
for node != nil {
compare := key - node.Key
switch {
case compare == 0:
return node, true
case compare < 0:
ceiling, found = node, true
node = node.Left
case compare > 0:
node = node.Right
}
}
if found {
return ceiling, true
}
return nil, false
}
func (tree *_Tree) Higher(key int) (higher *_RBNode, found bool) {
found = false
node := tree.Root
for node != nil {
compare := key - node.Key
switch {
case compare < 0:
higher, found = node, true
node = node.Left
case compare >= 0:
node = node.Right
}
}
if found {
return higher, true
}
return nil, false
}
func (tree *_Tree) Clear() {
tree.Root = nil
tree.size = 0
}
func (tree *_Tree) String() string {
str := "RedBlackTree\n"
if !tree.Empty() {
output(tree.Root, "", true, &str)
}
return str
}
func (node *_RBNode) String() string {
return fmt.Sprintf("%v", node.Key)
}
func output(node *_RBNode, prefix string, isTail bool, str *string) {
if node.Right != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
output(node.Right, newPrefix, false, str)
}
*str += prefix
if isTail {
*str += "└── "
} else {
*str += "┌── "
}
*str += node.String() + "\n"
if node.Left != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
output(node.Left, newPrefix, true, str)
}
}
func (tree *_Tree) lookup(key int) *_RBNode {
node := tree.Root
for node != nil {
compare := key - node.Key
switch {
case compare == 0:
return node
case compare < 0:
node = node.Left
case compare > 0:
node = node.Right
}
}
return nil
}
func (node *_RBNode) grandparent() *_RBNode {
if node != nil && node.Parent != nil {
return node.Parent.Parent
}
return nil
}
func (node *_RBNode) uncle() *_RBNode {
if node == nil || node.Parent == nil || node.Parent.Parent == nil {
return nil
}
return node.Parent.sibling()
}
func (node *_RBNode) sibling() *_RBNode {
if node == nil || node.Parent == nil {
return nil
}
if node == node.Parent.Left {
return node.Parent.Right
}
return node.Parent.Left
}
func (tree *_Tree) rotateLeft(node *_RBNode) {
right := node.Right
tree.replaceNode(node, right)
node.Right = right.Left
if right.Left != nil {
right.Left.Parent = node
}
right.Left = node
node.Parent = right
}
func (tree *_Tree) rotateRight(node *_RBNode) {
left := node.Left
tree.replaceNode(node, left)
node.Left = left.Right
if left.Right != nil {
left.Right.Parent = node
}
left.Right = node
node.Parent = left
}
func (tree *_Tree) replaceNode(old *_RBNode, new *_RBNode) {
if old.Parent == nil {
tree.Root = new
} else {
if old == old.Parent.Left {
old.Parent.Left = new
} else {
old.Parent.Right = new
}
}
if new != nil {
new.Parent = old.Parent
}
}
func (tree *_Tree) insertCase1(node *_RBNode) {
if node.Parent == nil {
node.color = _black
} else {
tree.insertCase2(node)
}
}
func (tree *_Tree) insertCase2(node *_RBNode) {
if nodeColor(node.Parent) == _black {
return
}
tree.insertCase3(node)
}
func (tree *_Tree) insertCase3(node *_RBNode) {
uncle := node.uncle()
if nodeColor(uncle) == _red {
node.Parent.color = _black
uncle.color = _black
node.grandparent().color = _red
tree.insertCase1(node.grandparent())
} else {
tree.insertCase4(node)
}
}
func (tree *_Tree) insertCase4(node *_RBNode) {
grandparent := node.grandparent()
if node == node.Parent.Right && node.Parent == grandparent.Left {
tree.rotateLeft(node.Parent)
node = node.Left
} else if node == node.Parent.Left && node.Parent == grandparent.Right {
tree.rotateRight(node.Parent)
node = node.Right
}
tree.insertCase5(node)
}
func (tree *_Tree) insertCase5(node *_RBNode) {
node.Parent.color = _black
grandparent := node.grandparent()
grandparent.color = _red
if node == node.Parent.Left && node.Parent == grandparent.Left {
tree.rotateRight(grandparent)
} else if node == node.Parent.Right && node.Parent == grandparent.Right {
tree.rotateLeft(grandparent)
}
}
func (node *_RBNode) maximumNode() *_RBNode {
if node == nil {
return nil
}
for node.Right != nil {
node = node.Right
}
return node
}
func (tree *_Tree) deleteCase1(node *_RBNode) {
if node.Parent == nil {
return
}
tree.deleteCase2(node)
}
func (tree *_Tree) deleteCase2(node *_RBNode) {
sibling := node.sibling()
if nodeColor(sibling) == _red {
node.Parent.color = _red
sibling.color = _black
if node == node.Parent.Left {
tree.rotateLeft(node.Parent)
} else {
tree.rotateRight(node.Parent)
}
}
tree.deleteCase3(node)
}
func (tree *_Tree) deleteCase3(node *_RBNode) {
sibling := node.sibling()
if nodeColor(node.Parent) == _black &&
nodeColor(sibling) == _black &&
nodeColor(sibling.Left) == _black &&
nodeColor(sibling.Right) == _black {
sibling.color = _red
tree.deleteCase1(node.Parent)
} else {
tree.deleteCase4(node)
}
}
func (tree *_Tree) deleteCase4(node *_RBNode) {
sibling := node.sibling()
if nodeColor(node.Parent) == _red &&
nodeColor(sibling) == _black &&
nodeColor(sibling.Left) == _black &&
nodeColor(sibling.Right) == _black {
sibling.color = _red
node.Parent.color = _black
} else {
tree.deleteCase5(node)
}
}
func (tree *_Tree) deleteCase5(node *_RBNode) {
sibling := node.sibling()
if node == node.Parent.Left &&
nodeColor(sibling) == _black &&
nodeColor(sibling.Left) == _red &&
nodeColor(sibling.Right) == _black {
sibling.color = _red
sibling.Left.color = _black
tree.rotateRight(sibling)
} else if node == node.Parent.Right &&
nodeColor(sibling) == _black &&
nodeColor(sibling.Right) == _red &&
nodeColor(sibling.Left) == _black {
sibling.color = _red
sibling.Right.color = _black
tree.rotateLeft(sibling)
}
tree.deleteCase6(node)
}
func (tree *_Tree) deleteCase6(node *_RBNode) {
sibling := node.sibling()
sibling.color = nodeColor(node.Parent)
node.Parent.color = _black
if node == node.Parent.Left && nodeColor(sibling.Right) == _red {
sibling.Right.color = _black
tree.rotateLeft(node.Parent)
} else if nodeColor(sibling.Left) == _red {
sibling.Left.color = _black
tree.rotateRight(node.Parent)
}
}
func nodeColor(node *_RBNode) _color {
if node == nil {
return _black
}
return node.color
}
type Iterator struct {
tree *_Tree
node *_RBNode
position _position
}
type _position byte
const (
begin, between, end _position = 0, 1, 2
)
func (tree *_Tree) Iterator() Iterator {
return Iterator{tree: tree, node: nil, position: begin}
}
func (tree *_Tree) IteratorAt(node *_RBNode) Iterator {
return Iterator{tree: tree, node: node, position: between}
}
func (iterator *Iterator) Next() bool {
if iterator.position == end {
goto end
}
if iterator.position == begin {
left := iterator.tree.Left()
if left == nil {
goto end
}
iterator.node = left
goto between
}
if iterator.node.Right != nil {
iterator.node = iterator.node.Right
for iterator.node.Left != nil {
iterator.node = iterator.node.Left
}
goto between
}
for iterator.node.Parent != nil {
node := iterator.node
iterator.node = iterator.node.Parent
if node == iterator.node.Left {
goto between
}
}
end:
iterator.node = nil
iterator.position = end
return false
between:
iterator.position = between
return true
}
func (iterator *Iterator) Prev() bool {
if iterator.position == begin {
goto begin
}
if iterator.position == end {
right := iterator.tree.Right()
if right == nil {
goto begin
}
iterator.node = right
goto between
}
if iterator.node.Left != nil {
iterator.node = iterator.node.Left
for iterator.node.Right != nil {
iterator.node = iterator.node.Right
}
goto between
}
for iterator.node.Parent != nil {
node := iterator.node
iterator.node = iterator.node.Parent
if node == iterator.node.Right {
goto between
}
}
begin:
iterator.node = nil
iterator.position = begin
return false
between:
iterator.position = between
return true
}
func (iterator *Iterator) Value() Value {
return iterator.node.Value
}
func (iterator *Iterator) Key() int {
return iterator.node.Key
}
func (iterator *Iterator) Node() *_RBNode {
return iterator.node
}
func (iterator *Iterator) Begin() {
iterator.node = nil
iterator.position = begin
}
func (iterator *Iterator) End() {
iterator.node = nil
iterator.position = end
}
func (iterator *Iterator) First() bool {
iterator.Begin()
return iterator.Next()
}
func (iterator *Iterator) Last() bool {
iterator.End()
return iterator.Prev()
}