Editorial for WC '18 Contest 4 S4 - Super Luigi Odyssey


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.

Our approach will consist of two overall steps. In the first step, we'll only concern ourselves with determining which platform Luigi will arrive at after each event. In the second step, we'll then compute the actual amount of energy used along the way.

To handle the first step, we'll begin by sorting the platforms in increasing order of position, and then constructing a 2D segment tree of platform heights in \mathcal O(N \log N) time. This will allow us to efficiently simulate the sequence of events — for each type-2 or type-3 event i, we can first determine the index c of Luigi's current platform in the ordered list of non-submerged platforms (in other words, the number of platforms to the left of it whose heights exceed the lava's current height), and then find either the (c-X_i)th or (c+X_i)th non-submerged platform from the left (if any). Each of these may be queried using the segment tree in \mathcal O(\log^2 N) time. Over the course of this simulation, we'll assemble a list of jumping intervals (h, a, b), such that we know that Luigi will at some point jump platform-by-platform between platforms a and b while the lava height is h.

In the second step, our task will be to compute the sum of these jumping intervals' energy costs. We'll perform a line sweep over lava heights in decreasing order, with an event for each jumping interval (at its given lava height), as well as an event for each platform (at its height, such that it will be non-submerged for all subsequent lava heights). Along the way, we'll maintain an ordered set of non-submerged platforms (represented as a Balanced Binary Search Tree), as well as a Fenwick Tree (also known as a Binary Indexed Tree) with the cost of jumping from each pillar to its next non-submerged pillar to the right (or 0 if there's no such next pillar).

Each time a platform becomes unsubmerged, we can update the set in \mathcal O(\log N) time, and then consider the neighbouring unsubmerged platforms to its left/right to also update the Fenwick Tree in \mathcal O(\log N) time. Finally, each time we consider a jumping interval, we can query the Fenwick Tree for that interval of pillars in \mathcal O(\log N) time to determine the sum of energy costs of the jumps within it. These energy cost sums should then all be added together to yield the final answer.

Overall, this gives us an algorithm with time complexity \mathcal O(N \log N + M \log^2 N).


Comments

There are no comments at the moment.