Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15636 | Accepted: 4222 |
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
Lines 2.. N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
Sample Input
4 2 5 1 9 10 4 6 8 2 4 6 3
Sample Output
16
Hint
Source
从CTrip编程大赛回来之后,发现是该象征性的准备下接下来的各种商业赛了……
老题了,给n个建筑,每个建筑的起点终点和高度,求所有建筑的面积和。
离散化,然后单调队列搞搞就可以了。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
struct Build
{
int l,r;
long long val;
};
struct Hash
{
int val,num;
};
Build build[40006];
Hash hash[100000];
int up;
int inout[40005];
struct cmp
{
bool operator()(const Hash &x,const Hash &y)
{
if (build[x.num].val!=build[y.num].val) return build[x.num].val<build[y.num].val;
return x.num<y.num;
}
};
priority_queue<Hash,vector<Hash>,cmp> q;
bool Cmp(Hash x,Hash y)
{
if (x.val!=y.val) return x.val<y.val;
return x.num<y.num;
}
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
up=0;
for (i=0;i<n;i++)
{
scanf("%d%d%lld",&build[i].l,&build[i].r,&build[i].val);
hash[up].val=build[i].l;
hash[up++].num=i;
hash[up].num=i;
hash[up++].val=build[i].r;
}
sort(hash,hash+up,Cmp);
memset(inout,0,sizeof(inout));
long long ans=0;
int last=0;
for (i=0;i<up;i++)
{
// printf("For No.%d: \n",i);
if (!q.empty())
{
ans+=(hash[i].val-last)*build[q.top().num].val;
// printf("%d %d %d\n",hash[i].val,last,build[q.top().num].val);
}
last=hash[i].val;
if (inout[hash[i].num]==0)
{
q.push(hash[i]);
inout[hash[i].num]=1;
}
else
{
inout[hash[i].num]=0;
}
while(!q.empty())
{
if (inout[q.top().num]==0) q.pop();
else break;
}
}
printf("%lld\n",ans);
}
return 0;
}