Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "strings"8 "time"9)10 1112type node58 struct {13 lr [2]*node5814 priority uint15 l, r int16 b byte17}18 19func (o *node58) cmp(b int) int {20 switch {21 case b < o.l:22 return 023 case b > o.l:24 return 125 default:26 return -127 }28}29 30func (o *node58) rotate(d int) *node58 {31 x := o.lr[d^1]32 o.lr[d^1] = x.lr[d]33 x.lr[d] = o34 return x35}36 37type treap58 struct {38 rd uint39 root *node5840}41 42func (t *treap58) fastRand() uint {43 t.rd ^= t.rd << 1344 t.rd ^= t.rd >> 1745 t.rd ^= t.rd << 546 return t.rd47}48 49func (t *treap58) _put(o *node58, l, r int, b byte) *node58 {50 if o == nil {51 return &node58{priority: t.fastRand(), l: l, r: r, b: b}52 }53 if d := o.cmp(l); d >= 0 {54 o.lr[d] = t._put(o.lr[d], l, r, b)55 if o.lr[d].priority > o.priority {56 o = o.rotate(d ^ 1)57 }58 } else {59 o.b = b60 }61 return o62}63 64func (t *treap58) put(l, r int, b byte) { t.root = t._put(t.root, l, r, b) }65 66func (t *treap58) _delete(o *node58, l int) *node58 {67 if o == nil {68 return nil69 }70 if d := o.cmp(l); d >= 0 {71 o.lr[d] = t._delete(o.lr[d], l)72 } else {73 if o.lr[1] == nil {74 return o.lr[0]75 }76 if o.lr[0] == nil {77 return o.lr[1]78 }79 d = 080 if o.lr[0].priority > o.lr[1].priority {81 d = 182 }83 o = o.rotate(d)84 o.lr[d] = t._delete(o.lr[d], l)85 }86 return o87}88 89func (t *treap58) delete(l int) { t.root = t._delete(t.root, l) }90 91func (t *treap58) floor(key int) (floor *node58) {92 for o := t.root; o != nil; {93 switch c := o.cmp(key); {94 case c == 0:95 o = o.lr[0]96 case c > 0:97 floor = o98 o = o.lr[1]99 default:100 return o101 }102 }103 return104}105 106func (t *treap58) next(l int) (next *node58) {107 for o := t.root; o != nil; {108 if o.cmp(l) == 0 {109 next = o110 o = o.lr[0]111 } else {112 o = o.lr[1]113 }114 }115 return116}117 118func (t *treap58) split(mid int) {119 if o := t.floor(mid); o.l < mid && mid <= o.r {120 r, b := o.r, o.b121 o.r = mid - 1122 t.put(mid, r, b)123 }124}125 126func (t *treap58) prepare(l, r int) {127 t.split(l)128 t.split(r + 1)129}130 131func (t *treap58) sort(l, r int, inc bool) {132 t.prepare(l, r)133 cnt := [26]int{}134 for o := t.floor(l); o != nil && o.l <= r; o = t.next(o.l) {135 cnt[o.b] += o.r - o.l + 1136 t.delete(o.l)137 }138 if inc {139 for i, c := range cnt {140 if c > 0 {141 t.put(l, l+c-1, byte(i))142 l += c143 }144 }145 } else {146 for i, c := range cnt {147 if c > 0 {148 t.put(r-c+1, r, byte(i))149 r -= c150 }151 }152 }153}154 155func CF558E(_r io.Reader, _w io.Writer) {156 in := bufio.NewReader(_r)157 out := bufio.NewWriter(_w)158 defer out.Flush()159 160 var n, q, l, r int161 var inc bool162 var s []byte163 Fscan(in, &n, &q, &s)164 t := &treap58{rd: uint(time.Now().UnixNano())/2 + 1}165 for i, b := range s {166 t.put(i+1, i+1, b-'a')167 }168 for ; q > 0; q-- {169 Fscan(in, &l, &r, &inc)170 t.sort(l, r, inc)171 }172 var f func(*node58)173 f = func(o *node58) {174 if o == nil {175 return176 }177 f(o.lr[0])178 Fprint(out, strings.Repeat(string('a'+o.b), o.r-o.l+1))179 f(o.lr[1])180 }181 f(t.root)182}183 184185