Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "sort"8)9 1011func cf2094H1(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 const mx = 100_00115 divisors := [mx][]int{}16 for i := 2; i < mx; i++ {17 for j := i; j < mx; j += i {18 divisors[j] = append(divisors[j], i)19 }20 }21 22 var T, n, q, v, l, r int23 pos := [mx][]int{}24 for Fscan(in, &T); T > 0; T-- {25 Fscan(in, &n, &q)26 a := make([]int, n)27 for i := range a {28 Fscan(in, &a[i])29 pos[a[i]] = append(pos[a[i]], i)30 }31 for range q {32 Fscan(in, &v, &l, &r)33 l--34 ans := 035 for {36 mnI := n37 for _, d := range divisors[v] {38 ps := pos[d]39 i := sort.SearchInts(ps, l)40 if i < len(ps) && ps[i] < r {41 mnI = min(mnI, ps[i])42 }43 }44 if mnI == n {45 ans += v * (r - l)46 break47 }48 ans += v * (mnI - l)49 l = mnI50 d := a[l]51 for v /= d; v%d == 0; v /= d {52 }53 }54 Fprintln(out, ans)55 }56 for _, v := range a {57 pos[v] = pos[v][:0]58 }59 }60}61 6263func cf2094H(in io.Reader, _w io.Writer) {64 out := bufio.NewWriter(_w)65 defer out.Flush()66 const mx = 100_00167 divisors := [mx][]int{}68 for i := 2; i < mx; i++ {69 for j := i; j < mx; j += i {70 divisors[j] = append(divisors[j], i)71 }72 }73 74 var T, n, q, v, l, r int75 pos := [mx][]int{}76 for Fscan(in, &T); T > 0; T-- {77 Fscan(in, &n, &q)78 a := make([]int, n)79 for i := range a {80 Fscan(in, &a[i])81 pos[a[i]] = append(pos[a[i]], i)82 }83 type tuple struct{ v, r, i int }84 g := make([][]tuple, n)85 for i := range q {86 Fscan(in, &v, &l, &r)87 g[l-1] = append(g[l-1], tuple{v, r, i})88 }89 ans := make([]int, q)90 for l, qs := range g {91 for _, t := range qs {92 v := t.v93 mnI := n94 for _, d := range divisors[v] {95 ps := pos[d]96 for len(ps) > 0 && ps[0] < l {97 ps = ps[1:]98 }99 if len(ps) > 0 && ps[0] < t.r {100 mnI = min(mnI, ps[0])101 }102 pos[d] = ps103 }104 if mnI == n {105 ans[t.i] += v * (t.r - l)106 continue107 }108 ans[t.i] += v * (mnI - l)109 d := a[mnI]110 for v /= d; v%d == 0; v /= d {111 }112 ans[t.i] += v113 t.v = v114 mnI++115 if mnI < n {116 g[mnI] = append(g[mnI], t)117 }118 }119 }120 for _, v := range ans {121 Fprintln(out, v)122 }123 for _, v := range a {124 pos[v] = pos[v][:0]125 }126 }127}128 129130