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.
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 from description);
- crossing point of one triangle horizontal edge with others hypotenuse;
- rightmost vertex of some triangle (value from task description).
If we look at these event points consecutively, we can calculate common area increasing, if we at each event point know number of actual line segments and sum of their lengths , where 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 can be calculated as . Performing sweeping from left to right and summing up these increments we finally obtain total common area of all triangles.
Comments