Median of a Data Stream
Use two heaps: a max-heap containing the smaller half of values and a min-heap containing the larger half. Maintain their sizes so they differ by at most one. The median is the root of the larger heap or the average of both roots when their sizes are equal.
Insertion: O(log n).
Median query: O(1).
Space: O(n).
Max-heap stores lower half.
Min-heap stores upper half.