- 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
- 68 lines of Go from the credited upstream file 665E.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 "runtime/debug"8)9 1011func init() { debug.SetGCPercent(-1) }12 13type trieNode65 struct {14 son [2]*trieNode6515 cnt int16}17 18type trie65 struct{ root *trieNode65 }19 20func (t *trie65) put(v int) *trieNode65 {21 o := t.root22 for i := 29; i >= 0; i-- {23 b := v >> i & 124 if o.son[b] == nil {25 o.son[b] = &trieNode65{}26 }27 o = o.son[b]28 o.cnt++29 }30 return o31}32 33func (t *trie65) countLimitXOR(v, limit int) (cnt int) {34 o := t.root35 for i := 29; i >= 0; i-- {36 b := v >> i & 137 if limit>>i&1 > 0 {38 if o.son[b] != nil {39 cnt += o.son[b].cnt40 }41 b ^= 142 }43 if o.son[b] == nil {44 return45 }46 o = o.son[b]47 }48 return49}50 51func CF665E(_r io.Reader, out io.Writer) {52 in := bufio.NewReader(_r)53 var n, k, v, xor int54 Fscan(in, &n, &k)55 ans := int64(n) * int64(n+1) / 256 t := &trie65{&trieNode65{}}57 t.put(0)58 for ; n > 0; n-- {59 Fscan(in, &v)60 xor ^= v61 ans -= int64(t.countLimitXOR(xor, k))62 t.put(xor)63 }64 Fprint(out, ans)65}66 6768