Approach
Stack-based processing
For Codeforces 1883D — In Love, 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
- 61 lines of Go from the credited upstream file 1883D.go.
- The implementation keeps its working state in language-native values and containers.
- 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 cf1883D(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 17 var q, l, r int18 var op string19 minR, maxL := hp83{nil, map[int]int{}}, hp83{nil, map[int]int{}}20 for Fscan(in, &q); q > 0; q-- {21 Fscan(in, &op, &l, &r)22 if op == "+" {23 minR.push(r)24 maxL.push(-l)25 } else {26 minR.del(r)27 maxL.del(-l)28 }29 minR.do()30 maxL.do()31 if minR.Len() > 0 && minR.IntSlice[0] < -maxL.IntSlice[0] {32 Fprintln(out, "YES")33 } else {34 Fprintln(out, "NO")35 }36 }37}38 3940 41type hp83 struct {42 sort.IntSlice43 todo map[int]int44}45func (h *hp83) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }46func (h *hp83) Pop() any { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }47func (h *hp83) del(v int) { h.todo[v]++ }48func (h *hp83) push(v int) {49 if h.todo[v] > 0 {50 h.todo[v]--51 } else {52 heap.Push(h, v)53 }54}55func (h *hp83) do() {56 for h.Len() > 0 && h.todo[h.IntSlice[0]] > 0 {57 h.todo[h.IntSlice[0]]--58 heap.Pop(h)59 }60}61