- 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
- 71 lines of Go from the credited upstream file 1187C.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 910 1112func CF1187C(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 type fact struct{ l, r int }17 18 var n, m, t, l, r int19 Fscan(in, &n, &m)20 fa := make([]int, 2*n)21 for i := range fa {22 fa[i] = i23 }24 var find func(int) int25 find = func(x int) int {26 if fa[x] != x {27 fa[x] = find(fa[x])28 }29 return fa[x]30 }31 merge := func(from, to int) { fa[find(from)] = find(to) }32 fs := []fact{}33 for ; m > 0; m-- {34 Fscan(in, &t, &l, &r)35 l--36 if t > 0 {37 tar := n + l38 for i := l; i < r; i++ {39 if find(i) != i {40 tar = find(i)41 break42 }43 }44 for i := l; i < r; i++ {45 merge(i, tar)46 }47 } else {48 fs = append(fs, fact{l, r})49 }50 }51o:52 for _, f := range fs {53 for i := f.l; i < f.r; i++ {54 if find(i) != find(f.l) {55 continue o56 }57 }58 Fprint(out, "NO")59 return60 }61 Fprintln(out, "YES")62 for i := 0; i < n; {63 st := i64 for ; i < n && find(i) == find(st); i++ {65 Fprint(out, n-st, " ")66 }67 }68}69 7071