- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 169 lines of Go from the credited upstream file 915E.go.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "time"8)9 10type node15 struct {11 lr [2]*node1512 priority uint13 l, r int14 work bool15}16 17func (o *node15) cmp(b int) int {18 switch {19 case b < o.l:20 return 021 case b > o.l:22 return 123 default:24 return -125 }26}27 28func (o *node15) rotate(d int) *node15 {29 x := o.lr[d^1]30 o.lr[d^1] = x.lr[d]31 x.lr[d] = o32 return x33}34 35type treap15 struct {36 rd uint37 root *node1538 workDayCnt int39}40 41func (t *treap15) fastRand() uint {42 t.rd ^= t.rd << 1343 t.rd ^= t.rd >> 1744 t.rd ^= t.rd << 545 return t.rd46}47 48func (t *treap15) _put(o *node15, l, r int, work bool) *node15 {49 if o == nil {50 return &node15{priority: t.fastRand(), l: l, r: r, work: work}51 }52 if d := o.cmp(l); d >= 0 {53 o.lr[d] = t._put(o.lr[d], l, r, work)54 if o.lr[d].priority > o.priority {55 o = o.rotate(d ^ 1)56 }57 } else {58 o.work = work59 }60 return o61}62 63func (t *treap15) put(l, r int, work bool) { t.root = t._put(t.root, l, r, work) }64 65func (t *treap15) _delete(o *node15, l int) *node15 {66 if o == nil {67 return nil68 }69 if d := o.cmp(l); d >= 0 {70 o.lr[d] = t._delete(o.lr[d], l)71 } else {72 if o.lr[1] == nil {73 return o.lr[0]74 }75 if o.lr[0] == nil {76 return o.lr[1]77 }78 d = 079 if o.lr[0].priority > o.lr[1].priority {80 d = 181 }82 o = o.rotate(d)83 o.lr[d] = t._delete(o.lr[d], l)84 }85 return o86}87 88func (t *treap15) delete(l int) { t.root = t._delete(t.root, l) }89 90func (t *treap15) floor(key int) (floor *node15) {91 for o := t.root; o != nil; {92 switch c := o.cmp(key); {93 case c == 0:94 o = o.lr[0]95 case c > 0:96 floor = o97 o = o.lr[1]98 default:99 return o100 }101 }102 return103}104 105func (t *treap15) next(l int) (next *node15) {106 for o := t.root; o != nil; {107 if o.cmp(l) == 0 {108 next = o109 o = o.lr[0]110 } else {111 o = o.lr[1]112 }113 }114 return115}116 117func (t *treap15) split(mid int) {118 if o := t.floor(mid); o.l < mid && mid <= o.r {119 r, work := o.r, o.work120 o.r = mid - 1121 t.put(mid, r, work)122 }123}124 125func (t *treap15) prepare(l, r int) {126 t.split(l)127 t.split(r + 1)128}129 130func (t *treap15) updateCnt(o *node15, work bool) {131 if !o.work && work {132 t.workDayCnt += o.r - o.l + 1133 } else if o.work && !work {134 t.workDayCnt -= o.r - o.l + 1135 }136}137 138func (t *treap15) merge(l, r int, work bool) {139 t.prepare(l, r)140 for o := t.next(l); o != nil && o.l <= r; o = t.next(o.l) {141 t.updateCnt(o, work)142 t.delete(o.l)143 }144 o := t.floor(l)145 t.updateCnt(o, work)146 o.r = r147 o.work = work148}149 150151func CF915E(_r io.Reader, _w io.Writer) {152 in := bufio.NewReader(_r)153 out := bufio.NewWriter(_w)154 defer out.Flush()155 156 var n, q, l, r, k int157 Fscan(in, &n, &q)158 t := &treap15{rd: uint(time.Now().UnixNano())/2 + 1}159 t.put(1, n, true)160 t.workDayCnt = n161 for ; q > 0; q-- {162 Fscan(in, &l, &r, &k)163 t.merge(l, r, k == 2)164 Fprintln(out, t.workDayCnt)165 }166}167 168169