- 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
- 89 lines of Go from the credited upstream file 2063F2.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 910func cf2063F2(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 const mod = 99824435314 pow := func(x, n int) int {15 res := 116 for n > 0 {17 if n&1 > 0 {18 res = res * x % mod19 }20 x = x * x % mod21 n >>= 122 }23 return res24 }25 26 var T, n int27 for Fscan(in, &T); T > 0; T-- {28 Fscan(in, &n)29 mx := n * 230 f := make([]int, mx+1)31 f[0] = 132 for i := 1; i <= mx; i++ {33 f[i] = f[i-1] * (i*4 - 2) % mod * pow(i+1, mod-2) % mod34 }35 36 x := make([]int, n+1)37 y := make([]int, n+1)38 a := make([]int, mx+1)39 s := make([]int, mx+1)40 fa := make([]int, mx+1)41 sz := make([]int, mx+1)42 del := make([]bool, mx+1)43 res := make([]int, n+1)44 it := 045 for i := 1; i <= n; i++ {46 Fscan(in, &x[i], &y[i])47 a[x[i]] = 148 a[y[i]] = 049 }50 51 for i := 1; i <= mx; i++ {52 if a[i] == 1 {53 fa[i] = s[it]54 it++55 s[it] = i56 } else {57 it--58 }59 }60 61 var find func(int) int62 find = func(x int) int {63 if !del[x] {64 return x65 }66 fa[x] = find(fa[x])67 return fa[x]68 }69 70 mul := 171 for i := n; i >= 1; i-- {72 res[i] = mul73 del[x[i]] = true74 k := find(x[i])75 mul = mul * pow(f[sz[x[i]]]*f[sz[k]]%mod, mod-2) % mod76 sz[k] += sz[x[i]] + 177 mul = mul * f[sz[k]] % mod78 }79 res[0] = mul80 81 for _, v := range res {82 Fprint(out, v, " ")83 }84 Fprintln(out)85 }86}87 8889