- 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
- 75 lines of Go from the credited upstream file 1957D.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 "math/bits"7)8 910func cf1957D(in io.Reader, out io.Writer) {11 var T, n int12 for Fscan(in, &T); T > 0; T-- {13 Fscan(in, &n)14 a := make([]int, n)15 suf := [30][2]int{}16 s := 017 for i := range a {18 Fscan(in, &a[i])19 s ^= a[i]20 for j := range suf {21 suf[j][s>>j&1]++22 }23 }24 25 ans := 026 pre := [30][2]int{}27 s = 028 for _, v := range a {29 for j := range pre {30 pre[j][s>>j&1]++31 }32 hb := bits.Len(uint(v)) - 133 ans += pre[hb][0]*suf[hb][0] + pre[hb][1]*suf[hb][1]34 s ^= v35 for j := range suf {36 suf[j][s>>j&1]--37 }38 }39 Fprintln(out, ans)40 }41}42 4344 4546func cf1957D_2(in io.Reader, out io.Writer) {47 var T, n int48 for Fscan(in, &T); T > 0; T-- {49 Fscan(in, &n)50 a := make([]int, n)51 total := [30][2]int{}52 s := 053 for i := range a {54 Fscan(in, &a[i])55 s ^= a[i]56 for j := range total {57 total[j][s>>j&1]++58 }59 }60 61 ans := 062 pre := [30][2]int{}63 s = 064 for _, v := range a {65 hb := bits.Len(uint(v)) - 166 ans += (pre[hb][0]+1)*(total[hb][0]-pre[hb][0]) + pre[hb][1]*(total[hb][1]-pre[hb][1])67 s ^= v68 for j := range pre {69 pre[j][s>>j&1]++70 }71 }72 Fprintln(out, ans)73 }74}75