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
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
16YYYYYYYYYYYYYYYYYYYYYYYYYY
注意各种__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;
}