Editorial for WC '15 Contest 3 J3 - Red Sun Simulator


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.

In this problem, we would like to compute two values: the maximum and minimum number of minutes that Superman can be weakened for by the red sun simulator. These two values are always fixed, and knowing them will make the problem trivial to solve. The observation is that if he can be weakened for both the min and max number of minutes, then he can also be weakened by any number of minutes between them. Assuming we know this range, then for every query T_i, we output Y if and only if T_i is between the min and max number of minutes, inclusive.

We can initialize the min and max to 0, and then iterate over the N minutes while updating them appropriately. Namely, in each minute i, one of three possible situations can be true:

  1. Superman must be weakened (the min and max will each increase by 1)
  2. Superman may or may not be weakened (only the max will increase by 1)
  3. Superman cannot be weakened (nothing is changed)

Since there are up to 5 intervals to consider for each minute, we must decide how to determine whether the weakening of Superman is mandatory (case 1) or possible (case 2).

For Superman's weakening to be mandatory, either the solar frequency of minute i is equal to 1 (and so every value of every interval is a multiple), or every interval for minute i solely consists of a single integer which is a multiple of the solar frequency (convince yourself that it is impossible for two consecutive integers to both be a multiple of another integer greater than 1). In this case, if even a single interval does not have A_{i,j} = B_{i,j} and A_{i,j} as a multiple of F_i, then we know that there is a chance for the RSS to fail at weakening Superman during minute i. Whether Superman's weakening is mandatory can be checked using the AND operation on an initially true boolean value as the intervals are being inputted (with no added time complexity).

The harder part is checking whether it's at least possible to weaken Superman during that minute. This is directly doable in linear time with respect to the total length of all intervals in minute i. Start with a variable "possible" set to true. Then for each interval j, we can check for multiples of F with the following for-loop in \mathcal O(B_{i,j}-A_{i,j}) time.

for (int x = F; x <= B; x += F)
  if (A <= x && x <= B)
    possible = true;

However, since A and B may be up to 1 billion, this method will clearly not run in time. We can try mitigating this slowness with tricks like looping starting from \max(F, A), or even generating the multiples of F to see if each falls between A and B like the following:

for (int x = A; x <= B; x++)
  if (F % x == 0)
    possible = true;

But ultimately, it will still be futile since for values like F = 2 on intervals [1, 10^9], we still have to loop 500 million times. The insight required to overcome this bottleneck is ultimately this: Suppose we want to get the smallest multiple of F greater than A. If we take \lfloor \frac A F \rfloor and multiply it by F again, then we are guaranteed to get a value that is no greater than A (since \lfloor \frac A F \rfloor \le \frac A F) and a multiple of F (since we just multiplied it by F). Simply adding F to the result, we will get the first multiple of F greater than A. Then, we can just check if this is less than or equal to B. This is sufficient to solve the problem (since we know how to determine whether a multiple is in any interval in constant time), but if we want something more compact, then we can play around with the idea to finally deduce that each interval contains at least one multiple of F if and only if \lfloor \frac{A-1}{F} \rfloor = \lfloor \frac B F \rfloor.

Case 3 occurs when neither case 1 nor case 2 is true, so we can simply do nothing after we determined so. Since every interval and question can be processed in constant time, the above algorithm will have a running time complexity of \mathcal O((M_1+M_2+\dots+M_N)+Q).


Comments

There are no comments at the moment.