Editorial for WC '15 Contest 4 S4 - Stakeout


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.

For starters, we need to determine which buildings each agent can see. After sorting the positions of the buildings in \mathcal O(N \log N) time, we can determine the interval of buildings that each agent i can cover in \mathcal O(\log N) time using binary search, by searching for the first building whose position is no smaller than A_i-R_i, and the last building whose position is no greater than A_i+R_i.

As for answering the questions, we can observe that 2^i > 2^1+2^2+\dots+2^{i-1}, meaning that for any i, hiring any subset of agents with indices smaller than i is worth it if it means we can avoid hiring agent i. This suggests the approach of iterating over the agents downwards from M to 1, and for each one, only hiring them if necessary – in other words, if skipping that agent and hiring all of the agents with indices smaller than i wouldn't yield sufficient coverage of the buildings.

To implement this approach, we can start by assuming that we'll hire all M agents, and then for each agent i, we can try removing them and testing if each building is still covered by enough agents – if not, agent i must be necessary, so we must re-insert them into the set of hired agents and add 2^i onto the total necessary cost (assuming that we've precomputed the values of 2^i \bmod 10^9+7 for i = 1 \dots M). If even hiring all M agents to start with isn't sufficient, then the answer can immediately be determined to be -1.

What remains is being able to execute the above operations efficiently. In particular, we must be able to insert and remove agents from the set of hired agents, and test if all buildings are in sight of at least C_i hired agents. If we imagine an array S such that S_i is the number of hired agents that building S is in sight of, then these operations equate to adding 1 to an interval of S values, subtracting 1 from an interval of S values, and determining the minimum S value in the array.

Now, each of these operations can be handled by a segment tree with lazy propagation in \mathcal O(\log N) time. Each node in the tree should store the minimum value in its interval, as well as a lazy value of how much should be added to (or subtracted from) its entire interval. Conveniently, adding a constant value c to every index in an interval results in that interval's minimum value also increasing by c.

For each question, we hire all of the agents and then iterate over all of them in an attempt to remove them, performing one or two segment tree operations each time. Therefore, the total time complexity is \mathcal O((N+QM) \log N).


Comments

There are no comments at the moment.