- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 89 lines of Go from the credited upstream file 522B.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the build once, then multiply the logarithmic update or query path by the number of operations.
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)8 9func max522B(a, b int) int {10 if a > b {11 return a12 }13 return b14}15 16type stNode522B struct {17 l, r int18 val int19}20type segmentTree522B []stNode522B21 22func (t segmentTree522B) _pushUp(o int) {23 lo, ro := t[o<<1], t[o<<1|1]24 t[o].val = max522B(lo.val, ro.val)25}26 27func (t segmentTree522B) _build(arr []int, o, l, r int) {28 t[o].l, t[o].r = l, r29 if l == r {30 t[o].val = arr[l-1]31 return32 }33 mid := (l + r) >> 134 t._build(arr, o<<1, l, mid)35 t._build(arr, o<<1|1, mid+1, r)36 t._pushUp(o)37}38 39func (t segmentTree522B) _query(o, l, r int) (res int) {40 if l <= t[o].l && t[o].r <= r {41 return t[o].val42 }43 mid := (t[o].l + t[o].r) >> 144 res = -1e945 if l <= mid {46 res = max522B(res, t._query(o<<1, l, r))47 }48 if mid < r {49 res = max522B(res, t._query(o<<1|1, l, r))50 }51 return52}53 54func (t segmentTree522B) init(arr []int) { t._build(arr, 1, 1, len(arr)) }55func (t segmentTree522B) query(l, r int) int {56 if l > r {57 return 058 }59 return t._query(1, l, r)60}61 6263func Sol522B(reader io.Reader, writer io.Writer) {64 in := bufio.NewReader(reader)65 out := bufio.NewWriter(writer)66 defer out.Flush()67 68 var n int69 Fscan(in, &n)70 sumW := 071 w := make([]int, n)72 h := make([]int, n)73 for i := range w {74 Fscan(in, &w[i], &h[i])75 sumW += w[i]76 }77 78 t := make(segmentTree522B, 4*n)79 t.init(h)80 for i, wi := range w {81 maxH := max522B(t.query(1, i), t.query(i+2, n))82 Fprint(out, (sumW-wi)*maxH, " ")83 }84}85 86878889