- 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
- 78 lines of Go from the credited upstream file 1030F.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 fenwick30 []int11 12func (f fenwick30) update(i, v int) {13 for ; i < len(f); i += i & -i {14 f[i] += v15 }16}17 18func (f fenwick30) pre(i int) (res int) {19 for ; i > 0; i &= i - 1 {20 res += f[i]21 }22 return23}24 25func (f fenwick30) query(l, r int) int {26 return f.pre(r) - f.pre(l-1)27}28 29func (f fenwick30) kth(k int) (res int) {30 for b := 1 << 17; b > 0; b >>= 1 {31 if nxt := res | b; nxt < len(f) && f[nxt] < k {32 k -= f[nxt]33 res = nxt34 }35 }36 return res + 137}38 39func cf1030F(in io.Reader, _w io.Writer) {40 out := bufio.NewWriter(_w)41 defer out.Flush()42 const mod = 1_000_000_00743 var n, q, l, r int44 Fscan(in, &n, &q)45 a := make([]int, n+1)46 for i := 1; i <= n; i++ {47 Fscan(in, &a[i])48 a[i] -= i49 }50 w := make([]int, n+1)51 fw := make(fenwick30, n+1)52 fwa := make(fenwick30, n+1)53 for i := 1; i <= n; i++ {54 Fscan(in, &w[i])55 fw.update(i, w[i])56 fwa.update(i, w[i]*a[i]%mod)57 }58 59 for range q {60 Fscan(in, &l, &r)61 if l < 0 {62 l = -l63 d := r - w[l]64 w[l] = r65 fw.update(l, d)66 fwa.update(l, d*a[l]%mod)67 } else {68 m := fw.kth((fw.pre(l-1) + fw.pre(r) + 1) / 2)69 x := a[m]70 ans := fw.query(l, m)%mod*x - fwa.query(l, m) +71 fwa.query(m, r) - fw.query(m, r)%mod*x72 Fprintln(out, (ans%mod+mod)%mod)73 }74 }75}76 7778