- 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
- 90 lines of Go from the credited upstream file 1771F.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 "bufio"5 . "fmt"6 "io"7 "math/rand"8 "runtime/debug"9 "time"10)11 1213var null = &node71{}14 15func init() {16 debug.SetGCPercent(-1)17 null.lo = null18 null.ro = null19}20 21type node71 struct {22 lo, ro *node7123 xorS int24}25 26func (o node71) modify(l, r, i int, v int) *node71 {27 if l == r {28 o.xorS ^= v29 return &o30 }31 m := (l + r) >> 132 if i <= m {33 if o.lo == null {34 o.lo = &node71{lo: null, ro: null}35 }36 o.lo = o.lo.modify(l, m, i, v)37 } else {38 if o.ro == null {39 o.ro = &node71{lo: null, ro: null}40 }41 o.ro = o.ro.modify(m+1, r, i, v)42 }43 o.xorS = o.lo.xorS ^ o.ro.xorS44 return &o45}46 47func (o *node71) query(old *node71, l, r int) int {48 if l == r {49 return l50 }51 m := (l + r) >> 152 if o.lo.xorS != old.lo.xorS {53 return o.lo.query(old.lo, l, m)54 }55 return o.ro.query(old.ro, m+1, r)56}57 58func cf1771F(_r io.Reader, _w io.Writer) {59 in := bufio.NewReader(_r)60 out := bufio.NewWriter(_w)61 defer out.Flush()62 rand.Seed(time.Now().UnixNano())63 const R int = 1e9 + 164 65 var n, v, q, l, r, ans int66 Fscan(in, &n)67 t := make([]*node71, n+1)68 t[0] = &node71{lo: null, ro: null}69 mp := make(map[int]int, n)70 for i := 1; i <= n; i++ {71 Fscan(in, &v)72 if _, ok := mp[v]; !ok {73 mp[v] = rand.Int()74 }75 t[i] = t[i-1].modify(0, R, v, mp[v])76 }77 for Fscan(in, &q); q > 0; q-- {78 Fscan(in, &l, &r)79 l ^= ans80 r ^= ans81 ans = t[r].query(t[l-1], 0, R)82 if ans > 1e9 {83 ans = 084 }85 Fprintln(out, ans)86 }87}88 8990