- 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
- 58 lines of Go from the credited upstream file 341D.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)8 910type fenwick41 [][][2][2]int11 12func (t fenwick41) add(x, y, val int) {13 n := len(t)14 for i := x; i < n; i += i & -i {15 for j := y; j < n; j += j & -j {16 t[i][j][x&1][y&1] ^= val17 }18 }19}20 21func (t fenwick41) update(x1, y1, x2, y2, val int) {22 t.add(x1, y1, val)23 t.add(x1, y2+1, val)24 t.add(x2+1, y1, val)25 t.add(x2+1, y2+1, val)26}27 28func (t fenwick41) get(x, y int) (res int) {29 for i := x; i > 0; i &= i - 1 {30 for j := y; j > 0; j &= j - 1 {31 res ^= t[i][j][x&1][y&1]32 }33 }34 return35}36 37func cf341D(in io.Reader, _w io.Writer) {38 out := bufio.NewWriter(_w)39 defer out.Flush()40 var n, m, op, x1, y1, x2, y2, v int41 Fscan(in, &n, &m)42 t := make(fenwick41, n+1)43 for i := range t {44 t[i] = make([][2][2]int, n+1)45 }46 for range m {47 Fscan(in, &op, &x1, &y1, &x2, &y2)48 if op == 1 {49 Fprintln(out, t.get(x2, y2)^t.get(x2, y1-1)^t.get(x1-1, y2)^t.get(x1-1, y1-1))50 } else {51 Fscan(in, &v)52 t.update(x1, y1, x2, y2, v)53 }54 }55}56 5758