- 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
- 72 lines of Go from the credited upstream file 1285E.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 "sort"8)9 1011func CF1285E(_r io.Reader, _w io.Writer) {12 in := bufio.NewReader(_r)13 out := bufio.NewWriter(_w)14 defer out.Flush()15 max := func(a, b int) int {16 if a > b {17 return a18 }19 return b20 }21 type seg struct{ l, r int64 }22 23 var T, n int24 for Fscan(in, &T); T > 0; T-- {25 Fscan(in, &n)26 a := make([]seg, n)27 es := make([]int64, 0, n*2)28 for i := range a {29 Fscan(in, &a[i].l, &a[i].r)30 a[i].l += 1e931 a[i].r += 1e932 es = append(es, a[i].l<<1, a[i].r<<1|1)33 }34 sort.Slice(es, func(i, j int) bool { return es[i] < es[j] })35 36 var ans, dup, mxDup int37 nonDupSeg := []seg{}38 l := int64(-1)39 for _, e := range es {40 if e&1 > 0 {41 dup--42 if dup == 1 {43 l = e >> 144 } else if dup == 0 {45 l = -146 ans++47 }48 } else {49 if dup == 1 && l >= 0 {50 nonDupSeg = append(nonDupSeg, seg{l, e >> 1})51 }52 dup++53 mxDup = max(mxDup, dup)54 }55 }56 if mxDup == 1 { 57 Fprintln(out, n-1)58 continue59 }60 61 mx := 062 for _, p := range a {63 i := sort.Search(len(nonDupSeg), func(i int) bool { return nonDupSeg[i].l >= p.l })64 j := sort.Search(len(nonDupSeg), func(i int) bool { return nonDupSeg[i].l >= p.r })65 mx = max(mx, j-i)66 }67 Fprintln(out, ans+mx)68 }69}70 7172