Editorial for WC '17 Finals S4 - Ultimatum
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's start by considering how to evaluate the answer to a given query at all, before worrying about how to do so efficiently. Let be the height of the -th tallest skyscraper. Next, let be the total number of height- skyscrapers, and let be the number of height- skyscrapers which will be vaporized. Note that this means that, for any given height- skyscraper, the probability that it'll be vaporized is . The answer can then be expressed as the sum of the following values:
- The total number of citizens in the interval .
- The expected number of citizens in vaporized height- skyscrapers outside the interval . Note that this is equal to multiplied by the total number of citizens in height- skyscrapers outside the interval , due to linearity of expectation.
- The total number of citizens in skyscrapers taller than outside the interval .
Naively, at least values 2 and 3 take time to compute per query, so next we'll need to look for an efficient way to share computations between queries. These two values depend on , which depends on , which suggests that processing the queries in non-decreasing order of may be most convenient. So, we'll want to input all of them, sort them by , compute and store their answers, and output them in the original order at the end.
Let's imagine that starts off at . Then, each time increases by one, either decreases to a new smaller value (at which point we can count its corresponding value of , and set to ), or stays constant and increases by one. This means that we can easily keep track of the portion of the answer. The tricky remaining part is being able to look up interval sums of values efficiently, for three types of skyscrapers – all of them, only ones with exactly the current height , and only ones taller than height . An advanced data structure such as a binary indexed tree (or a more general segment tree) will do the trick here. For example, we can maintain three binary indexed trees, one for each of the three types of skyscrapers mentioned above, and update them appropriately whenever decreases. In total, each value will be inserted and removed at most once into each of the binary indexed trees, with a time complexity of each, and each binary indexed tree will be queried once per query, also with a time complexity of each. Overall, this approach can achieve a time complexity of .
Comments