- 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
- 65 lines of Go from the credited upstream file 2064D.go.
- The implementation visibly relies on sequence storage.
- 1 loop block 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/bits"8 "slices"9)10 1112func cf2064D(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 var T, n, q, x int16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n, &q)18 a := make([]int, n)19 pre := [31]int{}20 for i := range pre {21 pre[i] = -122 }23 pres := make([][31]int, n+1)24 for i := range a {25 Fscan(in, &a[i])26 m := bits.Len(uint(a[i]))27 pres[i] = pre28 pre[m] = i29 }30 pres[n] = pre31 s := make([]int, n+1)32 for i := n - 1; i >= 0; i-- {33 s[i] = s[i+1] ^ a[i]34 }35 36 for range q {37 Fscan(in, &x)38 oriX := x39 cur, preM, maxP := n, 31, -140 for {41 m := bits.Len(uint(x))42 if m+1 < preM {43 maxP = max(maxP, slices.Max(pres[cur][m+1:preM]))44 }45 p := pres[cur][m]46 if p <= maxP {47 Fprint(out, n-1-maxP, " ")48 break49 }50 x = oriX ^ s[p+1]51 if x < a[p] {52 Fprint(out, n-1-p, " ")53 break54 }55 x ^= a[p]56 preM = m + 157 cur = p58 }59 }60 Fprintln(out)61 }62}63 6465