- 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
- 83 lines of Go from the credited upstream file 2093G.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)7 89type node93 struct {10 son [2]*node9311 cnt int12}13 14type trie93 struct{ root *node93 }15 16const trieBitLen93 = 3017 18func (t *trie93) put(v int) *node93 {19 o := t.root20 for i := trieBitLen93 - 1; i >= 0; i-- {21 b := v >> i & 122 if o.son[b] == nil {23 o.son[b] = &node93{}24 }25 o = o.son[b]26 o.cnt++27 }28 return o29}30 31func (t *trie93) del(v int) *node93 {32 o := t.root33 for i := trieBitLen93 - 1; i >= 0; i-- {34 o = o.son[v>>i&1]35 o.cnt--36 }37 return o38}39 40func (t *trie93) maxXor(v int) (ans int) {41 o := t.root42 for i := trieBitLen93 - 1; i >= 0; i-- {43 b := v >> i & 144 if o.son[b^1] != nil && o.son[b^1].cnt > 0 {45 ans |= 1 << i46 b ^= 147 }48 o = o.son[b]49 }50 return51}52 53func cf2093G(in io.Reader, out io.Writer) {54 var T, n, k int55 for Fscan(in, &T); T > 0; T-- {56 Fscan(in, &n, &k)57 a := make([]int, n)58 for i := range a {59 Fscan(in, &a[i])60 }61 if k == 0 {62 Fprintln(out, 1)63 continue64 }65 t := &trie93{&node93{}}66 ans, left := n+1, 067 for i, v := range a {68 t.put(v)69 for t.maxXor(v) >= k {70 ans = min(ans, i-left+1)71 t.del(a[left])72 left++73 }74 }75 if ans > n {76 ans = -177 }78 Fprintln(out, ans)79 }80}81 8283