- 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
- 87 lines of Go from the credited upstream file 617E.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"8 "sort"9)10 1112func Sol617E(reader io.Reader, writer io.Writer) {13 in := bufio.NewReader(reader)14 out := bufio.NewWriter(writer)15 defer out.Flush()16 17 var n, q, k, v int18 Fscan(in, &n, &q, &k)19 sum := make([]int, n+1)20 for i := 0; i < n; i++ {21 Fscan(in, &v)22 sum[i+1] = sum[i] ^ v23 }24 25 type query struct {26 blockIdx int27 l, r, idx int28 }29 qs := make([]query, q)30 blockSize := int(math.Round(math.Sqrt(float64(n))))31 for i := range qs {32 var l, r int33 Fscan(in, &l, &r)34 qs[i] = query{l / blockSize, l, r + 1, i}35 }36 sort.Slice(qs, func(i, j int) bool {37 qi, qj := qs[i], qs[j]38 if qi.blockIdx != qj.blockIdx {39 return qi.blockIdx < qj.blockIdx40 }41 if qi.blockIdx&1 == 0 {42 return qi.r < qj.r43 }44 return qi.r > qj.r45 })46 47 l, r, _ans := 1, 1, int64(0)48 cnt := make([]int, 1<<20)49 cnt[0] = 150 update := func(idx, delta int) {51 s := sum[idx]52 tar := k ^ s53 if delta == 1 {54 _ans += int64(cnt[tar])55 cnt[s]++56 } else {57 cnt[s]--58 _ans -= int64(cnt[tar])59 }60 }61 ans := make([]int64, q)62 for _, q := range qs {63 for ; r < q.r; r++ {64 update(r, 1)65 }66 for ; l < q.l; l++ {67 update(l-1, -1)68 }69 for l > q.l {70 l--71 update(l-1, 1)72 }73 for r > q.r {74 r--75 update(r, -1)76 }77 ans[q.idx] = _ans78 }79 for _, v := range ans {80 Fprintln(out, v)81 }82}83 84858687