Editorial for WC '16 Contest 2 S4 - Diplomacy


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let's consider plotting the temperature and gravity values on a Cartesian plane. For example, let's pretend that all temperature values are x-coordinates, and all gravity values are y-coordinates. Every race's preferred requirements correspond to a rectangle, and the answer is the largest number of rectangles that overlap (inclusively) at any single point.

This geometric representation suggests a possible approach – line sweep. Let's consider sweeping upwards through the rectangles. As we go, we'll want to handle interesting events, namely the bottoms and tops of rectangles. A rectangle with lower-left corner (x_1, y_1) and upper-right corner (x_2, y_2) will correspond to a starting event at y-coordinate y_1, and an ending event at y-coordinate y_2, with both events tied to the range of x-coordinates [x_1, x_2]. We'll want to generate all 2N events and then iterate over them in increasing order of y-coordinate. Because rectangles' ranges should be inclusive, we should carefully break ties in the sorting so that we iterate over all starting events at a given y-coordinate before the ending events at the same y-coordinate.

During the line sweep, we'll want to keep track of what rectangles are currently ongoing in some fashion (in terms of their x-coordinate ranges). Importantly, we'll need to be able to efficiently determine the largest number of these 1-dimensional inclusive ranges that overlap at any single point. If we can compute this value at every step of the line sweep, then the final answer will be the largest of these values.

What remains is coming up with a data structure which will allow us to handle these updates and queries efficiently (as we'll have to do \mathcal O(N) of them over the course of the line sweep). Let's model exactly what we need the data structure to support. Let A[i] be the number of active ranges which include point i. A rectangle's starting event corresponds to incrementing A[i] for each i in the event's range [x_1, x_2]. Similarly, a rectangle's event corresponds to decrementing this range of A values. Finally, we'll need to look up the largest A value.

One thing worth noting about this A array is that it can be very large (as x-coordinates can be between 1 and 10^9). However, the exact values of the rectangles' x-coordinates don't matter, only their relative order does. As such, they can be compressed down to values between 1 and at most 2N. This will reduce A's size down to \mathcal O(N), which is more manageable.

Now, what data structure can efficiently support the required set of updates and queries? A segment tree with lazy propagation can get them done in \mathcal O(\log N) time each, which will result in an overall time complexity of \mathcal O(N \log N). Each node in the tree should simply store the largest A value in its range, as well as a lazy value of how much its entire range of A values should be incremented/decremented by. This works out nicely due to the observation that, when a set of values are all incremented by a constant amount x, their maximum value will also increase by x. Therefore, range updates can be lazily applied in a standard fashion, and the maximum of all of the values can be determined from the root of the tree. It's worth noting that an interval tree can also be used in place of a segment tree to manage the state of the ongoing rectangles during the line sweep.


Comments

There are no comments at the moment.