离散化+线段树 POJ 3277 City Horizon

时间:2022-11-13 22:51:15

POJ 3277 City Horizon

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 18466

Accepted: 5077

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

4
2 5 1
9 10 4
6 8 2
4 6 3

Sample Output

16

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.
 /*做法:因为所有矩形的都在一个X轴上,就把X方向是做线段树,每一个进行区间覆盖,最后查询到单点,计算总的面积,因为以X轴的坐标建立线段树,空间过大,可以离散化,用不超过40000个点表示坐标*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 40010
typedef long long ll;
ll zl[N],yl[N],hig[N],seg[N<<];
int n,len,cnt=;
struct Tree{
ll hi;
int l,r;
}tree[N<<];
void build_tree(int l,int r,int k)
{
tree[k].r=r;
tree[k].l=l;
tree[k].hi=;
if(l+==r) return;
int mid=(l+r)>>;
build_tree(l,mid,k<<);
build_tree(mid,r,k<<|);
}
void bing(int l,int r,ll z,ll y,int i,int k)
{
if(seg[l]>=z&&seg[r]<=y)
{
if(hig[i]>tree[k].hi)
tree[k].hi=hig[i];
return ;
}
int mid=(l+r)>>;//进行区间覆盖的时候,要看清楚条件,去覆盖哪段区间
if(y<=seg[mid])
bing(l,mid,z,y,i,k<<);
else if(z>=seg[mid])
bing(mid,r,z,y,i,k<<|);
else {
bing(l,mid,z,y,i,k<<);
bing(mid,r,z,y,i,k<<|);
}
}
ll Solve(int h,int k,int l,int r)
{
if(tree[k].hi<h)
tree[k].hi=h;/*相当于懒惰标记,是否父亲区间的h改变,但是孩子区间的高度没有变*/
if(l+==r)/*求面积并,必须查询到单点,因为小区间的高度>=大区间的高度*/
return (ll)(seg[r]-seg[l])*tree[k].hi;/*因为最后是这种形式,所以必须是前闭后开区间,全闭区间是没法做的。*/
int mid=(l+r)>>;
ll zzz=Solve(tree[k].hi,k<<,l,mid);
ll yyy=Solve(tree[k].hi,k<<|,mid,r);
return zzz+yyy;
}
int main()
{
while(scanf("%d",&n)==)
{
seg[]=;
for(int i=;i<=n;++i)
{
//cin>>zl[i]>>yl[i]>>hig[i];
scanf("%d%d%d",&zl[i],&yl[i],&hig[i]);
seg[++seg[]]=zl[i];
seg[++seg[]]=yl[i];
}
sort(seg+,seg+seg[]+);
cnt=unique(seg+,seg+seg[]+)-seg-;/*去重,用最少的节点*/
memset(tree,,sizeof());
build_tree(,cnt,);/*不是build_tree(1,cnt+1,1);因为这样会存在[cnt,cnt+1),这个区间是不存在的,因为没有cnt+1这个点,再去求面积的话,会出来负数*/
for(int i=;i<=n;++i)
bing(,cnt,zl[i],yl[i],i,);
cout<<Solve(,,,cnt);
}
return ;
}