Editorial for Baltic OI '02 P3 - Triangles


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.

We can use a plane sweep method sweeping from left to right. There are three kinds of event points for the algorithm (denoted in Figure 1 by gray arrows):

  • beginning of new triangle (value xi from description);
  • crossing point of one triangle horizontal edge with others hypotenuse;
  • rightmost vertex of some triangle (value xi+mi from task description).

If we look at these event points consecutively, we can calculate common area increasing, if we at each event point xk know number nk of actual line segments and sum of their lengths sk=Δyi, where Δyi is the length of one segment. Example with three segments are shown in Figure 2.

Then increment for each gap between two neighbour event points (Δx=xk+1xk) can be calculated as skΔxnk(Δx)22. Performing sweeping from left to right and summing up these increments we finally obtain total common area of all triangles.


Comments

There are no comments at the moment.