Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910type node11 struct{ lo, ro *node11 }11 12func (o *node11) put(l, r, i int) *node11 {13 if o == nil {14 o = &node11{}15 }16 if l == r {17 return o18 }19 m := (l + r) / 220 if i <= m {21 o.lo = o.lo.put(l, m, i)22 } else {23 o.ro = o.ro.put(m+1, r, i)24 }25 return o26}27 28func (o *node11) merge(b *node11) *node11 {29 if b == nil {30 return o31 }32 if o == nil {33 return b34 }35 o.lo = o.lo.merge(b.lo)36 o.ro = o.ro.merge(b.ro)37 return o38}39 40func move11(from, to **node11, l, r, ql, qr int) {41 if *from == nil {42 return43 }44 if ql <= l && r <= qr {45 *to = (*to).merge(*from)46 *from = nil47 return48 }49 if *to == nil {50 *to = &node11{}51 }52 m := (l + r) / 253 if ql <= m {54 move11(&(*from).lo, &(*to).lo, l, m, ql, qr)55 }56 if qr > m {57 move11(&(*from).ro, &(*to).ro, m+1, r, ql, qr)58 }59}60 61func (o *node11) collect(ans []int, l, r, v int) {62 if o == nil {63 return64 }65 if l == r {66 ans[l] = v67 return68 }69 m := (l + r) / 270 o.lo.collect(ans, l, m, v)71 o.ro.collect(ans, m+1, r, v)72}73 74func cf911G(in io.Reader, _w io.Writer) {75 out := bufio.NewWriter(_w)76 defer out.Flush()77 var n, v, q, l, r, x, y int78 Fscan(in, &n)79 roots := [101]*node11{}80 for i := 1; i <= n; i++ {81 Fscan(in, &v)82 roots[v] = roots[v].put(1, n, i)83 }84 85 Fscan(in, &q)86 for range q {87 Fscan(in, &l, &r, &x, &y)88 if x != y {89 move11(&roots[x], &roots[y], 1, n, l, r)90 }91 }92 93 ans := make([]int, n+1)94 for v, rt := range roots {95 rt.collect(ans, 1, n, v)96 }97 for _, v := range ans[1:] {98 Fprint(out, v, " ")99 }100}101 102103