- 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
- 155 lines of Go from the credited upstream file 1579E2.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 "time"8)9 1011type node79 struct {12 lr [2]*node7913 priority uint14 key int15 keyCnt int16 subCnt int17}18 19func (o *node79) size() int {20 if o != nil {21 return o.subCnt22 }23 return 024}25 26func (o *node79) maintain() {27 o.subCnt = o.keyCnt + o.lr[0].size() + o.lr[1].size()28}29 30func (o *node79) rotate(d int) *node79 {31 x := o.lr[d^1]32 o.lr[d^1] = x.lr[d]33 x.lr[d] = o34 o.maintain()35 x.maintain()36 return x37}38 39type treap79 struct {40 rd uint41 root *node7942}43 44func (t *treap79) fastRand() uint {45 t.rd ^= t.rd << 1346 t.rd ^= t.rd >> 1747 t.rd ^= t.rd << 548 return t.rd49}50 51func (t *treap79) size() int { return t.root.size() }52 53func (t *treap79) _put(o *node79, key int) *node79 {54 if o == nil {55 o = &node79{priority: t.fastRand(), key: key, keyCnt: 1}56 } else if d := o.cmp(key); d >= 0 {57 o.lr[d] = t._put(o.lr[d], key)58 if o.lr[d].priority > o.priority {59 o = o.rotate(d ^ 1)60 }61 } else {62 o.keyCnt++63 }64 o.maintain()65 return o66}67 68func (t *treap79) put(key int) { t.root = t._put(t.root, key) }69 70func (t *treap79) _delete(o *node79, key int) *node79 {71 if o == nil {72 return nil73 }74 if d := o.cmp(key); d >= 0 {75 o.lr[d] = t._delete(o.lr[d], key)76 } else {77 if o.keyCnt > 1 {78 o.keyCnt--79 } else {80 if o.lr[1] == nil {81 return o.lr[0]82 }83 if o.lr[0] == nil {84 return o.lr[1]85 }86 d = 087 if o.lr[0].priority > o.lr[1].priority {88 d = 189 }90 o = o.rotate(d)91 o.lr[d] = t._delete(o.lr[d], key)92 }93 }94 o.maintain()95 return o96}97 98func (t *treap79) delete(key int) { t.root = t._delete(t.root, key) }99 100func (o *node79) cmp(a int) int {101 b := o.key102 if a == b {103 return -1104 }105 if a < b {106 return 0107 }108 return 1109}110 111func (t *treap79) rank(key int) (kth int) {112 for o := t.root; o != nil; {113 switch c := o.cmp(key); {114 case c == 0:115 o = o.lr[0]116 case c > 0:117 kth += o.lr[0].size() + o.keyCnt118 o = o.lr[1]119 default:120 kth += o.lr[0].size()121 return122 }123 }124 return125}126 127func CF1579E2(_r io.Reader, _w io.Writer) {128 in := bufio.NewReader(_r)129 out := bufio.NewWriter(_w)130 defer out.Flush()131 min := func(a, b int) int {132 if a > b {133 return b134 }135 return a136 }137 138 var T, n, v int139 t := &treap79{rd: uint(time.Now().UnixNano())/2 + 1}140 for Fscan(in, &T); T > 0; T-- {141 ans := int64(0)142 Fscan(in, &n, &v)143 t.root = nil144 t.put(v)145 for i := 1; i < n; i++ {146 Fscan(in, &v)147 ans += int64(min(t.rank(v), i-t.rank(v+1)))148 t.put(v)149 }150 Fprintln(out, ans)151 }152}153 154155