思路:线段树加上离散化
就是将各个坐标值排序,那么40000个buildings,就只有80000个点,这就是离散化的思想。
每次更新,就取更大的高度。可以将各个结构体按高度进行排序。
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
__int64 y[81000<<2];
struct node
{
__int64 l,r;
__int64 s,yl,yr;
int flag;
}tree[81000<<2];
struct w
{
__int64 x,yl,yr;
int flag;
}p[81000<<2],tem;
int cmp(struct w a,struct w b)
{
return a.x<b.x;
}
void build(__int64 left,__int64 right,__int64 root)
{
tree[root].l=left;
tree[root].r=right;
tree[root].s=tree[root].flag=0;
tree[root].yl=y[left];
tree[root].yr=y[right];
if(left+1==right)
return;
int mid=(left+right)>>1;
build(left,mid,root<<1);
build(mid,right,root<<1|1);
}
void len(__int64 t)
{
if(tree[t].flag>0)
{
tree[t].s=tree[t].yr-tree[t].yl;
return ;
}
if(tree[t].l+1==tree[t].r)
tree[t].s=0;
else
tree[t].s=tree[t<<1].s+tree[t<<1|1].s;
}
void update(__int64 t,w e)
{
if(e.yl==tree[t].yl&&e.yr==tree[t].yr)
{
tree[t].flag+=e.flag;
len(t);
return;
}
if(e.yr<=tree[t<<1].yr)
update(t<<1,e);
else if(e.yl>=tree[t<<1|1].yl)
update(t<<1|1,e);
else
{
w tmp=e;
tmp.yr=tree[t<<1].yr;
update(t<<1,tmp);
tmp=e;
tmp.yl=tree[t<<1|1].yl;
update(t<<1|1,tmp);
}
len(t);
}
int main()
{
int t,i,cnt;
__int64 x1,x2,y2;
while(~scanf("%I64d",&t))
{
cnt=1;
for(i=1;i<=t;i++)
{
int y1=0;
scanf("%I64d %I64d %I64d",&x1,&x2,&y2);
p[cnt].x=x1;
p[cnt].yl=y1;
p[cnt].yr=y2;
p[cnt].flag=1;
y[cnt++]=y1;
p[cnt].x=x2;
p[cnt].yl=y1;
p[cnt].yr=y2;
p[cnt].flag=-1;
y[cnt++]=y2;
}
sort(y+1,y+cnt);
sort(p+1,p+cnt,cmp);
build(1,cnt-1,1);
update(1,p[1]);
__int64 sum=0;
for(int i=2;i<cnt;i++)
{
sum+=tree[1].s*(p[i].x-p[i-1].x);
update(1,p[i]);
}
printf("%I64d\n",sum);
}
return 0;
}
这道题目和poj1151是一样的,我在1151的前提上改了一下,但是感觉让我自己写还是写不出来,弱爆了!!