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 nodeM70[K comparable, V any] struct {13 son [2]*nodeM70[K, V]14 priority uint15 key K16 value V17 subSize int18}19 20func (o *nodeM70[K, V]) size() int {21 if o != nil {22 return o.subSize23 }24 return 025}26 27func (o *nodeM70[K, V]) maintain() {28 o.subSize = 1 + o.son[0].size() + o.son[1].size()29}30 31func (o *nodeM70[K, V]) rotate(d int) *nodeM70[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 treapM70[K comparable, V any] struct {41 rd uint42 root *nodeM70[K, V]43 comparator func(a, b K) int44}45 46func (t *treapM70[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 *treapM70[K, V]) size() int { return t.root.size() }54func (t *treapM70[K, V]) empty() bool { return t.size() == 0 }55 56func (t *treapM70[K, V]) _put(o *nodeM70[K, V], key K, value V) *nodeM70[K, V] {57 if o == nil {58 o = &nodeM70[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 *treapM70[K, V]) put(key K, value V) { t.root = t._put(t.root, key, value) }79 80func (t *treapM70[K, V]) _delete(o *nodeM70[K, V], key K) *nodeM70[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 *treapM70[K, V]) delete(key K) { t.root = t._delete(t.root, key) }109 110func (t *treapM70[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 *treapM70[K, V]) kth(k int) (o *nodeM70[K, V]) {127 if k < 0 || k >= t.root.size() {128 return129 }130 for o = t.root; o != nil; {131 leftSize := o.son[0].size()132 if k < leftSize {133 o = o.son[0]134 } else {135 k -= leftSize + 1136 if k < 0 {137 break138 }139 o = o.son[1]140 }141 }142 return143}144 145func newMap70[K cmp.Ordered, V any]() *treapM70[K, V] {146 return &treapM70[K, V]{147 rd: uint(time.Now().UnixNano())/2 + 1,148 comparator: cmp.Compare[K],149 }150}151 152func cf70D(in io.Reader, _w io.Writer) {153 out := bufio.NewWriter(_w)154 defer out.Flush()155 det := func(x1, y1, x2, y2 int) int { return x1*y2 - x2*y1 }156 _remove := func(t *treapM70[int, int], i int) bool {157 if i <= 0 || i+1 >= t.size() {158 return false159 }160 pre := t.kth(i - 1)161 cur := t.kth(i)162 nxt := t.kth(i + 1)163 if det(cur.key-pre.key, cur.value-pre.value, nxt.key-pre.key, nxt.value-pre.value) >= 0 {164 t.delete(cur.key)165 return true166 }167 return false168 }169 contains := func(t *treapM70[int, int], x, y int) bool {170 i := t.lowerBoundIndex(x)171 if i == t.size() {172 return false173 }174 cur := t.kth(i)175 if cur.key == x {176 return y <= cur.value177 }178 if i == 0 {179 return false180 }181 pre := t.kth(i - 1)182 return det(cur.key-pre.key, cur.value-pre.value, x-pre.key, y-pre.value) <= 0183 }184 insert := func(t *treapM70[int, int], x, y int) bool {185 if contains(t, x, y) {186 return false187 }188 t.put(x, y)189 idx := t.lowerBoundIndex(x)190 for j := idx + 1; _remove(t, j); {191 }192 for j := idx - 1; _remove(t, j); j-- {193 }194 return true195 }196 top := newMap70[int, int]() 197 down := newMap70[int, int]() 198 199 var q, op, x, y int200 Fscan(in, &q)201 for range q {202 Fscan(in, &op, &x, &y)203 if op == 1 {204 insert(top, x, y)205 insert(down, x, -y)206 } else if contains(top, x, y) && contains(down, x, -y) {207 Fprintln(out, "YES")208 } else {209 Fprintln(out, "NO")210 }211 }212}213 214215