- 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
- 58 lines of Go from the credited upstream file 1326E.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 data26 struct{ sum, sufMax int }12type seg26 []data2613 14func (seg26) merge(l, r data26) data26 {15 return data26{l.sum + r.sum, max(l.sufMax+r.sum, r.sufMax)}16}17 18func (t seg26) update(o, l, r, i, v int) {19 if l == r {20 t[o].sum += v21 t[o].sufMax += v22 return23 }24 m := (l + r) >> 125 if i <= m {26 t.update(o<<1, l, m, i, v)27 } else {28 t.update(o<<1|1, m+1, r, i, v)29 }30 t[o] = t.merge(t[o<<1], t[o<<1|1])31}32 33func cf1326E(in io.Reader, _w io.Writer) {34 out := bufio.NewWriter(_w)35 defer out.Flush()36 var n, v int37 Fscan(in, &n)38 pos := make([]int, n+1)39 for i := 1; i <= n; i++ {40 Fscan(in, &v)41 pos[v] = i42 }43 44 t := make(seg26, 2<<bits.Len(uint(n-1)))45 i := n + 146 for range n {47 for ; i > 1 && t[1].sufMax <= 0; {48 i--49 t.update(1, 1, n, pos[i], 1)50 }51 Fprint(out, i, " ")52 Fscan(in, &v)53 t.update(1, 1, n, v, -1)54 }55}56 5758