- 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
- 186 lines of Go from the credited upstream file 527C_treap.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)8 9var _x527C = uint(1)10 11func fastRand527C() uint {12 _x527C ^= _x527C << 1313 _x527C ^= _x527C >> 1714 _x527C ^= _x527C << 515 return _x527C16}17 18type node527C struct {19 lr [2]*node527C20 priority uint21 msz int22 key int23 value int24}25 26func (o *node527C) pushUp() {27 msz := o.value28 if ol := o.lr[0]; ol != nil {29 msz += ol.msz30 }31 if or := o.lr[1]; or != nil {32 msz += or.msz33 }34 o.msz = msz35}36 37type treap527C struct {38 root *node527C39 comparator func(a, b int) int40}41 42func newTreap527C() *treap527C {43 return &treap527C{comparator: func(a, b int) int {44 if a < b {45 return 046 }47 if a > b {48 return 149 }50 return -151 }}52}53 54func (t *treap527C) rotate(o *node527C, d int) *node527C {55 x := o.lr[d^1]56 o.lr[d^1] = x.lr[d]57 x.lr[d] = o58 x.msz = o.msz59 o.pushUp()60 return x61}62 63func (t *treap527C) _put(o *node527C, key int) *node527C {64 if o == nil {65 return &node527C{priority: fastRand527C(), msz: 1, key: key, value: 1}66 }67 if cmp := t.comparator(key, o.key); cmp >= 0 {68 o.lr[cmp] = t._put(o.lr[cmp], key)69 if o.lr[cmp].priority > o.priority {70 o = t.rotate(o, cmp^1)71 }72 } else {73 o.value++74 }75 o.pushUp()76 return o77}78 79func (t *treap527C) put(key int) { t.root = t._put(t.root, key) }80 81func (t *treap527C) _delete(o *node527C, key int) *node527C {82 if o == nil {83 return nil84 }85 if cmp := t.comparator(key, o.key); cmp >= 0 {86 o.lr[cmp] = t._delete(o.lr[cmp], key)87 } else {88 if o.value > 1 {89 o.value--90 } else {91 if o.lr[1] == nil {92 return o.lr[0]93 }94 if o.lr[0] == nil {95 return o.lr[1]96 }97 cmp2 := 098 if o.lr[0].priority > o.lr[1].priority {99 cmp2 = 1100 }101 o = t.rotate(o, cmp2)102 o.lr[cmp2] = t._delete(o.lr[cmp2], key)103 }104 }105 o.pushUp()106 return o107}108 109func (t *treap527C) delete(key int) { t.root = t._delete(t.root, key) }110 111func (t *treap527C) floor(key int) (floor *node527C) {112 for o := t.root; o != nil; {113 switch cmp := t.comparator(key, o.key); {114 case cmp == 0:115 o = o.lr[0]116 case cmp > 0:117 floor = o118 o = o.lr[1]119 default:120 return o121 }122 }123 return124}125 126func (t *treap527C) next(key int) (next *node527C) {127 for o := t.root; o != nil; {128 if cmp := t.comparator(key, o.key); cmp != 0 {129 o = o.lr[1]130 } else {131 next = o132 o = o.lr[0]133 }134 }135 return136}137 138func (t *treap527C) max() (max *node527C) {139 for o := t.root; o != nil; o = o.lr[1] {140 max = o141 }142 return143}144 145146func Sol527CTreap(reader io.Reader, writer io.Writer) {147 cut := func(t, mt *treap527C, mid int) {148 o := t.floor(mid)149 l := o.key150 r := t.next(l).key151 mt.delete(r - l)152 mt.put(r - mid)153 mt.put(mid - l)154 t.put(mid)155 }156 157 in := bufio.NewReader(reader)158 out := bufio.NewWriter(writer)159 defer out.Flush()160 161 w, h, mw, mh := newTreap527C(), newTreap527C(), newTreap527C(), newTreap527C()162 var w0, h0, n int163 Fscan(in, &w0, &h0, &n)164 w.put(0)165 w.put(w0)166 mw.put(w0)167 h.put(0)168 h.put(h0)169 mh.put(h0)170 for ; n > 0; n-- {171 var op string172 var mid int173 Fscan(in, &op, &mid)174 if op[0] == 'V' {175 cut(w, mw, mid)176 } else {177 cut(h, mh, mid)178 }179 Fprintln(out, int64(mw.max().key)*int64(mh.max().key))180 }181}182 183184185186