Editorial for COCI '16 Contest 5 #3 Unija


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.

It is enough to observe only the right half of the image and in the end double the result. The solution is to sum the heights of X-columns for X = 1, 2, \dots, 10^7, which can be done using a for loop.

How to determine the height of the column at coordinate X? It is larger than or equal to the height of column at coordinate X+1. It is larger if a rectangle exists that ends at coordinate X and is higher than the column at coordinate X+1, otherwise it is equal. Therefore, initially, we need to store the X-coordinates where the given rectangles end and their heights, then traverse the X-columns "backwards", towards the beginning, in order to apply the aforementioned formula.


Comments

There are no comments at the moment.