- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 147 lines of Go from the credited upstream file 1249D2.go.
- The implementation visibly relies on sequence storage.
- 1 loop block detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910type node49 struct {11 lr [2]*node4912 priority uint13 key int14 ids []int15}16 17func (o *node49) cmp(b int) int {18 switch {19 case b < o.key:20 return 021 case b > o.key:22 return 123 default:24 return -125 }26}27 28func (o *node49) rotate(d int) *node49 {29 x := o.lr[d^1]30 o.lr[d^1] = x.lr[d]31 x.lr[d] = o32 return x33}34 35type treap49 struct {36 rd uint37 root *node4938}39 40func (t *treap49) fastRand() uint {41 t.rd ^= t.rd << 1342 t.rd ^= t.rd >> 1743 t.rd ^= t.rd << 544 return t.rd45}46 47func (t *treap49) _put(o *node49, key, id int) *node49 {48 if o == nil {49 return &node49{priority: t.fastRand(), key: key, ids: []int{id}}50 }51 if d := o.cmp(key); d >= 0 {52 o.lr[d] = t._put(o.lr[d], key, id)53 if o.lr[d].priority > o.priority {54 o = o.rotate(d ^ 1)55 }56 } else {57 o.ids = append(o.ids, id)58 }59 return o60}61 62func (t *treap49) put(key, id int) { t.root = t._put(t.root, key, id) }63 64func (t *treap49) _delete(o *node49, key int) *node49 {65 if o == nil {66 return nil67 }68 if d := o.cmp(key); d >= 0 {69 o.lr[d] = t._delete(o.lr[d], key)70 } else {71 if len(o.ids) > 1 {72 o.ids = o.ids[:len(o.ids)-1]73 } else {74 if o.lr[1] == nil {75 return o.lr[0]76 }77 if o.lr[0] == nil {78 return o.lr[1]79 }80 d = 081 if o.lr[0].priority > o.lr[1].priority {82 d = 183 }84 o = o.rotate(d)85 o.lr[d] = t._delete(o.lr[d], key)86 }87 }88 return o89}90 91func (t *treap49) delete(key int) { t.root = t._delete(t.root, key) }92 93func (t *treap49) min() (min *node49) {94 for o := t.root; o != nil; o = o.lr[0] {95 min = o96 }97 return98}99 100func (t *treap49) max() (max *node49) {101 for o := t.root; o != nil; o = o.lr[1] {102 max = o103 }104 return105}106 107func CF1249D2(_r io.Reader, out io.Writer) {108 in := bufio.NewReader(_r)109 var n, k, l, r, sz int110 type pair struct{ v, i int }111 rs := make([][]pair, 2e5+1)112 Fscan(in, &n, &k)113 for i := 1; i <= n; i++ {114 Fscan(in, &l, &r)115 rs[l] = append(rs[l], pair{r, i})116 }117 118 ans := []interface{}{}119 t := &treap49{rd: 1}120 for l, rs := range rs {121 for {122 o := t.min()123 if o == nil || o.key >= l {124 break125 }126 t.delete(o.key)127 sz--128 }129 for _, p := range rs {130 t.put(p.v, p.i)131 sz++132 }133 for ; sz > k; sz-- {134 o := t.max()135 ans = append(ans, o.ids[len(o.ids)-1])136 o.ids = o.ids[:len(o.ids)-1]137 if len(o.ids) == 0 {138 t.delete(o.key)139 }140 }141 }142 Fprintln(out, len(ans))143 Fprintln(out, ans...)144}145 146147