POJ 3277 City Horizon
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 18466 |
|
Accepted: 5077 |
Description
Farmer John has taken he cows on a trips to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular Buildin Gs.
The entire horizon is represented by a number line with N (1≤ n ≤40,000) buildings. Building I's silhouette have 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, with 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
IWith three space-separated integers:
Ai,
Bi, and
Hi
Output
Line 1:the total area, in square units, of the silhouettes formed by all
NBuildings
Sample Input
42 5 19 10 46 8 24 6 3
Sample Output
16
Hint
The first building overlaps with the fourth building for a area of 1 square unit, so the total area is just 3*1 + 1*4 + 2 * * + 2*3-1 = 16.
1 /*Practice: Because all the rectangles are on an x-axis, the X-direction is to do the line segment tree, each of the interval coverage, the last query to a single point, calculate the total area, because the x-axis coordinates to establish a segment tree, space is too large, can be discretized, with no more than 40,000 points to represent the coordinates*/2#include <iostream>3 using namespacestd;4#include <cstdio>5#include <cstring>6#include <algorithm>7 #defineN 400108typedefLong Longll;9ll zl[n],yl[n],hig[n],seg[n<<1];Ten intN,len,cnt=0; One structtree{ A ll Hi; - intL,r; -}tree[n<<3]; the voidBuild_tree (intLintRintk) - { -Tree[k].r=R; -Tree[k].l=l; +Tree[k].hi=0; - if(L +1==R)return; + intMid= (l+r) >>1; ABuild_tree (l,mid,k<<1); atBuild_tree (mid,r,k<<1|1); - } - voidBingintLintR,ll Z,ll y,intIintk) - { - if(seg[l]>=z&&seg[r]<=y) - { in if(hig[i]>Tree[k].hi) -Tree[k].hi=Hig[i]; to return ; + } - intMid= (l+r) >>1;//when the interval is covered, you need to see the conditions to cover which interval the if(y<=Seg[mid]) *Bing (l,mid,z,y,i,k<<1); $ Else if(z>=Seg[mid])Panax NotoginsengBing (mid,r,z,y,i,k<<1|1); - Else { theBing (l,mid,z,y,i,k<<1); +Bing (mid,r,z,y,i,k<<1|1); A } the } +ll Solve (intHintKintLintR) - { $ if(tree[k].hi<h) $Tree[k].hi=h;/*is equivalent to the lazy mark, whether the father interval of H changes, but the height of the child interval has not changed*/ - if(L +1==R)/*to find the area and, must query to a single point, because the height of the inter-cell >= large interval height*/ - return(LL) (Seg[r]-seg[l]) *tree[k].hi;/*because this is the final form, it must be a front-closed interval, and the closed interval is impossible to do. */ the intMid= (l+r) >>1; -ll Zzz=solve (tree[k].hi,k<<1, l,mid);Wuyill Yyy=solve (tree[k].hi,k<<1|1, mid,r); the returnzzz+yyy; - } Wu intMain () - { About while(SCANF ("%d", &n) = =1) $ { -seg[0]=0; - for(intI=1; i<=n;++i) - { A //cin>>zl[i]>>yl[i]>>hig[i]; +scanf"%d%d%d",&zl[i],&yl[i],&hig[i]); theseg[++seg[0]]=Zl[i]; -seg[++seg[0]]=Yl[i]; $ } theSort (seg+1, seg+seg[0]+1); theCnt=unique (seg+1, seg+seg[0]+1)-seg-1;/*go heavy, with the fewest nodes*/ thememset (Tree,0,sizeof(0)); theBuild_tree (1Cnt1);/*is not Build_tree (1,cnt+1,1), because this will exist [cnt,cnt+1], this interval is not there, because there is no cnt+1 this point, then to seek the area, will come out negative*/ - for(intI=1; i<=n;++i) inBing1, Cnt,zl[i],yl[i],i,1); theCout<<solve (0,1,1, CNT); the } About return 0; the}
discretization + segment Tree POJ 3277 City Horizon