Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and admire the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line, and $N$ buildings stand along it ($1 \le N \le 40{,}000$). Building $i$'s silhouette has a base that spans the horizon from position $A_i$ to $B_i$ ($1 \le A_i < B_i \le 1{,}000{,}000{,}000$) and has height $H_i$ ($1 \le H_i \le 1{,}000{,}000{,}000$). Every building rests on the ground ($y = 0$).
Determine the area, in square units, of the combined silhouette formed by all $N$ buildings — that is, the area covered by the union of all the rectangles. Overlapping regions must be counted only once.
In the example above, the first building overlaps with the fourth building over an area of $1$ square unit. Counting the overlap only once, the total area is $3 \times 1 + 1 \times 4 + 2 \times 2 + 2 \times 3 - 1 = 16$.