- 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
- 104 lines of Go from the credited upstream file 593E.go.
- The implementation visibly relies on sequence storage, ordered lookup.
- 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 910const mod93 = 1_000_000_00711 12type matrix93 [][]int13 14func newMatrix93(n, m int) matrix93 {15 a := make(matrix93, n)16 for i := range a {17 a[i] = make([]int, m)18 }19 return a20}21 22func (a matrix93) mul(b matrix93) matrix93 {23 c := newMatrix93(len(a), len(b[0]))24 for i, row := range a {25 for k, x := range row {26 if x == 0 {27 continue28 }29 for j, y := range b[k] {30 c[i][j] = (c[i][j] + x*y) % mod9331 }32 }33 }34 return c35}36 37func (a matrix93) powMul(n int, f0 matrix93) matrix93 {38 res := f039 for ; n > 0; n /= 2 {40 if n%2 > 0 {41 res = a.mul(res)42 }43 a = a.mul(a)44 }45 return res46}47 48func cf593E(in io.Reader, _w io.Writer) {49 out := bufio.NewWriter(_w)50 defer out.Flush()51 var n, m, q, tp, x, y, t int52 Fscan(in, &n, &m, &q)53 54 size := n * m55 M := newMatrix93(size, size)56 set := func(x, y, val int) {57 i := x*m + y58 M[i][i] = val59 if y > 0 {60 M[i][i-1] = val61 }62 if y < m-1 {63 M[i][i+1] = val64 }65 if x > 0 {66 M[i][i-m] = val67 }68 if x < n-1 {69 M[i][i+m] = val70 }71 }72 73 for i := range n {74 for j := range m {75 set(i, j, 1)76 }77 }78 79 f := newMatrix93(size, 1)80 f[0][0] = 181 82 pre := 183 for range q {84 Fscan(in, &tp, &x, &y, &t)85 x--86 y--87 88 f = M.powMul(t-pre, f)89 pre = t90 91 i := x*m + y92 if tp == 1 {93 Fprintln(out, f[i][0])94 } else if tp == 2 {95 f[i][0] = 096 set(x, y, 0)97 } else {98 set(x, y, 1)99 }100 }101}102 103104