- 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
- 168 lines of Go from the credited upstream file 1702F.go.
- The implementation keeps its working state in language-native values and containers.
- 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 "math/bits"8 "time"9)10 1112type node02 struct {13 lr [2]*node0214 priority uint15 key int16 keyCnt int17 subCnt int18}19 20func (o *node02) size() int {21 if o != nil {22 return o.subCnt23 }24 return 025}26 27func (o *node02) maintain() {28 o.subCnt = o.keyCnt + o.lr[0].size() + o.lr[1].size()29}30 31func (o *node02) rotate(d int) *node02 {32 x := o.lr[d^1]33 o.lr[d^1] = x.lr[d]34 x.lr[d] = o35 o.maintain()36 x.maintain()37 return x38}39 40type treap02 struct {41 rd uint42 root *node0243}44 45func (t *treap02) fastRand() uint {46 t.rd ^= t.rd << 1347 t.rd ^= t.rd >> 1748 t.rd ^= t.rd << 549 return t.rd50}51 52func (t *treap02) size() int { return t.root.size() }53 54func (t *treap02) _put(o *node02, key, c int) *node02 {55 if o == nil {56 o = &node02{priority: t.fastRand(), key: key, keyCnt: c}57 } else if d := o.cmp(key); d >= 0 {58 o.lr[d] = t._put(o.lr[d], key, c)59 if o.lr[d].priority > o.priority {60 o = o.rotate(d ^ 1)61 }62 } else {63 o.keyCnt += c64 }65 o.maintain()66 return o67}68 69func (t *treap02) put(key, c int) { t.root = t._put(t.root, key, c) }70 71func (t *treap02) _delete(o *node02, key, c int) *node02 {72 if o == nil {73 return nil74 }75 if d := o.cmp(key); d >= 0 {76 o.lr[d] = t._delete(o.lr[d], key, c)77 } else {78 if o.keyCnt > c {79 o.keyCnt -= c80 } else {81 if o.lr[1] == nil {82 return o.lr[0]83 }84 if o.lr[0] == nil {85 return o.lr[1]86 }87 d = 088 if o.lr[0].priority > o.lr[1].priority {89 d = 190 }91 o = o.rotate(d)92 o.lr[d] = t._delete(o.lr[d], key, c)93 }94 }95 o.maintain()96 return o97}98 99func (t *treap02) delete(key, c int) { t.root = t._delete(t.root, key, c) }100 101func newTreap() *treap02 { return &treap02{rd: uint(time.Now().UnixNano())/2 + 1} }102 103func (o *node02) cmp(a int) int {104 b := o.key105 if a == b {106 return -1107 }108 if a < b {109 return 0110 }111 return 1112}113 114func (t *treap02) max() (max *node02) {115 for o := t.root; o != nil; o = o.lr[1] {116 max = o117 }118 return119}120 121func CF1702F(_r io.Reader, _w io.Writer) {122 in := bufio.NewReader(_r)123 out := bufio.NewWriter(_w)124 defer out.Flush()125 126 var T, n, v int127o:128 for Fscan(in, &T); T > 0; T-- {129 Fscan(in, &n)130 a := newTreap()131 for i := 0; i < n; i++ {132 Fscan(in, &v)133 a.put(v>>bits.TrailingZeros(uint(v)), 1)134 }135 b := newTreap()136 for i := 0; i < n; i++ {137 Fscan(in, &v)138 b.put(v>>bits.TrailingZeros(uint(v)), 1)139 }140 141 for a.size() > 0 {142 p := b.max()143 q := a.max()144 if p.key < q.key {145 Fprintln(out, "NO")146 continue o147 }148 if p.key == q.key {149 if p.keyCnt < q.keyCnt {150 Fprintln(out, "NO")151 continue o152 }153 a.delete(q.key, q.keyCnt)154 b.delete(p.key, p.keyCnt)155 if p.keyCnt > q.keyCnt {156 b.put(p.key>>1, p.keyCnt-q.keyCnt)157 }158 } else {159 b.delete(p.key, p.keyCnt)160 b.put(p.key>>1, p.keyCnt)161 }162 }163 Fprintln(out, "YES")164 }165}166 167168