- 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
- 84 lines of Go from the credited upstream file 2071D2.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 . "fmt"5 "io"6)7 89func cf2071D2(in io.Reader, out io.Writer) {10 var T, n, l, r int11 for Fscan(in, &T); T > 0; T-- {12 Fscan(in, &n, &l, &r)13 a := make([]int, n*2+3)14 pre := make([]int, n*2+3)15 for i := 1; i <= n; i++ {16 Fscan(in, &a[i])17 pre[i] = pre[i-1] + a[i]18 }19 if n%2 == 0 {20 n++21 a[n] = pre[n/2] % 222 pre[n] = pre[n-1] + a[n]23 }24 for i := n + 1; i <= n*2; i++ {25 a[i] = pre[i/2] % 226 pre[i] = pre[i-1] + a[i]27 }28 29 even := make([]int, n*2+1)30 for i := 1; i <= n*2; i++ {31 even[i] = even[i-1]32 if i%2 == 0 {33 even[i] += a[i]34 }35 }36 37 f := func(m int) (res int) {38 for {39 if m <= n*2 {40 res ^= a[m]41 break42 }43 res ^= pre[n] % 244 m /= 245 if m%2 > 0 {46 break47 }48 }49 return50 }51 var sum func(int) (int, int)52 sum = func(m int) (int, int) {53 if m <= n*2 {54 return even[m], pre[m] - even[m]55 }56 eve := even[n*2-1]57 odd := pre[n*2-1] - eve58 if m%2 == 0 {59 m++60 odd -= f(m)61 }62 if m/2%2 > 0 {63 m++64 eve -= f(m)65 m++66 odd -= f(m)67 }68 e, _ := sum(m / 2)69 e -= even[n]70 both := e71 if pre[n]%2 > 0 {72 both = m/2 - n + 1 - e73 }74 return eve + both, odd + both75 }76 77 re, ro := sum(r)78 le, lo := sum(l - 1)79 Fprintln(out, re+ro-le-lo)80 }81}82 8384