TOJ 1706: City Horizon -- 线段树,平衡树

来源:互联网 发布:js自动答题代码 编辑:程序博客网 时间:2024/06/06 02:15

1706: City Horizon

Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte
Total Submit: 44            Accepted:11

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. Buildingi's silhouette has a base that spans locationsAi throughBi along the horizon (1 ≤ Ai <Bi ≤ 1,000,000,000) and has heightHi (1 ≤Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by allN 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 allN buildings

Sample Input

42 5 19 10 46 8 24 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 Open 2007


分析:这道题用线段树做很简单,不过偶然看到自己以前写的代码,那时还不知道线段树,甚至不知道STL。。。于是排序算法和树操作都是自己写的(只是普通的二叉树,那时应该也不会写平衡树。。。)反正就这样把这题给写出来了。。。

思路很简单,把所有坐标排序,然后从左向右扫描,始终记录当前最高高度。遇到区间起点就向树中插入结点,同时更新高度,遇到区间终点就删除结点,同时找到现有的最大高度。因为要用DELETE和EXTRACT-MAX操作,就用的二叉树。

0 0