Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "sort"8)9 1011type node10 struct {12 ch [2]*node1013 priority uint14 key, sz int15}16 17func (o *node10) cmp(b int) int {18 switch {19 case b < o.key:20 return 021 case b > o.key:22 return 123 default:24 return -125 }26}27 28func (o *node10) size() int {29 if o != nil {30 return o.sz31 }32 return 033}34 35func (o *node10) maintain() { o.sz = 1 + o.ch[0].size() + o.ch[1].size() }36 37func (o *node10) rotate(d int) *node10 {38 x := o.ch[d^1]39 o.ch[d^1] = x.ch[d]40 x.ch[d] = o41 o.maintain()42 x.maintain()43 return x44}45 46type treap10 struct {47 rd uint48 root *node1049}50 51func (t *treap10) fastRand() uint {52 t.rd ^= t.rd << 1353 t.rd ^= t.rd >> 1754 t.rd ^= t.rd << 555 return t.rd56}57 58func (t *treap10) _put(o *node10, key int) *node10 {59 if o == nil {60 return &node10{priority: t.fastRand(), key: key, sz: 1}61 }62 d := o.cmp(key)63 o.ch[d] = t._put(o.ch[d], key)64 if o.ch[d].priority > o.priority {65 o = o.rotate(d ^ 1)66 }67 o.maintain()68 return o69}70 71func (t *treap10) put(key int) { t.root = t._put(t.root, key) }72 73func (t *treap10) _delete(o *node10, key int) *node10 {74 if d := o.cmp(key); d >= 0 {75 o.ch[d] = t._delete(o.ch[d], key)76 } else {77 if o.ch[1] == nil {78 return o.ch[0]79 }80 if o.ch[0] == nil {81 return o.ch[1]82 }83 d = 084 if o.ch[0].priority > o.ch[1].priority {85 d = 186 }87 o = o.rotate(d)88 o.ch[d] = t._delete(o.ch[d], key)89 }90 o.maintain()91 return o92}93 94func (t *treap10) delete(key int) { t.root = t._delete(t.root, key) }95 96func (t *treap10) rank(key int) (kth int) {97 for o := t.root; o != nil; {98 switch c := o.cmp(key); {99 case c == 0:100 o = o.ch[0]101 case c > 0:102 kth += 1 + o.ch[0].size()103 o = o.ch[1]104 default:105 kth += o.ch[0].size()106 return107 }108 }109 return110}111 112func CF610D(_r io.Reader, out io.Writer) {113 in := bufio.NewReader(_r)114 type pair struct{ p, l, r int }115 var a, b []pair116 var n, x1, y1, x2, y2 int117 for Fscan(in, &n); n > 0; n-- {118 Fscan(in, &x1, &y1, &x2, &y2)119 if y1 == y2 {120 if x1 > x2 {121 x1, x2 = x2, x1122 }123 a = append(a, pair{y1, x1, x2})124 } else {125 if y1 > y2 {126 y1, y2 = y2, y1127 }128 b = append(b, pair{x1, y1, y2})129 }130 }131 132 ans := int64(0)133 unique := func(a []pair) (b []pair) {134 sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.p < b.p || a.p == b.p && a.l < b.l })135 for _, p := range a {136 if b == nil || p.p > b[len(b)-1].p || p.l > b[len(b)-1].r {137 b = append(b, p)138 } else if p.r > b[len(b)-1].r {139 b[len(b)-1].r = p.r 140 }141 }142 for _, p := range b {143 ans += int64(p.r - p.l + 1)144 }145 return b146 }147 a = unique(a)148 b = unique(b)149 if len(a) > len(b) {150 a, b = b, a151 }152 153 type event struct{ e, p int }154 es := make([]event, 0, 2*len(a))155 for _, p := range a {156 es = append(es, event{p.l<<1 | 1, p.p}, event{(p.r + 1) << 1, p.p})157 }158 sort.Slice(es, func(i, j int) bool { return es[i].e < es[j].e })159 t := &treap10{rd: 1} 160 i := 0161 for _, p := range b {162 for ; i < len(es) && es[i].e>>1 <= p.p; i++ {163 if es[i].e&1 > 0 {164 t.put(es[i].p)165 } else {166 t.delete(es[i].p)167 }168 }169 ans -= int64(t.rank(p.r+1) - t.rank(p.l)) 170 }171 Fprint(out, ans)172}173 174175