- 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
- 151 lines of Go from the credited upstream file 1838D.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 node38 struct {12 lr [2]*node3813 priority uint14 key int15 subCnt int16}17 18func (o *node38) size() int {19 if o != nil {20 return o.subCnt21 }22 return 023}24 25func (o *node38) maintain() { o.subCnt = 1 + o.lr[0].size() + o.lr[1].size() }26 27func (o *node38) rotate(d int) *node38 {28 x := o.lr[d^1]29 o.lr[d^1] = x.lr[d]30 x.lr[d] = o31 o.maintain()32 x.maintain()33 return x34}35 36type treap38 struct {37 rd uint38 root *node3839}40 41func (t *treap38) fastRand() uint {42 t.rd ^= t.rd << 1343 t.rd ^= t.rd >> 1744 t.rd ^= t.rd << 545 return t.rd46}47 48func (t *treap38) size() int { return t.root.size() }49 50func (t *treap38) _put(o *node38, key int) *node38 {51 if o == nil {52 return &node38{priority: t.fastRand(), key: key, subCnt: 1}53 }54 if d := o.cmp(key); d >= 0 {55 o.lr[d] = t._put(o.lr[d], key)56 if o.lr[d].priority > o.priority {57 o = o.rotate(d ^ 1)58 }59 } else {60 61 }62 o.maintain()63 return o64}65 66func (t *treap38) put(key int) { t.root = t._put(t.root, key) }67 68func (t *treap38) _delete(o *node38, key int) *node38 {69 if o == nil {70 return nil71 }72 if d := o.cmp(key); d >= 0 {73 o.lr[d] = t._delete(o.lr[d], key)74 } else {75 if o.lr[1] == nil {76 return o.lr[0]77 }78 if o.lr[0] == nil {79 return o.lr[1]80 }81 d = 082 if o.lr[0].priority > o.lr[1].priority {83 d = 184 }85 o = o.rotate(d)86 o.lr[d] = t._delete(o.lr[d], key)87 }88 o.maintain()89 return o90}91 92func (t *treap38) delete(key int) { t.root = t._delete(t.root, key) }93 94func (o *node38) cmp(a int) int {95 b := o.key96 if a == b {97 return -198 }99 if a < b {100 return 0101 }102 return 1103}104 105func (t *treap38) min() (min int) {106 for o := t.root; o != nil; o = o.lr[0] {107 min = o.key108 }109 return110}111 112func (t *treap38) max() (max int) {113 for o := t.root; o != nil; o = o.lr[1] {114 max = o.key115 }116 return117}118 119func CF1838D(_r io.Reader, _w io.Writer) {120 in := bufio.NewReader(_r)121 out := bufio.NewWriter(_w)122 defer out.Flush()123 124 var n, q, p int125 var s []byte126 Fscan(in, &n, &q, &s)127 t := &treap38{rd: uint(time.Now().UnixNano())/2 + 1}128 for i, b := range s {129 if b%2 != byte(i%2) {130 t.put(i)131 }132 }133 for ; q > 0; q-- {134 Fscan(in, &p)135 p--136 s[p] ^= 1137 if s[p]%2 != byte(p%2) {138 t.put(p)139 } else {140 t.delete(p)141 }142 if n%2 > 0 || t.size() > 0 && t.min()%2 <= t.max()%2 {143 Fprintln(out, "NO")144 } else {145 Fprintln(out, "YES")146 }147 }148}149 150151