Standard Slope Trick gives the minimum. What if we need for arbitrary ? Count breakpoints to the left of . Each adds to slope.
Compute from base + slope integral. Efficient query: use ordered set (like C++ set) instead of heaps. per query. Trade-off: heaps are simpler and faster for operations, but don't support arbitrary queries. For arbitrary point queries, replace heaps with ordered sets. This gives query at the cost of complexity.