- 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
- 57 lines of Go from the credited upstream file 13E.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)8 910func CF13E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, q, op, p, v, last int3216 Fscan(in, &n, &q)17 a := make([]int32, n)18 for i := range a {19 Fscan(in, &a[i])20 }21 const B = 50022 type pair struct{ last, jump int32 }23 data := make([]pair, n)24 f := func(i int32) {25 if i+a[i] >= n || i/B != (i+a[i])/B {26 data[i] = pair{i, 0}27 } else {28 data[i] = data[i+a[i]]29 data[i].jump++30 }31 }32 for i := n - 1; i >= 0; i-- {33 f(i)34 }35 36 for ; q > 0; q-- {37 Fscan(in, &op, &p)38 p--39 if op == 0 {40 Fscan(in, &v)41 a[p] = v42 for i := p; i >= p-p%B; i-- {43 f(i)44 }45 } else {46 jump := int32(0)47 for ; p < n; p = last + a[last] {48 jump += data[p].jump + 149 last = data[p].last50 }51 Fprintln(out, last+1, jump)52 }53 }54}55 5657