- 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
- 153 lines of Go from the credited upstream file 960F.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 "time"8)9 1011type node60 struct {12 lr [2]*node6013 priority uint14 key, val int15}16 17func (o *node60) 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 *node60) rotate(d int) *node60 {29 x := o.lr[d^1]30 o.lr[d^1] = x.lr[d]31 x.lr[d] = o32 return x33}34 35type treap60 struct {36 rd uint37 root *node6038}39 40func (t *treap60) fastRand() uint {41 t.rd ^= t.rd << 1342 t.rd ^= t.rd >> 1743 t.rd ^= t.rd << 544 return t.rd45}46 47func (t *treap60) _put(o *node60, key, val int) *node60 {48 if o == nil {49 return &node60{priority: t.fastRand(), key: key, val: val}50 }51 if d := o.cmp(key); d >= 0 {52 o.lr[d] = t._put(o.lr[d], key, val)53 if o.lr[d].priority > o.priority {54 o = o.rotate(d ^ 1)55 }56 }57 return o58}59 60func (t *treap60) put(key, val int) { t.root = t._put(t.root, key, val) }61 62func (t *treap60) _delete(o *node60, key int) *node60 {63 if o == nil {64 return nil65 }66 if d := o.cmp(key); d >= 0 {67 o.lr[d] = t._delete(o.lr[d], key)68 } else {69 if o.lr[1] == nil {70 return o.lr[0]71 }72 if o.lr[0] == nil {73 return o.lr[1]74 }75 d = 076 if o.lr[0].priority > o.lr[1].priority {77 d = 178 }79 o = o.rotate(d)80 o.lr[d] = t._delete(o.lr[d], key)81 }82 return o83}84 85func (t *treap60) delete(key int) { t.root = t._delete(t.root, key) }86 87func (t *treap60) lowerBound(key int) (lb *node60) {88 for o := t.root; o != nil; {89 switch c := o.cmp(key); {90 case c == 0:91 lb = o92 o = o.lr[0]93 case c > 0:94 o = o.lr[1]95 default:96 return o97 }98 }99 return100}101 102func (t *treap60) prev(key int) (prev *node60) {103 for o := t.root; o != nil; {104 if o.cmp(key) <= 0 {105 o = o.lr[0]106 } else {107 prev = o108 o = o.lr[1]109 }110 }111 return112}113 114func CF960F(_r io.Reader, out io.Writer) {115 in := bufio.NewReader(_r)116 var n, m, v, w, wt, ans int117 Fscan(in, &n, &m)118 ts := make([]*treap60, n)119 rd := uint(time.Now().UnixNano())/2 + 1120 for i := range ts {121 ts[i] = &treap60{rd: rd}122 }123 for ; m > 0; m-- {124 Fscan(in, &v, &w, &wt)125 v--126 w--127 res := 1128 if o := ts[v].prev(wt + 1); o != nil {129 res = o.val + 1130 }131 if res > ans {132 ans = res133 }134 for {135 o := ts[w].lowerBound(wt)136 if o == nil || o.val > res {137 break138 }139 ts[w].delete(o.key)140 }141 if o := ts[w].lowerBound(wt); o != nil && o.key == wt {142 continue143 }144 if o := ts[w].prev(wt); o != nil && o.val >= res {145 continue146 }147 ts[w].put(wt, res)148 }149 Fprint(out, ans)150}151 152153