bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(离散化+扫描线)
2016-08-09 16:51
585 查看
1645: [Usaco2007 Open]City Horizon 城市地平线
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 541 Solved: 255
[Submit][Status][Discuss]
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 buildingsSample Input
42 5 1
9 10 4
6 8 2
4 6 3
Sample Output
16OUTPUT 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
Silver[Submit][Status][Discuss]
题解:扫描线+线段树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 200000
#define LL long long
using namespace std;
int n,m;
LL len[N*4],h
,ans;
int a
,b
,v
,cnt,num
,tot,tree[N*4];
struct data
{
int l,r,x,pd;
}c
;
int cmp(int x,int y)
{
return h[x]<h[y];
}
int cmp1(data a,data b)
{
return a.x<b.x;
}
void update(int now,int l,int r)
{
if (tree[now]>0)
len[now]=(LL)(v[r]-v[l-1]);
else if (l==r) len[now]=0;
else len[now]=len[now<<1]+len[now<<1|1];
}
void qjchange(int now,int l,int r,int ll,int rr,int v)
{
if (l>=ll&&r<=rr)
{
tree[now]+=v;
update(now,l,r);
return;
}
int mid=(l+r)/2;
if (ll<=mid) qjchange(now<<1,l,mid,ll,rr,v);
if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,v);
update(now,l,r);
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
int x,y; scanf("%d%d",&x,&y);
++tot; c[tot].l=1; c[tot].x=x; c[tot].pd=1;
++tot; c[tot].l=1; c[tot].x=y; c[tot].pd=-1;
scanf("%lld",&h[i]); b[i]=i;
}
b[n+1]=n+1; h[n+1]=1;
b[n+2]=n+2; h[n+2]=0;
sort(b+1,b+n+3,cmp);
h[b[0]]=1000000001;
for (int i=1;i<=n+2;i++)
if (h[b[i]]!=h[b[i-1]])
++cnt,v[cnt]=h[b[i]],num[b[i]]=cnt;
else num[b[i]]=cnt;
for (int i=1;i<=2*n;i++)
c[i].r=num[(i-1)/2+1],c[i].l=num[n+1];
sort(c+1,c+2*n+1,cmp1);
for (int i=1;i<=2*n;i++)
{
if (i!=1) ans+=(LL)(c[i].x-c[i-1].x)*len[1];
qjchange(1,1,cnt,c[i].l,c[i].r,c[i].pd);
}
printf("%lld\n",ans);
}
相关文章推荐
- 【bzoj1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(线段树扫描线)
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线(扫描线+线段树)
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
- bzoj1645 [Usaco2007 Open]City Horizon 城市地平线
- bzoj1645 [Usaco2007 Open]City Horizon 城市地平线
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
- 【BZOJ】1645: [Usaco2007 Open]City Horizon 城市地平线(线段树+特殊的技巧)
- BZOJ1645 [Usaco2007 Open]City Horizon 城市地平线
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线【线段树】【矩形面积并】
- 1645: [Usaco2007 Open]City Horizon 城市地平线
- BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线
- 1645: [Usaco2007 Open]City Horizon 城市地平线 (并查集)
- [Usaco2007 Open]City Horizon 城市地平线
- 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
- 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)
- 【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)
- bzoj1646 [Usaco2007 Open]Catch That Cow 抓住那只牛
- 【bzoj1711】[Usaco2007 Open]Dining吃饭