- 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
- 82 lines of Go from the credited upstream file 1436E.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 "math/bits"8)9 1011type seg36 []struct{ l, r, min int }12 13func (t seg36) build(o, l, r int) {14 t[o].l, t[o].r = l, r15 if l == r {16 return17 }18 m := (l + r) >> 119 t.build(o<<1, l, m)20 t.build(o<<1|1, m+1, r)21}22 23func (t seg36) update(o, i, val int) {24 if t[o].l == t[o].r {25 t[o].min = val26 return27 }28 m := (t[o].l + t[o].r) >> 129 if i <= m {30 t.update(o<<1, i, val)31 } else {32 t.update(o<<1|1, i, val)33 }34 t[o].min = min(t[o<<1].min, t[o<<1|1].min)35}36 37func (t seg36) preMin(o, r int) int {38 if t[o].r <= r {39 return t[o].min40 }41 m := (t[o].l + t[o].r) >> 142 if r <= m {43 return t.preMin(o<<1, r)44 }45 return min(t[o<<1].min, t.preMin(o<<1|1, r))46}47 48func cf1436E(_r io.Reader, out io.Writer) {49 in := bufio.NewReader(_r)50 var n, v int51 Fscan(in, &n)52 has := make([]bool, n+3)53 last := make([]int, n+2)54 t := make(seg36, 2<<bits.Len(uint(n-1)))55 t.build(1, 1, n)56 for i := 1; i <= n; i++ {57 Fscan(in, &v)58 if v == 1 {59 has[2] = true60 } else {61 has[1] = true62 if t.preMin(1, v-1) > last[v] {63 has[v] = true64 }65 last[v] = i66 }67 t.update(1, v, i)68 }69 for v := 2; v <= n+1; v++ {70 if t.preMin(1, v-1) > last[v] {71 has[v] = true72 }73 }74 mex := 175 for has[mex] {76 mex++77 }78 Fprint(out, mex)79}80 8182