July 12 - ADA Training

City Horizon

Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.

The entire horizon is represented by a number line with n (1n40000) buildings. Building i's silhouette has a base that spans locations ai through bi along the horizon (1ai < bi109) and has height hi (1hi109). Determine the area, in square units, of the aggregate silhouette formed by all n buildings.


The first line contains a single integer n. Each of the next n lines describes building i with three space-separated integers: ai, bi and hi.


Print the total area, in square units, of the silhouettes formed by all n buildings.

Time limit 1 second
Memory limit 128 MiB
Input example #1
2 5 1
9 10 4
6 8 2
4 6 3
Output example #1
Source 2007 USACO US Open, Silver Division