- 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 1712E2.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 910type fenwick12 []int11 12func (f fenwick12) update(i, v int) {13 for ; i < len(f); i += i & -i {14 f[i] += v15 }16}17 18func (f fenwick12) pre(i int) (s int) {19 for ; i > 0; i &= i - 1 {20 s += f[i]21 }22 return23}24 25func cf1712E2(in io.Reader, _w io.Writer) {26 out := bufio.NewWriter(_w)27 defer out.Flush()28 var m, l, r int29 Fscan(in, &m)30 const mx int = 2e5 + 131 type pair struct{ r, i int }32 qs := [mx][]pair{}33 for i := range m {34 Fscan(in, &l, &r)35 qs[l] = append(qs[l], pair{r, i})36 }37 38 ans := make([]int, m)39 divisors := [mx]int{}40 t := make(fenwick12, mx)41 for l := mx - 1; l > 0; l-- {42 for j := l * 2; j < mx; j += l {43 t.update(j, divisors[j])44 divisors[j]++45 }46 for _, p := range qs[l] {47 r := p.r48 ans[p.i] = (r-l+1)*(r-l)*(r-l-1)/6 - t.pre(r) - max(r/6-(l+2)/3+1, 0) - max(r/15-(l+5)/6+1, 0)49 }50 }51 for _, v := range ans {52 Fprintln(out, v)53 }54}55 5657