Approach
Stack-based processing
For Codeforces 69E — Subsegments, 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
- 52 lines of Go from the credited upstream file 69E.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 CF69E(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 17 var n, k int18 Fscan(in, &n, &k)19 a := make([]int, n)20 cnt := map[int]int{}21 h := hp69{}22 for i := range a {23 Fscan(in, &a[i])24 v := a[i]25 cnt[v]++26 if cnt[v] == 1 {27 heap.Push(&h, v)28 }29 if i >= k-1 {30 for h.Len() > 0 && cnt[h.IntSlice[0]] != 1 {31 heap.Pop(&h)32 }33 if h.Len() > 0 {34 Fprintln(out, h.IntSlice[0])35 } else {36 Fprintln(out, "Nothing")37 }38 v := a[i-k+1]39 cnt[v]--40 if cnt[v] == 1 {41 heap.Push(&h, v)42 }43 }44 }45}46 4748type hp69 struct{ sort.IntSlice }49func (h hp69) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }50func (h *hp69) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }51func (h *hp69) Pop() any { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }52