- 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
- 105 lines of Go from the credited upstream file 914F.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"8 "math/bits"9 "slices"10)11 1213const w14 = bits.UintSize14 15type bitset14 []uint16 17func (b bitset14) flip(p int) { b[p/w14] ^= 1 << (p % w14) }18 19func (b bitset14) rsh(k int) bitset14 {20 if k == 0 {21 return b22 }23 shift, offset := k/w14, k%w1424 n := len(b)25 if shift >= n {26 return make(bitset14, n)27 }28 b = slices.Clone(b)29 lim := n - 1 - shift30 if offset == 0 {31 copy(b, b[shift:])32 } else {33 for i := 0; i < lim; i++ {34 b[i] = b[i+shift]>>offset | b[i+shift+1]<<(w14-offset)35 }36 b[lim] = b[n-1] >> offset37 }38 clear(b[lim+1:])39 return b40}41 42func (b bitset14) and(c bitset14) {43 for i, v := range c {44 b[i] &= v45 }46}47 48func (b bitset14) onesCountRange(l, r int) int {49 maskL := ^uint(0) << (l % w14)50 maskR := ^uint(0) << (r % w14)51 i := l / w1452 if i == r/w14 {53 return bits.OnesCount(b[i] & (maskL ^ maskR))54 }55 cnt1 := bits.OnesCount(b[i] & maskL)56 for i++; i < r/w14; i++ {57 cnt1 += bits.OnesCount(b[i])58 }59 return cnt1 + bits.OnesCount(b[i]&^maskR)60}61 62func cf914F(in io.Reader, _w io.Writer) {63 out := bufio.NewWriter(_w)64 defer out.Flush()65 var s, t []byte66 var q, op, l, r int67 Fscan(in, &s, &q)68 69 n := len(s)70 pos := [26]bitset14{}71 for i := range pos {72 pos[i] = make(bitset14, (n+w14-1)/w14)73 }74 for i, b := range s {75 pos[b-'a'].flip(i)76 }77 78 match := make(bitset14, (n+w14-1)/w14)79 for range q {80 Fscan(in, &op)81 if op == 1 {82 Fscan(in, &l, &t)83 l--84 pos[s[l]-'a'].flip(l)85 s[l] = t[0]86 pos[s[l]-'a'].flip(l)87 } else {88 Fscan(in, &l, &r, &t)89 if r-l+1 < len(t) {90 Fprintln(out, 0)91 continue92 }93 for i := range match {94 match[i] = math.MaxUint95 }96 for i, b := range t {97 match.and(pos[b-'a'].rsh(i))98 }99 Fprintln(out, match.onesCountRange(l-1, r-len(t)+1))100 }101 }102}103 104105