Approach
Stack-based processing
For Codeforces 1887C — Minimum Array, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 66 lines of Go from the credited upstream file 1887C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
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 "container/heap"6 . "fmt"7 "io"8 "sort"9)10 1112func cf1887C(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 var T, n, q, l, r, x int16 for Fscan(in, &T); T > 0; T-- {17 Fscan(in, &n)18 a := make([]int, n)19 for i := range a {20 Fscan(in, &a[i])21 }22 23 sd := make([]int, n+1)24 d := make([]int, n+1)25 h := hp87{}26 Fscan(in, &q)27 for range q {28 Fscan(in, &l, &r, &x)29 if x == 0 {30 continue31 }32 l--33 if d[l] == 0 {34 heap.Push(&h, l)35 }36 d[l] += x37 if d[r] == 0 {38 heap.Push(&h, r)39 }40 d[r] -= x41 for h.Len() > 0 && d[h.IntSlice[0]] == 0 {42 heap.Pop(&h)43 }44 if h.Len() > 0 && d[h.IntSlice[0]] < 0 {45 for _, i := range h.IntSlice {46 sd[i] += d[i]47 d[i] = 048 }49 h.IntSlice = h.IntSlice[:0]50 }51 }52 53 s := 054 for i, v := range a {55 s += sd[i]56 Fprint(out, v+s, " ")57 }58 Fprintln(out)59 }60}61 6263type hp87 struct{ sort.IntSlice }64func (h *hp87) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }65func (h *hp87) Pop() any { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }66