Dynamic Programming21 sections · 916 units
Open in Course

When Queries Aren't Sorted

The general case

What if xix_i values come in arbitrary order? The front-popping trick fails because you might need a "popped" line later. Two solutions:

1.1. Binary search: Keep all lines on the envelope. For query xx, binary search for the segment containing xx. O(logn)O(\log n) per query.

2.2. Li Chao tree: A segment tree where each node stores a line. Insert and query both take O(logn)O(\log n). The deque still works for insertions (if slopes are sorted). Only queries change.