设为首页 加入收藏

TOP

poj3277--City Horizon(线段树+离散化+扫描线)
2015-07-20 17:56:20 来源: 作者: 【 】 浏览:4
Tags:poj3277--City Horizon 线段 离散 扫描
City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16206 Accepted: 4414

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

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

Hint

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. 和poj1151相同,求解矩形的并的面积http://blog.csdn.net/winddreams/article/details/38495093 按x轴有左向右扫描,对y值进行离散化,为了将每个段连接起来,所以左右子树应该是[l,mid],[mid,r],每段的差是1,不是0,这样可以防止,忽略掉mid+1到mid这一段,然后判断,只有lazy大于0时,这一个节点的所有长度都加进去,不然节点大小 = 左右子树的和
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; #define maxn 81000 #define LL __int64 struct node1 { LL x, y1 , y2 ; LL flag ; } p[maxn<<2]; struct node2 { LL l , r ; LL sum ; } cl[maxn<<2]; LL lazy[maxn<<2]; LL s[maxn<<2] ; bool cmp(node1 a,node1 b) { return a.x < b.x ; } void push_up(int rt) { if( lazy[rt] ) cl[rt].sum = cl[rt].r - cl[rt].l ; else cl[rt].sum = cl[rt<<1].sum + cl[rt<<1|1].sum ; } void creat(int rt,int l,int r) { cl[rt].l = s[l] ; cl[rt].r = s[r] ; if( r - l > 1 ) { creat(rt<<1,l,(l+r)/2); creat(rt<<1|1,(l+r)/2,r); } else cl[rt].sum = 0 ; return ; } void update(int rt,LL l,LL r,int flag) { if( l == cl[rt].l && r == cl[rt].r ) { lazy[rt] += flag ; push_up(rt); return ; } else { if( cl[rt<<1].r > l ) { LL k = cl[rt<<1].r < r ? cl[rt<<1].r : r ; update(rt<<1,l,k,flag); } if( cl[rt<<1|1].l < r ) { LL k = cl[rt<<1|1].l > l ? cl[rt<<1|1].l : l ; update(rt<<1|1,k,r,flag); } push_up(rt); } } int main() { LL i , n , x1 , x2 , h , low , ans ; while(scanf("%I64d", &n)!=EOF) { for(i = 0 ; i < n ; i++) { scanf("%I64d %I64d %I64d", &x1, &x2, &h); p[i].x = x1 ; p[i].y1 = 0 ; p[i].y2 = h ; p[i].flag = 1 ; p[i+n].x = x2 ; p[i+n].y1 = 0 ; p[i+n].y2 = h ; p[i+n].flag = -1 ; s[i+1] = h ; } s[n+1] = 0 ; sort(s+1,s+(n+2)); sort(p,p+2*n,cmp); creat(1,1,n+1); update(1,p[0].y1,p[0].y2,p[0].flag); ans = 0 ; for(i = 1 ; i < 2*n ; i++) { ans += ( p[i].x - p[i-1].x )*cl[1].sum; update(1,p[i].y1,p[i].y2,p[i].flag); } printf("%I64d\n", ans); } return 0; } 
     
    
   
  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj3687Labeling Balls(反向拓扑.. 下一篇线段树模板(NOTONLYSUCCESS神牛)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: