- 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
- 81 lines of Go from the credited upstream file 1736C2.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 910func cf1736C2(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 var n, m, l, l2, r2, x int14 Fscan(in, &n)15 ans := n * (n + 1) / 216 right := make([]int, n)17 right2 := make([]int, n)18 for i := range right {19 right[i] = n20 right2[i] = n21 }22 a := make([]int, n)23 for i := range a {24 Fscan(in, &a[i])25 v := a[i] - i - 126 for l2 < r2 && -l2 > v {27 right2[l2] = i28 l2++29 }30 for l < i && -l > v {31 ans -= n - i32 right[l] = i33 l++34 r2++35 }36 }37 38 sumR := make([]int, n+1)39 sumInc := make([]int, n+1)40 for i, v := range right {41 sumR[i+1] = sumR[i] + v42 sumInc[i+1] = sumInc[i] + right2[i] - v43 }44 45 ge := make([]int, n+1)46 p := 047 for i := range ge {48 for right[p] < i {49 p++50 }51 ge[i] = p52 }53 54 Fscan(in, &m)55 for range m {56 Fscan(in, &p, &x)57 p--58 tar := x - p - 1 59 l := ge[p+1] 60 if x <= a[p] {61 if tar >= -l {62 Fprintln(out, ans) 63 } else {64 r := -tar65 66 Fprintln(out, ans-(sumR[r]-sumR[l]-(r-l)*p))67 }68 } else {69 if p == 0 || right[p-1] < p-1 {70 Fprintln(out, ans) 71 } else {72 ll := max(ge[p], -tar)73 74 Fprintln(out, ans+sumInc[l]-sumInc[ll])75 }76 }77 }78}79 8081