Dynamic Programming21 sections · 916 units
Open in Course

CHT Query - Walkthrough

Tracing a query

Deque has lines with intersections at x=3,7,12x = 3, 7, 12. Query for x=10x = 10. Binary search: 10>310 > 3, 10>710 > 7, 10<1210 < 12. The optimal line is between intersections 7 and 12.

With sorted queries: if last query was x=8x = 8, pointer was at line after intersection 7. For x=10x = 10, pointer stays (still before intersection 12). Amortized O(1)O(1) per query: pointer only moves right. Total movement across all queries is O(n)O(n).