- 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
- 66 lines of Go from the credited upstream file 2143D2.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)7 89type fenwick43 []int10 11func (t fenwick43) update(i, v int) {12 for ; i < len(t); i += i & -i {13 t[i] += v14 }15}16 17func (t fenwick43) pre(i int) (res int) {18 for ; i > 0; i &= i - 1 {19 res += t[i]20 }21 return22}23 24func cf2143D2(in io.Reader, out io.Writer) {25 const mod = 1_000_000_00726 var T, n, v int27 for Fscan(in, &T); T > 0; T-- {28 Fscan(in, &n)29 row := make([]fenwick43, n+1)30 col := make([]fenwick43, n+1)31 for i := range col {32 row[i] = make(fenwick43, n+1)33 col[i] = make(fenwick43, n+1)34 }35 upd := func(x, y, v int) {36 row[x].update(y, v)37 col[y].update(x, v)38 }39 40 41 42 upd(1, 1, 1)43 for range n {44 Fscan(in, &v)45 46 47 for y := 1; y <= v; y++ {48 upd(v, y, col[y].pre(v)%mod)49 }50 51 52 for x := v + 1; x <= n; x++ {53 upd(x, v, row[x].pre(v)%mod)54 }55 }56 57 ans := 058 for _, f := range row {59 ans += f.pre(n)60 }61 Fprintln(out, ans%mod)62 }63}64 6566