- 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 301D.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 CF301D(_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, l, r int16 Fscan(in, &n, &q)17 pos := make([]int, n+1)18 for i := 1; i <= n; i++ {19 Fscan(in, &r)20 pos[r] = i21 }22 targetPos := make([][]int, n+1)23 for i := 1; i <= n; i++ {24 for j := i; j <= n; j += i {25 l, r := pos[i], pos[j]26 if l > r {27 l, r = r, l28 }29 targetPos[l] = append(targetPos[l], r)30 }31 }32 type query struct{ r, i int }33 qs := make([][]query, n+1)34 for i := 0; i < q; i++ {35 Fscan(in, &l, &r)36 qs[l] = append(qs[l], query{r, i})37 }38 ans := make([]int, q)39 tree := make([]int, n+1)40 for i := n; i > 0; i-- {41 for _, r := range targetPos[i] {42 for ; r <= n; r += r & -r {43 tree[r]++44 }45 }46 for _, q := range qs[i] {47 for r := q.r; r > 0; r &= r - 1 {48 ans[q.i] += tree[r]49 }50 }51 }52 for _, v := range ans {53 Fprintln(out, v)54 }55}56 5758