Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "math/bits"8 "sort"9)10 1112var lucky21 = []int{4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777, 4444, 4447, 4474, 4477, 4744, 4747, 4774, 4777, 7444, 7447, 7474, 7477, 7744, 7747, 7774, 7777, 1e9}13var a21 []int14 15type seg21 []struct{ l, r, cnt, minD, todo int }16 17func (t seg21) set(o int) {18 i := t[o].l19 t[o].minD = lucky21[sort.SearchInts(lucky21, a21[i])] - a21[i]20 if t[o].minD == 0 {21 t[o].cnt = 122 } else {23 t[o].cnt = 024 }25}26 27func (t seg21) apply(o, f int) {28 t[o].minD -= f29 t[o].todo += f30}31 32func (t seg21) maintain(o int) {33 t[o].cnt = t[o<<1].cnt + t[o<<1|1].cnt34 t[o].minD = min(t[o<<1].minD, t[o<<1|1].minD)35}36 37func (t seg21) spread(o int) {38 f := t[o].todo39 if f == 0 {40 return41 }42 t.apply(o<<1, f)43 t.apply(o<<1|1, f)44 t[o].todo = 045}46 47func (t seg21) build(o, l, r int) {48 t[o].l, t[o].r = l, r49 if l == r {50 t.set(o)51 return52 }53 m := (l + r) >> 154 t.build(o<<1, l, m)55 t.build(o<<1|1, m+1, r)56 t.maintain(o)57}58 59func (t seg21) update(o, l, r, f int) {60 if l <= t[o].l && t[o].r <= r && t[o].minD > f {61 t.apply(o, f)62 return63 }64 if t[o].l == t[o].r {65 a21[t[o].l] += t[o].todo + f66 t[o].todo = 067 t.set(o)68 return69 }70 t.spread(o)71 m := (t[o].l + t[o].r) >> 172 if l <= m {73 t.update(o<<1, l, r, f)74 }75 if m < r {76 t.update(o<<1|1, l, r, f)77 }78 t.maintain(o)79}80 81func (t seg21) query(o, l, r int) int {82 if l <= t[o].l && t[o].r <= r {83 return t[o].cnt84 }85 m := (t[o].l + t[o].r) >> 186 if r <= m {87 return t.query(o<<1, l, r)88 }89 if l > m {90 return t.query(o<<1|1, l, r)91 }92 return t.query(o<<1, l, r) + t.query(o<<1|1, l, r)93}94 95func cf121E(in io.Reader, _w io.Writer) {96 out := bufio.NewWriter(_w)97 defer out.Flush()98 var n, m, l, r, d int99 var op string100 Fscan(in, &n, &m)101 a21 = make([]int, n)102 for i := range a21 {103 Fscan(in, &a21[i])104 }105 106 t := make(seg21, 2<<bits.Len(uint(n-1)))107 t.build(1, 0, n-1)108 109 for range m {110 Fscan(in, &op, &l, &r)111 l--112 r--113 if op[0] == 'a' {114 Fscan(in, &d)115 t.update(1, l, r, d)116 } else {117 Fprintln(out, t.query(1, l, r))118 }119 }120}121 122123