TAP2012H - High Mountains

To go on holidays, Horacio and Hernan have sacrificed their participation in an important programming contest. While you are in this contest, they are close to the Andes driving along Highway 40, in Argentina, enjoying a pleasant view of the mountains in the horizon. Right now the sky over the highway is a clear, uniform blue, while the visible part of the mountains is a profile presenting rich and attractive textures. This worries Horacio and Hernan, because they fear that the pictures they are taking will be very expensive to print correctly. For this reason, in the next stop they will take out their laptops and write a program to estimate the area of the mountain profile that has to be printed in each picture. Can you finish this program before them?

Horacio and Hernan will be modeling the mountain profile in the following way. Each mountain is represented by an isosceles triangle whose base rests on the X axis of the XY plane. Two equal-length sides connect the endpoints of the base to the opposite vertex of the triangle, which is the tip of the corresponding mountain. To describe the position and shape of the triangle, we use the coordinates along the X axis of the base endpoints, along with the height of the mountain.

The figure below is the model of a mountain profile formed by 4 mountains that are overlapping with one another. The area of the surface of the mountain profile that you have to calculate is marked with stripes. The lowest mountain of the figure is described by the values I = 4 (the left endpoint of the mountain base), D = 5 (the right endpoint of the mountain base) and H = 1 (the height of the

mountain)

To go on holidays, Horacio and Hernan have sacrificed their participation in an important programming contest. While you are in this contest, they are close to the Andes driving along Highway 40, in Argentina, enjoying a pleasant view of the mountains in the horizon. Right now the sky over the highway is a clear, uniform blue, while the visible part of the mountains is a profile presenting rich and attractive textures. This worries Horacio and Hernan, because they fear that the pictures they are taking will be very expensive to print correctly. For this reason, in the next stop they will take out their laptops and write a program to estimate the area of the mountain profile that has to be printed in each picture. Can you finish this program before them?

Horacio and Hernan will be modeling the mountain profile in the following way. Each mountain is represented by an isosceles triangle whose base rests on the X axis of the XY plane. Two equal-length sides connect the endpoints of the base to the opposite vertex of the triangle, which is the tip of the corresponding mountain. To describe the position and shape of the triangle, we use the coordinates along the X axis of the base's endpoints, along with the height of the mountain.

The figure below is the model of a mountain profile formed by 4 mountains that are overlapping with one another. The area of the mountain profile that you have to calculate is marked with stripes. The lowest mountain of the figure is described by the values I = 4 (the left endpoint of the mountain base), D = 5 (the right endpoint of the mountain base) and H = 1 (the height of the mountain).

In this problem, you are given the representation of the mountain profile, and you have to find the area of the union of all the corresponding triangles, in such a way that the overlapping parts are counted only once.

Input

Each test case is described using several lines. The first line contains a single integer number N, indicating the number of mountains (1 ≤ N ≤ 1000). Each of the N following lines describes a mountain using three integer numbers I, D and H, representing respectively the X coordinate of the left endpoint of the base, the same for the right endpoint of the base, and the height of the mountain (1 ≤ I, D, H ≤ 105 with I < D). In each test case there are no two mountains that are exactly the same (that is, with equal values for the three parameters I, D and H). The end of the input is signalled by a line containing the number -1.

Output

For each test case, you should print a single line containing a rational number, representing the area of the corresponding mountain profile. Round the result to the closest rational with two decimal digits. In case of ties, round up. Note that you should always use exactly two digits after the decimal dot, even if this means ending with a zero.

Hi...
my solution is O(n^2) over the number of segments, I'm looking for the intersection points of those segments, and I just take those over the upper segments, then I compute the area with the sum of trapeze area. But for the test number 5 my answer is 8653650000.00.
Why happen that ? is my approach correct ?