City Horizon

来源:互联网 发布:安装补丁的软件 编辑:程序博客网 时间:2024/05/16 10:19


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: AiBi, and Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all N buildings

Sample Input

42 5 19 10 46 8 24 6 3

Sample Output

16
YYYYYYYYYYYYYYYYYYYYYYYYYY
注意各种__int64;
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;int m;struct node{__int64 x,y,z,ans;}g[40005];__int64 cmp(node a,node b){if(a.z!=b.z)return a.z<b.z;if(a.y!=b.y)return a.y>b.y;if(a.x!=b.x)return a.x<b.x;}void cover(__int64 l,__int64 r,int h,int k){while(h<m&&(l>=g[h].y||r<=g[h].x))    h++;if(h>=m||r<=l){g[k].ans+=r-l;//当连续的一段被分成2部分是必须加一起;wa了n次;return;}if(l>=g[h].x&&r<=g[h].y)    return;if(l<g[h].x){cover(l,g[h].x,h+1,k);}if(r>g[h].y)    cover(g[h].y,r,h+1,k);}int main(){int i,j,k,n;while(scanf("%d",&m)!=EOF){memset(g,0,sizeof(g));for(i=0;i<m;i++){scanf("%I64d%I64d%I64d",&g[i].x,&g[i].y,&g[i].z);}//printf("\n\n");sort(g,g+m,cmp);for(i=m-1;i>=0;i--)    cover(g[i].x,g[i].y,i+1,i);__int64 sum=0;/*for(i=0;i<m;i++)printf("%I64d %I64d %I64d\n",g[i].x,g[i].y,g[i].z);*//*for(i=0;i<m;i++)    printf("%d \n",g[i].ans);*/for(i=0;i<m;i++){if(g[i].ans)sum+=g[i].ans*g[i].z;}printf("%I64d\n",sum);}return 0;}


原创粉丝点击