- 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
- 82 lines of Go from the credited upstream file 817E.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 node17 struct {14 son [2]*node1715 cnt int16}17 18type trie17 struct{ root *node17 }19 20const trieBitLen17 = 2721 22func (t *trie17) put(v int) *node17 {23 o := t.root24 for i := trieBitLen17 - 1; i >= 0; i-- {25 b := v >> i & 126 if o.son[b] == nil {27 o.son[b] = &node17{}28 }29 o = o.son[b]30 o.cnt++31 }32 return o33}34 35func (t *trie17) del(v int) *node17 {36 o := t.root37 for i := trieBitLen17 - 1; i >= 0; i-- {38 o = o.son[v>>i&1]39 o.cnt--40 }41 return o42}43 44func (t *trie17) countLimitXOR(v, limit int) (cnt int) {45 o := t.root46 for i := trieBitLen17 - 1; i >= 0; i-- {47 b := v >> i & 148 if limit>>i&1 > 0 {49 if o.son[b] != nil {50 cnt += o.son[b].cnt51 }52 b ^= 153 }54 if o.son[b] == nil {55 return56 }57 o = o.son[b]58 }59 return60}61 62func cf817E(in io.Reader, _w io.Writer) {63 out := bufio.NewWriter(_w)64 defer out.Flush()65 var q, op, v, l int66 t := trie17{&node17{}}67 Fscan(in, &q)68 for range q {69 Fscan(in, &op, &v)70 if op == 1 {71 t.put(v)72 } else if op == 2 {73 t.del(v)74 } else {75 Fscan(in, &l)76 Fprintln(out, t.countLimitXOR(v, l))77 }78 }79}80 8182