I found this to be the easiest of the IOI 2023 problems — it's the only one where I had a rough idea of how to solve it within a few minutes of thinking. It's still not easy though.
There are a few key observations that simplify the problem:
- A faster bus has no impact on the arrival times of a slower bus (even indirectly).
- This means that we can simply delete any buses faster than (or as fast as) the reserve bus, as it won't affect our answers.
- After doing that, the arrival times of all the non-reserve buses is fixed independent of Y.
- The arrival time of the reserve bus is an increasing function of Y.
We'll simulate the process in a spatial sweep along the road. At each sorting station, we'll answer the questions
- At what times to the non-reserve buses arrive here?
- For a given arrival time for the reserve bus, what is the range of Y values which would cause the reserve bus to arrive here at that time?
To answer the first question, we first need to know the order in which buses leave the previous sorting station, which is just a sort by departure time then by speed. A prefix maximum then gives the arrival times at the current station. To answer the second, we'll start by answering it specifically for times at which non-reserve buses arrive at this station. Consider a time t and slowest bus i to arrive at time t. If the reserve bus leaves the previous station between times t-W[i]d (exclusive) and t-Xd (inclusive) then it will arrive at time t. If it leaves any earlier it will be ahead of the traffic and arrive earlier, and if it leaves any later it will arrive later even if it doesn't encounter any traffic. To turn that range into a range of Y values, we rely on dynamic programming: we look up the interval of Y values corresponding to each endpoint and take their union.
What about answering the second question for times other than those when the non-reserve buses arrive? In those cases, the reserve bus did not encounter any traffic, so the answer for time t is simply the answer for the previous station at time t-Xd.
We'll need a data structure to represent all this. Conceptually it will be an increasing function mapping Y to an arrival time. It will need to support the following operations:
- Initialise with the identity function.
- Increment all values by Xd. This can be handled by maintaining an offset outside of the main data structure.
- Set all values in the interval (u, v] to t.
- Find the intercept for a given output value.
A suitable data structure is a balanced binary tree, where each node is an interval of Y values with a fixed output t. The intervals must be non-overlapping. For any Y value not contained in one of the intervals, the function is the identity. Setting an interval (u, v] to t requires
- Deleting any existing intervals contained entirely in that range.
- Trimming any intervals that overlap the range.
The deletion means each insertion could take linear time, but it is amortised since each inserted interval can only be deleted once.
This requires O(NM log(NM)) for initialisation and O(Q log(NM)) for the queries. I have one gripe with this problem, although I don't know if it is the fault of the IOI or just the IOI mirror on Codeforces: my solution was about 25% slower than the model solution, and exceeded the time limit. To make it pass, after initialisation I converted the std::map to a std::vector and used binary search on it to answer the queries, which is not a big-O change but was slightly more efficient and allowed it to get a full score.