[POJ] 3277. City Horizon
Description
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 (1 ≤ N ≤ 40,000) buildings. building I's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai <Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000 ). determine the area, in square units, of the aggregate silhouette formed by all N buildings.
Input
Line 1: A single integer: N
Lines 2. N + 1: Input line I + 1 describes building I with three space-separated integers: Ai, Bi, and Hi
Output
Line 1: The total area, in square units, of the silhouettes formed by all N buildings
Sample Input
4
2 5 1
9 10 4
6 8 2
4 6 3
Sample Output
16
Hint
The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3-1 = 16.
Source
USACO 2007 Open Silver
Theme
There are N buildings on a horizontal line, all of which are rectangular and can be covered by each other. Given the coordinate values of the left and right sides of each building, Ai, Bi, and high Hi of each building, we need to calculate the total coverage area of these buildings.
Subject data Scope:
Number of buildings N: 1 <= N <= 40000
Building coordinate value Ai, Bi: 1 <= Ai, Bi <= 10 ^ 9
Building Height Hi: 1 <= Hi <= 10 ^ 9
VczixL/Platform + o6zL + dPQtcS + Platform + C1yKGjy/nS1KOsztLDx7/platform/fyc + 1xM/platform/M9c/Platform + platform/ ydLUwPvTw8/large + a7/large + large/large + bG + y7zCt8fls/large/W1NrAtL + 8wse + large + 08nT2szixL/large + 1xL/large + kernel + tcTLvM/kernel/W0NKyyse + rbOju + kernel/kernel + 6687zGy + kernel/ix6ta1sqK0 + kernel/cz1z9 + kernel/kernel + release/release + release/release + vWrsewvbK5/bXEs/release/W1NrDv7j2z9 + release/m0 + release + 2zrxeykjwtag1_7 + release/release + release/ m0 + rHttcTP37bOo6zEx8O0sci9z9Xiwb249s/second + 2 zrXEyKjWtdChu/second + CgoKCjxwcmUgY2xhc3M9 "brush: java; ">// Insert void Insert (int left, int right, int height, int num) {// if the Inserted Line Segment completely overwrites the line segment represented by the current node if (intervalTree [num]. left = left & intervalTree [num]. right = right) {// update the weight (height) if (intervalTree [num]. height
At last, when calculating the area, we need to traverse the entire line segment tree, because in this way, we can determine the path from each root node to the leaf node, that is, each line segment (such as [, a + 1) line segment), the maximum height is. In the statistical process, the maximum height is retrieved from the root to the leaf. The calculation results of non-leaf nodes are the sum of the calculation results of the left and right subnodes. The implementation code is as follows (because the calculated data exceeds the int range, the long data type is used ):
// Calculated area long Cal (int h, int num) {// the statistical process traverses from the root to the leaf, calculate the maximum if (h> builds [num] In the traversal process. height) {builds [num]. height = h;} // if // Calculate if (builds [num] on the leaf node. left + 1 = builds [num]. right) {long area = builds [num]. height * (hash [builds [num]. right]-hash [builds [num]. left]); return area;} // if // The calculation result of a non-leaf node is the sum of the calculation results of the left and right subnodes. return Cal (builds [num]. height, 2 * num) + Cal (builds [num]. height, 2 * num + 1 );}