- 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
- 76 lines of Go from the credited upstream file 1313D.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 . "fmt"5 "io"6 "math/bits"7 "slices"8)9 1011func cf1313D(in io.Reader, out io.Writer) {12 var n, up, l, r int13 Fscan(in, &n, &up, &up)14 type pair struct{ v, i int }15 a := make([]pair, 0, n*2)16 for i := 1; i <= n; i++ {17 Fscan(in, &l, &r)18 a = append(a, pair{l<<1 | 1, i}, pair{(r + 1) << 1, i}) 19 }20 slices.SortFunc(a, func(a, b pair) int { return a.v - b.v })21 22 f := make([]int, 1<<up)23 for i := 1; i < 1<<up; i++ {24 f[i] = -1e925 }26 idx := make([]int, up)27 for ai, p := range a {28 d := 029 if ai > 0 {30 d = p.v>>1 - a[ai-1].v>>131 }32 k := 033 if p.v&1 > 0 { 34 for i, j := range idx {35 if j == 0 {36 k = i37 idx[i] = p.i38 break39 }40 }41 for j := len(f) - 1; j >= 0; j-- {42 odd := bits.OnesCount8(uint8(j))&1 > 043 if j>>k&1 > 0 { 44 d2 := 045 if !odd { 46 d2 = d47 }48 f[j] = f[j^1<<k] + d249 } else if odd { 50 f[j] += d51 }52 }53 } else { 54 for i, j := range idx {55 if j == p.i {56 k = i57 idx[i] = 058 break59 }60 }61 for j := range f {62 if j>>k&1 > 0 { 63 f[j] = -1e964 } else if bits.OnesCount8(uint8(j))&1 > 0 {65 f[j] = max(f[j]+d, f[j|1<<k])66 } else {67 f[j] = max(f[j], f[j|1<<k]+d)68 }69 }70 }71 }72 Fprint(out, f[0])73}74 7576