#SGM0000039. Đường chân trời (SKYLINE)
Đường chân trời (SKYLINE)
SKYLINE
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Buildings are added from back to front. For each building, count the horizontal length where its height is at least the skyline behind it, then update the skyline.
Input
The first line contains . Each of the next lines contains . The building covers the half-open horizontal interval at height . Buildings are listed from back to front.
Output
Print one integer: the total overlap. At each horizontal position, the current building contributes if is at least the skyline height before inserting that building.
Subtasks
- 20 points: .
- 30 points: .
- 50 points: , , ; total overlap is at most .
Examples
Input
3
5 11 3
1 10 1
3 13 2
Output
14
Explanation
The first building contributes 6. The second contributes only on , length 4. The third contributes on and , another 4. Thus the total is .