- 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
- 152 lines of Go from the credited upstream file 1690G.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)8 910type node90 struct {11 lr [2]*node9012 priority uint13 key int14}15 16func (o *node90) cmp(b int) int {17 switch {18 case b < o.key:19 return 020 case b > o.key:21 return 122 default:23 return -124 }25}26 27func (o *node90) rotate(d int) *node90 {28 x := o.lr[d^1]29 o.lr[d^1] = x.lr[d]30 x.lr[d] = o31 return x32}33 34type treap90 struct {35 rd uint36 root *node9037}38 39func (t *treap90) fastRand() uint {40 t.rd ^= t.rd << 1341 t.rd ^= t.rd >> 1742 t.rd ^= t.rd << 543 return t.rd44}45 46func (t *treap90) _put(o *node90, key int) *node90 {47 if o == nil {48 return &node90{priority: t.fastRand(), key: key}49 }50 if d := o.cmp(key); d >= 0 {51 o.lr[d] = t._put(o.lr[d], key)52 if o.lr[d].priority > o.priority {53 o = o.rotate(d ^ 1)54 }55 }56 return o57}58 59func (t *treap90) put(key int) { t.root = t._put(t.root, key) }60 61func (t *treap90) _delete(o *node90, key int) *node90 {62 if o == nil {63 return nil64 }65 if d := o.cmp(key); d >= 0 {66 o.lr[d] = t._delete(o.lr[d], key)67 } else {68 if o.lr[1] == nil {69 return o.lr[0]70 }71 if o.lr[0] == nil {72 return o.lr[1]73 }74 d = 075 if o.lr[0].priority > o.lr[1].priority {76 d = 177 }78 o = o.rotate(d)79 o.lr[d] = t._delete(o.lr[d], key)80 }81 return o82}83 84func (t *treap90) delete(key int) { t.root = t._delete(t.root, key) }85 86func (t *treap90) prev(key int) (prev *node90) {87 for o := t.root; o != nil; {88 if o.cmp(key) <= 0 {89 o = o.lr[0]90 } else {91 prev = o92 o = o.lr[1]93 }94 }95 return96}97 98func (t *treap90) next(key int) (next *node90) {99 for o := t.root; o != nil; {100 if o.cmp(key) == 0 {101 next = o102 o = o.lr[0]103 } else {104 o = o.lr[1]105 }106 }107 return108}109 110func CF1690G(_r io.Reader, _w io.Writer) {111 in := bufio.NewReader(_r)112 out := bufio.NewWriter(_w)113 defer out.Flush()114 115 var T, n, m, i, d int116 for Fscan(in, &T); T > 0; T-- {117 Fscan(in, &n, &m)118 a := make([]int, n)119 for i := range a {120 Fscan(in, &a[i])121 }122 ans := 0123 t := &treap90{rd: 1}124 mi := a[0] + 1125 for i, v := range a {126 if v < mi {127 ans++128 t.put(i)129 mi = v130 }131 }132 for ; m > 0; m-- {133 Fscan(in, &i, &d)134 i--135 v := a[i] - d136 if o := t.prev(i); o != nil && a[i] >= a[o.key] && v < a[o.key] {137 ans++138 t.put(i)139 }140 for o := t.next(i); o != nil && a[o.key] >= v; o = t.next(i) {141 ans--142 t.delete(o.key)143 }144 a[i] = v145 Fprint(out, ans, " ")146 }147 Fprintln(out)148 }149}150 151152