Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 "cmp"6 . "fmt"7 "io"8 "time"9)10 1112type nodeM[K comparable, V any] struct {13 son [2]*nodeM[K, V]14 priority uint15 key K16 value V17 subSize int18}19 20func (o *nodeM[K, V]) size() int {21 if o != nil {22 return o.subSize23 }24 return 025}26 27func (o *nodeM[K, V]) maintain() {28 o.subSize = 1 + o.son[0].size() + o.son[1].size()29}30 31func (o *nodeM[K, V]) rotate(d int) *nodeM[K, V] {32 x := o.son[d^1]33 o.son[d^1] = x.son[d]34 x.son[d] = o35 o.maintain()36 x.maintain()37 return x38}39 40type treapM[K comparable, V any] struct {41 rd uint42 root *nodeM[K, V]43 comparator func(a, b K) int44}45 46func (t *treapM[K, V]) fastRand() uint {47 t.rd ^= t.rd << 1348 t.rd ^= t.rd >> 1749 t.rd ^= t.rd << 550 return t.rd51}52 53func (t *treapM[K, V]) size() int { return t.root.size() }54func (t *treapM[K, V]) empty() bool { return t.size() == 0 }55 56func (t *treapM[K, V]) _put(o *nodeM[K, V], key K, value V) *nodeM[K, V] {57 if o == nil {58 o = &nodeM[K, V]{priority: t.fastRand(), key: key, value: value}59 } else {60 c := t.comparator(key, o.key)61 if c == 0 {62 o.value = value63 } else {64 d := 065 if c > 0 {66 d = 167 }68 o.son[d] = t._put(o.son[d], key, value)69 if o.son[d].priority > o.priority {70 o = o.rotate(d ^ 1)71 }72 }73 }74 o.maintain()75 return o76}77 78func (t *treapM[K, V]) put(key K, value V) { t.root = t._put(t.root, key, value) }79 80func (t *treapM[K, V]) _delete(o *nodeM[K, V], key K) *nodeM[K, V] {81 if o == nil {82 return nil83 }84 if c := t.comparator(key, o.key); c != 0 {85 d := 086 if c > 0 {87 d = 188 }89 o.son[d] = t._delete(o.son[d], key)90 } else {91 if o.son[1] == nil {92 return o.son[0]93 }94 if o.son[0] == nil {95 return o.son[1]96 }97 d := 098 if o.son[0].priority > o.son[1].priority {99 d = 1100 }101 o = o.rotate(d)102 o.son[d] = t._delete(o.son[d], key)103 }104 o.maintain()105 return o106}107 108func (t *treapM[K, V]) delete(key K) { t.root = t._delete(t.root, key) }109 110func (t *treapM[K, V]) lowerBoundIndex(key K) (kth int) {111 for o := t.root; o != nil; {112 c := t.comparator(key, o.key)113 if c < 0 {114 o = o.son[0]115 } else if c > 0 {116 kth += o.son[0].size() + 1117 o = o.son[1]118 } else {119 kth += o.son[0].size()120 break121 }122 }123 return124}125 126func (t *treapM[K, V]) upperBoundIndex(key K) (kth int) {127 for o := t.root; o != nil; {128 c := t.comparator(key, o.key)129 if c < 0 {130 o = o.son[0]131 } else if c > 0 {132 kth += o.son[0].size() + 1133 o = o.son[1]134 } else {135 kth += o.son[0].size() + 1136 break137 }138 }139 return140}141 142func (t *treapM[K, V]) kth(k int) (o *nodeM[K, V]) {143 if k < 0 || k >= t.root.size() {144 return145 }146 for o = t.root; o != nil; {147 leftSize := o.son[0].size()148 if k < leftSize {149 o = o.son[0]150 } else {151 k -= leftSize + 1152 if k < 0 {153 break154 }155 o = o.son[1]156 }157 }158 return159}160 161func (t *treapM[K, V]) prev(key K) *nodeM[K, V] { return t.kth(t.lowerBoundIndex(key) - 1) }162func (t *treapM[K, V]) next(key K) *nodeM[K, V] { return t.kth(t.upperBoundIndex(key)) }163func (t *treapM[K, V]) floor(key K) *nodeM[K, V] { return t.kth(t.upperBoundIndex(key) - 1) }164 165func newMap[K cmp.Ordered, V any]() *treapM[K, V] {166 return &treapM[K, V]{167 rd: uint(time.Now().UnixNano()),168 comparator: cmp.Compare[K],169 }170}171 172type fenwick38 []int173 174func (t fenwick38) update(i, v int) {175 for ; i < len(t); i += i & -i {176 t[i] += v177 }178}179 180func (t fenwick38) pre(i int) (s int) {181 for ; i > 0; i &= i - 1 {182 s += t[i]183 }184 return185}186 187func cf1638E(in io.Reader, _w io.Writer) {188 out := bufio.NewWriter(_w)189 defer out.Flush()190 var n, m, l, r, c, x int191 var op string192 Fscan(in, &n, &m)193 194 type pair struct{ r, c int }195 t := newMap[int, pair]()196 t.put(1, pair{n, 1})197 t.put(n+1, pair{})198 split := func(mid int) {199 o := t.floor(mid)200 if o.key < mid {201 t.put(mid, o.value)202 o.value.r = mid - 1203 }204 }205 206 f := make(fenwick38, n+1)207 inc := make([]int, n+1)208 209 for range m {210 Fscan(in, &op)211 if op[0] == 'C' {212 Fscan(in, &l, &r, &c)213 split(l)214 split(r + 1)215 for o := t.floor(l); o.key <= r; o = t.next(o.key) {216 d := inc[o.value.c] - inc[c]217 f.update(o.key, d)218 f.update(o.value.r+1, -d)219 t.delete(o.key)220 }221 t.put(l, pair{r, c})222 } else if op[0] == 'A' {223 Fscan(in, &c, &x)224 inc[c] += x225 } else {226 Fscan(in, &x)227 Fprintln(out, f.pre(x)+inc[t.floor(x).value.c])228 }229 }230}231 232233