Editorial for COCI '15 Contest 1 #5 Relativnost
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us first solve the task for the case when there are no changes in requirements and we solve it under a constant number of requested colored paintings and black and white paintings . We need to determine how many ways there are to choose different configurations of selling the paintings with the requirement that at least paintings are colored. The initial problem when there are no changes in requirements is solved using dynamic programming. Let denote the number of ways to choose the initial transactions so that exactly requirements are satisfied with colored paintings. The relation used to calculate is:
In the end, the solution is the sum of all for each greater than or equal to . The calculation can be simplified even more if we define as the number of ways for not exactly colored paintings, but at least colored paintings. The solution that calculated this relation every time the requirements change has the complexity and was good enough for of total points.
In order to get all the points, you needed to find a way to efficiently maintain the relation value after changes have been made. The inspiration comes from the tournament tree. If you are not familiar with this structure, be sure to read the tutorial at https://wiki.xfer.hr/tournament/. (DMOJ curator's note: Good luck with this tutorial.) Under the assumption that now you know how tournament tree works, we continue solving the task.
The solution will be maintained in a way that every node of the tournament tree is used to store the number of ways to choose the paintings in that subtree for each number of chosen colored paintings from to .
We are left with maintaining that relation after we change the requirements of one person. It is evident that it is fairly simple to calculate the change in result for each node in the tournament tree. The question is how to join two nodes.
Here we can notice that the number of ways of choosing the transaction over the entire subtree (consisting of the left - and right child - ) with at least purchases of colored paintings is .
The total time complexity is .
Comments