City Horizon
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15769 | Accepted: 4276 |
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
Output
Sample Input
4 2 5 1 9 10 4 6 8 2 4 6 3
Sample Output
16
Hint
Source
#include <iostream> #include <cstring> #include <cmath> #include <cstdio> #include <algorithm> #include <cstdlib> #define N 41000 using namespace std; int y[N*2]; struct num { int x; int y1,y2,flag; }a[N*2]; struct Num { int l,r,tl,tr,sum,tlen; }b[N*8]; bool cmp(num p1,num p2) { return p1.x<p2.x; } int main() { //freopen("data.txt","r",stdin); void build(int k,int l,int r); void update(int k,num p); int n; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { int x1,x2; int y2; scanf("%d %d %d",&x1,&x2,&y2); y[i*2-1] = 0; y[i*2] = y2; a[i*2-1].x = x1; a[i*2-1].y1 = 0; a[i*2-1].y2 = y2; a[i*2-1].flag = 1; a[i*2].x = x2; a[i*2].y1 = 0; a[i*2].y2 = y2; a[i*2].flag = -1; } n = n * 2; sort(y+1,y+n+1); sort(a+1,a+n+1,cmp); build(1,1,n); update(1,a[1]); __int64 s=0; for(int i=2;i<=n;i++) { s+=(__int64)(a[i].x-a[i-1].x)*(__int64)(b[1].tlen); update(1,a[i]); } printf("%I64d\n",s); } return 0; } void build(int k,int l,int r) { b[k].l =l; b[k].r = r; b[k].tl = y[l]; b[k].tr = y[r]; b[k].sum = b[k].tlen=0; if(l+1==r) { return ; } int mid = (l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid,r); } void cal(int k) { if(b[k].sum>0) { b[k].tlen = b[k].tr - b[k].tl; return ; } if(b[k].l+1==b[k].r) { b[k].tlen = 0; }else { b[k].tlen = b[k<<1].tlen+b[k<<1|1].tlen; } } void update(int k,num p) { if(b[k].tl==p.y1&&b[k].tr==p.y2) { b[k].sum+=p.flag; cal(k); return ; } if(b[k<<1].tr>=p.y2) { update(k<<1,p); }else if(b[k<<1|1].tl<=p.y1) { update(k<<1|1,p); }else { num p1 = p; p1.y2 = b[k<<1].tr; update(k<<1,p1); p1 = p; p1.y1 = b[k<<1|1].tl; update(k<<1|1,p1); } cal(k); }
POJ 3277 City Horizon,布布扣,bubuko.com
原文:http://blog.csdn.net/yongxingao/article/details/22694175