- 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
- 61 lines of Go from the credited upstream file 1980F2.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 "cmp"6 . "fmt"7 "io"8 "slices"9)10 1112func cf1980F2(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 var T, n, m, k int16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n, &m, &k)18 type tuple struct{ c, r, i int }19 a := make([]tuple, k+1)20 for i := range k {21 var r, c int22 Fscan(in, &r, &c)23 a = append(a, tuple{c, r, i})24 }25 a[k] = tuple{m + 1, n + 1, k}26 slices.SortFunc(a, func(a, b tuple) int { return cmp.Or(a.c-b.c, a.r-b.r) })27 28 ans := make([]int, k+1)29 sum := 030 preC := 031 s := tuple{0, 0, k}32 st := tuple{}33 for _, t := range a {34 c, r := t.c, t.r35 if r < st.c {36 continue37 }38 if r < s.c {39 st.i += (c - st.r) * (st.c - preC)40 st.c = r41 st.r = c42 } else {43 ans[s.i] -= st.i + (c-st.r)*(st.c-preC)44 ans[s.i] += (c - s.r) * (s.c - preC)45 sum += (c - s.r) * s.c46 preC = s.c47 st = tuple{s.c, c, 0}48 s = tuple{r, c, t.i}49 }50 }51 52 Fprintln(out, m*n-sum)53 for _, v := range ans[:k] {54 Fprint(out, v, " ")55 }56 Fprintln(out)57 }58}59 6061