Approach
Sorting and greedy selection
For Codeforces 631C — Report, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 54 lines of Go from the credited upstream file 631C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 "slices"8)9 1011func cf631C(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, m, tp, r int15 Fscan(in, &n, &m)16 a := make([]int, n)17 for i := range a {18 Fscan(in, &a[i])19 }20 21 type pair struct{ tp, r int }22 st := []pair{}23 for range m {24 Fscan(in, &tp, &r)25 for len(st) > 0 && st[len(st)-1].r <= r {26 st = st[:len(st)-1]27 }28 if len(st) == 0 || st[len(st)-1].tp != tp {29 st = append(st, pair{tp, r})30 }31 }32 st = append(st, pair{})33 34 b := slices.Clone(a[:st[0].r])35 slices.Sort(b)36 for i, p := range st[:len(st)-1] {37 l, r := st[i+1].r, p.r38 k := r - l39 if p.tp == 1 {40 copy(a[l:r], b[len(b)-k:])41 b = b[:len(b)-k]42 } else {43 slices.Reverse(b[:k])44 copy(a[l:r], b[:k])45 b = b[k:]46 }47 }48 for _, v := range a {49 Fprint(out, v, " ")50 }51}52 5354