1645: [Usaco2007 Open]City Horizon 城市地平线

1645: [Usaco2007 Open]City Horizon 城市地平线

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 315  Solved: 157
[Submit][Status]

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 A_i through B_i along the
horizon (1 <= A_i < B_i <= 1,000,000,000) and has height H_i (1
<= H_i <= 1,000,000,000). Determine the area, in square units, of
the aggregate silhouette formed by all N buildings.

N个矩形块,交求面积并.

Input

* Line 1: A single integer: N

* Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i

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

OUTPUT DETAILS:

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.

HINT

Source

题解:
这题的思路比较巧妙。
全部的图形被分成了2*n-1个矩形,所以只要用线段树维护每一个矩形的高即可,取max
代码:(copy)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define inf 10000000000
using namespace std;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int x[],y[],val[],disc[];
struct seg{int l,r,mx,tag;}t[];
int find(int x)
{
int l=,r=*n;
while(l<=r)
{
int mid=(l+r)>>;
if(disc[mid]<x)l=mid+;
else if(disc[mid]==x)return mid;
else r=mid-;
}
}
void pushdown(int k)
{
if(t[k].l==t[k].r)return;
int tag=t[k].tag;t[k].tag=;
if(tag)
{
t[k<<].tag=max(t[k<<].tag,tag);
t[k<<|].tag=max(t[k<<|].tag,tag);
t[k<<].mx=max(t[k<<].mx,tag);
t[k<<|].mx=max(t[k<<|].mx,tag);
}
}
void build(int k,int l,int r)
{
t[k].l=l;t[k].r=r;
if(l==r)return;
int mid=(l+r)>>;
build(k<<,l,mid);build(k<<|,mid+,r);
}
void update(int k,int x,int y,int val)
{
pushdown(k);
int l=t[k].l,r=t[k].r;
if(l==x&&y==r)
{
t[k].tag=val;t[k].mx=max(t[k].mx,val);
return;
}
int mid=(l+r)>>;
if(y<=mid)update(k<<,x,y,val);
else if(x>mid)update(k<<|,x,y,val);
else
{
update(k<<,x,mid,val);update(k<<|,mid+,y,val);
}
}
int query(int k,int x)
{
pushdown(k);
int l=t[k].l,r=t[k].r;
if(l==r)return t[k].mx;
int mid=(l+r)>>;
if(x<=mid)return query(k<<,x);
else return query(k<<|,x);
}
int main()
{
n=read();build(,,n<<);
for(int i=;i<=n;i++)
{
x[i]=read(),y[i]=read(),val[i]=read();
disc[(i<<)-]=x[i];disc[i<<]=y[i];
}
sort(disc+,disc+(n<<)+);
for(int i=;i<=n;i++)
x[i]=find(x[i]),y[i]=find(y[i]);
for(int i=;i<=n;i++)
{
update(,x[i],y[i]-,val[i]);
}
ll ans=;
for(int i=;i<*n;i++)
{
ans+=(ll)query(,i)*(disc[i+]-disc[i]);
}
printf("%lld",ans);
return ;
}
上一篇:在java代码中进行px与dip(dp)、px与sp单位值的转换


下一篇:2016总结&2017计划