City Horizon
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16254 | Accepted: 4428 |
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: Ai, Bi, and Hi
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, 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
这道题和上一题是一样的,但要注意数组的大小,不要和人家一样RE了N次,~~~~(>_<)~~~~
<span style="font-size:18px;">#include <iostream> #include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; struct node { long long l,r,c; long long cnt,rf,lf; }tree[1200400]; struct Line { long long x,y1,y2; long long f; }line[1200400]; bool cmp(Line a,Line b) { return a.x<b.x; } long long y[1200400]; void build(long long root,long long l,long long r) { tree[root].l=l; tree[root].r=r; tree[root].cnt=tree[root].c=0; tree[root].lf=y[l]; tree[root].rf=y[r]; if(l+1==r) return; long long mid=(l+r)/2; build(root*2,l,mid); build(root*2+1,mid,r); } void calen(long long root) { if(tree[root].c>0) { tree[root].cnt=tree[root].rf-tree[root].lf; return ; } if(tree[root].l+1==tree[root].r) tree[root].cnt=0; else tree[root].cnt=tree[root*2].cnt+tree[root*2+1].cnt; } void renew(long long root,Line e) { if(e.y1==tree[root].lf&&e.y2==tree[root].rf) { tree[root].c+=e.f; calen(root); return; } if(e.y2<=tree[root*2].rf) renew(root*2,e); else if(e.y1>=tree[root*2+1].lf) renew(root*2+1,e); else { Line tmp=e; tmp.y2=tree[root*2].rf; renew(root*2,tmp); tmp=e; tmp.y1=tree[root*2+1].lf; renew(root*2+1,tmp); } calen(root); } int main() { long long n,i,cnt; long long x1,x2,y1,ans; while(scanf("%lld",&n)!=EOF) { cnt=1; ans=0; if(n==0) break; for(i=0;i<n;i++) { scanf("%lld%lld%lld",&x1,&x2,&y1); line[cnt].x=0; line[cnt].y1=x1; line[cnt].y2=x2; line[cnt].f=1; y[cnt]=x1; cnt++; line[cnt].x=y1; line[cnt].y1=x1; line[cnt].y2=x2; line[cnt].f=-1; y[cnt]=x2; cnt++; } //cnt--; sort(line+1,line+cnt,cmp); sort(y+1,y+cnt); build(1,1,cnt-1); renew(1,line[1]); for(i=2;i<cnt;i++) { ans+=tree[1].cnt*(line[i].x-line[i-1].x); renew(1,line[i]); } printf("%lld\n",ans); } return 0; } </span><span style="font-size:14px;"> </span>