#P2003. Supporting Pillars in Floating Boards
Supporting Pillars in Floating Boards
Supporting Pillars in Floating Boards
In a game design, several boards are placed in fixed positions in the coordinate plane. Each board is described by its vertical distance from the floor (its height) and its horizontal span, given by a start and an end coordinate. To ensure that no board "floats in mid-air," each board must be supported at both of its ends. Specifically, for each board, its left support position is located at (x = L + 0.5) and its right support position at (x = R - 0.5). At each of these positions, if there is another board underneath (i.e. a board with a strictly lower height) that covers that x‐coordinate (its horizontal interval ([L, R]) contains the point), then the board above is considered to be supported by the board below. In this case, a supporting pillar is built from the upper board down to the top surface of the supporting board, and its length is the difference of their heights. Otherwise, if no board underneath covers the required support position, a supporting pillar is built from the board down to the floor (height 0), so its length equals the board's height.
The task is to calculate the total length of all the supporting pillars needed for all boards.
The formula for the pillar length at a support position for a board with height (h) is given by: [ \text{pillar_length} = \begin{cases} h - h_{support} & \text{if there exists a board below with maximum height}, h_{support} \ h & \text{otherwise} \end{cases} ] where the board below must satisfy (L \leq (\text{support position}) \leq R) and (h_{support} < h).
inputFormat
The input begins with an integer (n) ((n \ge 1)) denoting the number of boards. Each of the following (n) lines contains three space-separated numbers (h), (L), and (R) where (h) (a positive number) is the board's height (vertical distance from the floor) and (L) and (R) (with (L < R)) denote the horizontal positions of the board's start and end respectively.
outputFormat
Output a single number representing the total length of all supporting pillars needed to support all boards. The result should be accurate (if using floating point arithmetic, an absolute or relative error of 10^{-6} is acceptable).
sample
2
3 0 5
5 1 4
10.0