Editorial for WC '16 Contest 2 S3 - Turbolift Testing


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.

Solving this problem efficiently requires precomputing various useful values, and then using them to answer the questions.

Let's consider each of the N button sequences. Assuming that sequence i is executed with the turbolift starting at floor 0, let len[i] be the length of the sequence, delta[i] be the turbolift's final floor, maxP[i][j] be the highest floor reached at any point during the first j button presses of the sequence, minP[i][j] be the lowest floor reached during the first j button presses, maxS[i] be the highest floor reached at any point during the whole sequence (equal to maxP[i][len[i]]), and minS[i] be the lowest floor reached (equal to minP[i][len[i]]). These values can all be easily computed by simulating the sequence's button presses in \mathcal O(L[i]) time, which is sufficient as the sum of the sequences' lengths is at most 200\,000.

Next, let's repeat a similar process over the entire sequence of M button sequences. Let len_2[i] be the total length of the first i executed sequences, delta_2[i] be the turbolift's final floor after the first i sequences, max_2[i] be the highest floor reached at any point during the first i sequences, and min_2[i] be the lowest floor reached during the first i sequences. These values can similarly be easily computed in \mathcal O(M) time, with the help of the precomputed len, delta, maxS, and minS values. Note that len_2[0] = delta_2[0] = max_2[0] = min_2[0] = 0.

Finally, we're set up to answer the questions efficiently. Let's say we're interested in the first b button presses. The b-th button press must occur during some sequence i (1 \le i \le M) – in particular, during the first sequence i such that b \le len_2[i]. Since the values len_2[0 \dots M] are increasing, we can use binary search to determine the value of i in \mathcal O(\log M) time. Now, since the first i-1 button sequences have been completed before the b-th button press, the highest floor reached during the first b button presses is at least max_2[i-1]. However, the first b-len_2[i-1] button presses of button sequence S_i were additionally executed after that, which is where more of our precomputed values come into play – the answer must be \max(max_2[i-1], delta_2[i-1]+maxP[S_i][b-len_2[i-1]]). Similarly, the minimum floor reached is \min(min_2[i-1], delta_2[i-1]+minP[S_i][b-len_2[i-1]]).

The time complexity of this algorithm is \mathcal O(\sum_{i=1}^N |B_i| + M + Q \log M).


Comments

There are no comments at the moment.