- 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 2123G.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 cf2123G(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 gcd := func(a, b int) int {14 for a != 0 {15 a, b = b%a, a16 }17 return b18 }19 const mx = 50000120 divisors := [mx][]int32{}21 for i := int32(1); i < mx; i++ {22 for j := i; j < mx; j += i {23 divisors[j] = append(divisors[j], i)24 }25 }26 27 var T, n, m, q, op, j, x int28 pos := [mx]int{}29 for Fscan(in, &T); T > 0; T-- {30 Fscan(in, &n, &m, &q)31 a := make([]int, n)32 for i := range a {33 Fscan(in, &a[i])34 }35 36 ds := divisors[m]37 cnt := make([]int, len(ds))38 for i, d := range ds {39 pos[d] = i40 d := int(d)41 for j := range n - 1 {42 if a[j]%d > a[j+1]%d {43 cnt[i]++44 }45 }46 }47 48 for range q {49 Fscan(in, &op, &j)50 if op == 1 {51 j--52 Fscan(in, &x)53 for i, d := range ds {54 d := int(d)55 if j > 0 && a[j-1]%d > a[j]%d {56 cnt[i]--57 }58 if j > 0 && a[j-1]%d > x%d {59 cnt[i]++60 }61 if j < n-1 && a[j]%d > a[j+1]%d {62 cnt[i]--63 }64 if j < n-1 && x%d > a[j+1]%d {65 cnt[i]++66 }67 }68 a[j] = x69 } else {70 g := gcd(j, m)71 if cnt[pos[g]]*g+a[n-1]%g < m {72 Fprintln(out, "YES")73 } else {74 Fprintln(out, "NO")75 }76 }77 }78 }79}80 8182