- 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
- 91 lines of Go from the credited upstream file 1380F.go.
- The implementation visibly relies on sequence storage, ordered lookup.
- 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 9const m1380 = 99824435310 11var s1380 []byte12 13type seg1380 []struct {14 l, r int15 mat [2][2]int6416}17 18func (t seg1380) set(o, i int) {19 t[o].mat = [2][2]int64{{int64(s1380[i] + 1), 1}}20 if s1380[i-1] == 1 {21 t[o].mat[1][0] = int64(9 - s1380[i])22 }23}24 25func (seg1380) mul(a, b [2][2]int64) (c [2][2]int64) {26 for i := 0; i < 2; i++ {27 for j := 0; j < 2; j++ {28 for k := 0; k < 2; k++ {29 c[i][j] += a[i][k] * b[k][j]30 }31 c[i][j] %= m138032 }33 }34 return35}36 37func (t seg1380) maintain(o int) { t[o].mat = t.mul(t[o<<1].mat, t[o<<1|1].mat) }38 39func (t seg1380) build(o, l, r int) {40 t[o].l, t[o].r = l, r41 if l == r {42 t.set(o, l)43 return44 }45 m := (l + r) >> 146 t.build(o<<1, l, m)47 t.build(o<<1|1, m+1, r)48 t.maintain(o)49}50 51func (t seg1380) update(o, i int) {52 if t[o].l == t[o].r {53 t.set(o, i)54 return55 }56 if i <= (t[o].l+t[o].r)>>1 {57 t.update(o<<1, i)58 } else {59 t.update(o<<1|1, i)60 }61 t.maintain(o)62}63 6465func CF1380F(_r io.Reader, _w io.Writer) {66 in := bufio.NewReader(_r)67 out := bufio.NewWriter(_w)68 defer out.Flush()69 70 var n, q, i int71 var d byte72 Fscan(in, &n, &q, &s1380)73 for i := range s1380 {74 s1380[i] &= 1575 }76 s1380 = append([]byte{0}, s1380...)77 t := make(seg1380, 4*n)78 t.build(1, 1, n)79 for ; q > 0; q-- {80 Fscan(in, &i, &d)81 s1380[i] = d82 if i < n {83 t.update(1, i+1)84 }85 t.update(1, i)86 Fprintln(out, t[1].mat[0][0])87 }88}89 9091