- 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
- 81 lines of Go from the credited upstream file 339D.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 9type node339D struct {10 l, r int11 val int12 needOR bool13}14type segmentTree339D []node339D15 16func (t segmentTree339D) _pushUp(o int) {17 lo, ro := t[o<<1], t[o<<1|1]18 if lo.needOR {19 t[o].val = lo.val | ro.val20 } else {21 t[o].val = lo.val ^ ro.val22 }23}24 25func (t segmentTree339D) _build(arr []int, o, l, r int) {26 t[o].l, t[o].r = l, r27 if l == r {28 t[o].val = arr[l]29 t[o].needOR = true30 return31 }32 mid := (l + r) >> 133 t._build(arr, o<<1, l, mid)34 t._build(arr, o<<1|1, mid+1, r)35 t[o].needOR = !t[o<<1].needOR36 t._pushUp(o)37}38 39func (t segmentTree339D) _update(o, idx int, val int) {40 if t[o].l == t[o].r {41 t[o].val = val42 return43 }44 if idx <= (t[o].l+t[o].r)>>1 {45 t._update(o<<1, idx, val)46 } else {47 t._update(o<<1|1, idx, val)48 }49 t._pushUp(o)50}51 52func (t segmentTree339D) init(arr []int) { t._build(arr, 1, 1, len(arr)-1) }53func (t segmentTree339D) update(idx int, val int) { t._update(1, idx, val) }54 55func Sol339D(reader io.Reader, writer io.Writer) {56 in := bufio.NewReader(reader)57 out := bufio.NewWriter(writer)58 defer out.Flush()59 60 var n, m int61 Fscan(in, &n, &m)62 n = 1 << uint(n)63 arr := make([]int, n+1)64 for i := 1; i <= n; i++ {65 Fscan(in, &arr[i])66 }67 68 t := make(segmentTree339D, 2*n)69 t.init(arr)70 for ; m > 0; m-- {71 var idx, val int72 Fscan(in, &idx, &val)73 t.update(idx, val)74 Fprintln(out, t[1].val)75 }76}77 78798081