- 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
- 86 lines of Go from the credited upstream file 1983F.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 . "fmt"5 "io"6 "sort"7)8 910const w83 = 3111 12var nodes83 [1e5 * (w83 + 3 - 17)]node8313var cur83 uint14 15type node83 struct {16 son [2]*node8317 mx int18}19 20func newNode83() *node83 {21 cur83++22 nodes83[cur83] = node83{}23 return &nodes83[cur83]24}25 26type trie83 struct{ root *node83 }27 28func (t *trie83) put(v, idx int) *node83 {29 o := t.root30 for i := w83 - 1; i >= 0; i-- {31 b := v >> i & 132 if o.son[b] == nil {33 o.son[b] = newNode83()34 }35 o = o.son[b]36 o.mx = idx37 }38 return o39}40 41func (t *trie83) maxIdx(v, high int) int {42 high++43 res := -144 o := t.root45 for i := w83 - 1; i >= 0; i-- {46 b := v >> i & 147 if high>>i&1 > 0 {48 if o.son[b] != nil {49 res = max(res, o.son[b].mx)50 }51 b ^= 152 }53 if o.son[b] == nil {54 break55 }56 o = o.son[b]57 }58 return res59}60 61func cf1983F(in io.Reader, out io.Writer) {62 var T, n, k int63 for Fscan(in, &T); T > 0; T-- {64 Fscan(in, &n, &k)65 a := make([]int, n)66 for i := range a {67 Fscan(in, &a[i])68 }69 ans := sort.Search(1<<w83, func(high int) bool {70 cur83 = 071 t := &trie83{newNode83()}72 mxI := -173 cnt := 074 for i, v := range a {75 mxI = max(mxI, t.maxIdx(v, high))76 cnt += mxI + 177 t.put(v, i)78 }79 return cnt >= k80 })81 Fprintln(out, ans)82 }83}84 8586