Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17061 | Accepted: 4653 |
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
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 40011
using namespace std;
struct node
{
long long int lf,rf;
long long int cnt,ans;
long long int l,r;
}q[N<<3];
struct tt
{
long long int x1;
long long int x2;
long long int y;
long long int flag;
}p[N<<3];
int n;
int k;
long long int pnum[N<<2];
bool cmp(tt a,tt b)
{
if(a.y == b.y)
{
return a.x1 < b.x1;
}
return a.y<b.y;
}
void build(long long int l,long long int r,long long int rt)
{
q[rt].l = l;
q[rt].r = r;
q[rt].ans = 0;
q[rt].cnt = 0;
q[rt].lf = pnum[l];
q[rt].rf = pnum[r];
if(l + 1 == r)
{
return ;
}
int mid = (l+r)>>1;
build(l,mid,rt<<1);
build(mid,r,rt<<1|1);
}
void updata(long long int rt)
{
if(q[rt].ans > 0)
{
q[rt].cnt = q[rt].rf - q[rt].lf;
return ;
}
else
{
q[rt].cnt = 0;
}
if(q[rt].l + 1 == q[rt].r)
{
return ;
}
else
{
q[rt].cnt = q[rt<<1].cnt + q[rt<<1|1].cnt;
}
}
void insert(long long int rt,tt dot)
{
if(q[rt].lf == dot.x1 && q[rt].rf == dot.x2)
{
q[rt].ans += dot.flag;
updata(rt);
return ;
}
if(q[rt<<1].rf>=dot.x2)
{
insert(rt<<1,dot);
}
else if(q[rt<<1|1].lf<=dot.x1)
{
insert(rt<<1|1,dot);
}
else
{
struct tt pt = dot;
pt.x2 = q[rt<<1].rf;
insert(rt<<1,pt);
pt = dot;
pt.x1 = q[rt<<1|1].lf;
insert(rt<<1|1,pt);
}
updata(rt);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
long long int x1,x2,y;
k = 1;
for(int i=0;i<n;i++)
{
scanf("%lld%lld%lld",&x1,&x2,&y);
p[k].x1 = x1;
p[k].x2 = x2;
p[k].y = 0;
p[k].flag = 1;
pnum[k] = x1;
k++;
p[k].x1 = x1;
p[k].x2 = x2;
p[k].y = y;
p[k].flag = -1;
pnum[k] = x2;
k++;
}
k--;
sort(p+1,p+k+1,cmp);
sort(pnum+1,pnum+1+k);
build(1,k,1);
insert(1,p[1]);
long long int sum = 0;
for(int i=2;i<=k;i++)
{
sum += q[1].cnt * (p[i].y - p[i-1].y);
insert(1,p[i]);
}
printf("%lld\n",sum);
}
return 0;
}