- 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
- 159 lines of Go from the credited upstream file 555C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks 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 "time"8)9 1011type node55 struct {12 lr [2]*node5513 priority uint14 key, end int15 up bool16}17 18func (o *node55) cmp(b int) int {19 switch {20 case b < o.key:21 return 022 case b > o.key:23 return 124 default:25 return -126 }27}28 29func (o *node55) rotate(d int) *node55 {30 x := o.lr[d^1]31 o.lr[d^1] = x.lr[d]32 x.lr[d] = o33 return x34}35 36type treap55 struct {37 rd uint38 root *node5539}40 41func (t *treap55) fastRand() uint {42 t.rd ^= t.rd << 1343 t.rd ^= t.rd >> 1744 t.rd ^= t.rd << 545 return t.rd46}47 48func (t *treap55) _put(o *node55, key, end int, up bool) *node55 {49 if o == nil {50 return &node55{priority: t.fastRand(), key: key, end: end, up: up}51 }52 if d := o.cmp(key); d >= 0 {53 o.lr[d] = t._put(o.lr[d], key, end, up)54 if o.lr[d].priority > o.priority {55 o = o.rotate(d ^ 1)56 }57 }58 return o59}60 61func (t *treap55) put(key, end int, up bool) { t.root = t._put(t.root, key, end, up) }62 63func (t *treap55) _delete(o *node55, key int) *node55 {64 if o == nil {65 return nil66 }67 if d := o.cmp(key); d >= 0 {68 o.lr[d] = t._delete(o.lr[d], key)69 } else {70 if o.lr[1] == nil {71 return o.lr[0]72 }73 if o.lr[0] == nil {74 return o.lr[1]75 }76 d = 077 if o.lr[0].priority > o.lr[1].priority {78 d = 179 }80 o = o.rotate(d)81 o.lr[d] = t._delete(o.lr[d], key)82 }83 return o84}85 86func (t *treap55) delete(key int) { t.root = t._delete(t.root, key) }87 88func (t *treap55) floor(key int) (floor *node55) {89 for o := t.root; o != nil; {90 switch c := o.cmp(key); {91 case c == 0:92 o = o.lr[0]93 case c > 0:94 floor = o95 o = o.lr[1]96 default:97 return o98 }99 }100 return101}102 103func (t *treap55) lowerBound(key int) (lb *node55) {104 for o := t.root; o != nil; {105 switch c := o.cmp(key); {106 case c == 0:107 lb = o108 o = o.lr[0]109 case c > 0:110 o = o.lr[1]111 default:112 return o113 }114 }115 return116}117 118func CF555C(_r io.Reader, _w io.Writer) {119 in := bufio.NewReader(_r)120 out := bufio.NewWriter(_w)121 defer out.Flush()122 123 t := &treap55{rd: uint(time.Now().UnixNano())/2 + 1}124 var n, q, x, y, end int125 var dir []byte126 for Fscan(in, &n, &q); q > 0; q-- {127 Fscan(in, &x, &y, &dir)128 up := dir[0] == 'U'129 if up {130 if o := t.lowerBound(x); o == nil {131 end = 0132 } else if o.key == x {133 Fprintln(out, 0)134 continue135 } else if o.up {136 end = o.end137 } else {138 end = n + 1 - o.key139 }140 Fprintln(out, y-end)141 } else {142 if o := t.floor(x); o == nil {143 end = 0144 } else if o.key == x {145 Fprintln(out, 0)146 continue147 } else if o.up {148 end = o.key149 } else {150 end = o.end151 }152 Fprintln(out, x-end)153 }154 t.put(x, end, up)155 }156}157 158159