- 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
- 67 lines of Go from the credited upstream file 992E.go.
- The implementation visibly relies on sequence storage.
- 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)8 910type fenwick92 []int11 12func (t fenwick92) update(i, x int) {13 for ; i < len(t); i += i & -i {14 t[i] += x15 }16}17 18func (t fenwick92) pre(i int) (s int) {19 for ; i > 0; i &= i - 1 {20 s += t[i]21 }22 return23}24 25func (t fenwick92) lowerBound(sum int) (res int) {26 for b := 1 << 17; b > 0; b >>= 1 {27 if nxt := res | b; nxt < len(t) && t[nxt] < sum {28 sum -= t[nxt]29 res = nxt30 }31 }32 return res + 133}34 35func cf992E(in io.Reader, _w io.Writer) {36 out := bufio.NewWriter(_w)37 defer out.Flush()38 var n, q, p, x int39 Fscan(in, &n, &q)40 a := make([]int, n+1)41 t := make(fenwick92, n+1)42 for i := 1; i <= n; i++ {43 Fscan(in, &a[i])44 t.update(i, a[i])45 }46 47o:48 for range q {49 Fscan(in, &p, &x)50 t.update(p, x-a[p])51 a[p] = x52 53 i := 154 for i <= n {55 s := t.pre(i)56 if a[i]*2 == s {57 Fprintln(out, i)58 continue o59 }60 i = t.lowerBound(s * 2)61 }62 Fprintln(out, -1)63 }64}65 6667