Approach
Stack-based processing
For Codeforces 555B — Case of Fugitive, 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
- 74 lines of Go from the credited upstream file 555B.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 CF555B(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 17 var n, m int18 var pl, pr, l, r int6419 Fscan(in, &n, &m, &pl, &pr)20 type tuple struct {21 l, r int6422 i int23 }24 b := make([]tuple, n-1)25 for i := range b {26 Fscan(in, &l, &r)27 b[i] = tuple{l - pr, r - pl, i}28 pl, pr = l, r29 }30 sort.Slice(b, func(i, j int) bool { return b[i].r < b[j].r })31 a := make([]pair55, m)32 for i := range a {33 Fscan(in, &a[i].x)34 a[i].i = i + 135 }36 sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })37 38 ans := make([]int, n-1)39 h, j := hp55{}, 040 for _, p := range a {41 for ; j < n-1 && b[j].l <= p.x; j++ {42 heap.Push(&h, pair55{b[j].r, b[j].i})43 }44 if len(h) > 0 {45 if h[0].x < p.x {46 break47 }48 ans[heap.Pop(&h).(pair55).i] = p.i49 }50 }51 if len(h) > 0 || j < n-1 {52 Fprint(out, "No")53 } else {54 Fprintln(out, "Yes")55 for _, v := range ans {56 Fprint(out, v, " ")57 }58 }59}60 6162 63type pair55 struct {64 x int6465 i int66}67type hp55 []pair5568 69func (h hp55) Len() int { return len(h) }70func (h hp55) Less(i, j int) bool { return h[i].x < h[j].x }71func (h hp55) Swap(i, j int) { h[i], h[j] = h[j], h[i] }72func (h *hp55) Push(v interface{}) { *h = append(*h, v.(pair55)) }73func (h *hp55) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }74