890. The Best Offer Still Valid
A price-comparison service processes q operations in time order:
add v e an offer worth v arrives, valid up to and including day e
day t report the highest value among the offers still valid on day t
The days named by day operations never go backwards. For each day
operation print the best valid value, or none if nothing is valid.
add 10 3, add 7 9, day 1, day 4, add 20 5, day 6 -> 10, 7, 7
add 5 1, day 2 -> none
add 3 1, add 2 2, add 1 3, day 1, day 2, day 3, day 4 -> 3, 2, 1, none
In the first row, on day 1 both offers are valid and 10 is better. On day 4 the offer worth 10 has expired (its last day was 3), so the answer is 7. The offer worth 20 expires on day 5, so by day 6 it is already gone and 7 wins again.
## What most people try first
Scanning the whole list of offers on every query. Correct, and O(q) per
query, so 4 * 10^10 comparisons at the limits.
Sorting the offers by expiry and taking the best of a suffix is tempting and does not work, because value and expiry are unrelated: the best offer may expire first or last, and no single ordering answers both questions.
## A max-heap that is allowed to be out of date
Keep a max-heap of (value, expiry) ordered by value. Its top may well be
expired, and that is fine: an expired entry is simply DELETED when it is
noticed.
day t:
while the heap is non-empty and top.expiry < t:
pop and discard it
print top.value, or "none"
This is LAZY DELETION, and it is the standard answer to "a heap cannot remove something from its middle". Instead of removing the entry when it becomes invalid, you leave it there and skip it when it surfaces.
## Why it is correct, and why the days matter
Correctness rests on one line of the statement: the query days never decrease.
If top.expiry < t then that offer is invalid now, and since every future
t is at least this one, it is invalid forever. Discarding it permanently is
therefore safe.
Reverse the guarantee, letting days move backwards, and the whole method collapses: an offer discarded at day 10 might be needed again at day 4, and nothing has kept it. That is worth noticing, because "the queries are monotone" is the kind of sentence that looks like scene-setting and is actually the load-bearing assumption.
## Why it is fast even though the loop is unbounded
A single day query can pop many entries. But every offer is pushed exactly
once and popped at most once, so across the whole run there are at most q
pops in total. The cost is O(q log q) overall even though no individual
query is bounded by a constant. That is the same amortised argument that makes
a monotonic stack linear.
## The boundary that breaks a careless implementation
The expiry comparison. The offer is valid up to and INCLUDING day e, so it
is discarded when e < t, not when e <= t. With the wrong operator an
offer disappears on its last valid day, and the test asking for the answer on
day 10 with two offers expiring on day 10 catches it.
The second one is emptiness. Every offer may have expired, and the heap can be drained completely by a single query, so the top must not be read without checking the size.
## What is NOT an overflow here
Values and days both reach 10^9, which is inside the signed 32-bit range
since 2147483647 is larger, and nothing is summed or multiplied.
## Related problems
Problem 891 uses lazy deletion too, but there the removals are arbitrary rather than driven by a monotone clock, which forces a count of pending deletions instead of a single comparison. Problem 1913 counts hits in a trailing window with a queue rather than a heap, because there the order of expiry is the order of arrival.
## Cost
Time O(q log q). Space O(q).
Constraints - `1 ≤ q ≤ 200000` - `1 ≤ v ≤ 1000000000` - `1 ≤ e ≤ 1000000000` - `1 ≤ t ≤ 1000000000` - The `t` values are non-decreasing in the order the `day` operations appear.
Input
The first line contains one integer q.
Each of the next q lines is either add v e with two integers, or day t with one integer. The values of t are non-decreasing over the whole log.
Output
Print one line for every day operation, in order: the highest value among the offers still valid on that day, or the word none if none is valid. If the log contains no day at all, print the single word none.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.