码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3277 City Horizon

时间:2016-08-30 00:20:07      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

City Horizon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18555   Accepted: 5114

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: AiBi, 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.
 

题目大意:在水平直线上给定一组矩形,这些矩形的底边在同一水平线上,求这些矩形占据的面积。

题目分析:很显然,这些矩形的长和高都不一样,还会有重合的区域。如何求这些矩形的面积呢?画个图就知道了。

 

本题需要注意:

离散化的手法;

延迟更新的手法:在最后求面积时延迟

long long (强制转换);

技术分享
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 #define lson l,m,rt<<1
 8 #define rson m,r,rt<<1|1
 9 
10 typedef long long LL;
11 
12 const int maxn=40005;
13 const int maxl=maxn*2;
14 int a[maxn],b[maxn],c[maxn];
15 int jh[maxl];
16 int h[maxl<<2],tag[maxl<<2];
17 int N,M;
18 
19 void UpDate(int L,int R,int d,int l,int r,int rt)
20 {
21     if(L<=l&&r<=R)
22     {
23         if(h[rt]<d) h[rt]=d;
24         return;
25     }
26     if(l+1==r) return;
27     int m=(l+r)>>1;
28     if(L<=m) UpDate(L,R,d,lson);
29     if(R>=m) UpDate(L,R,d,rson);
30 }
31     
32 LL Query(int l,int r,int rt,int height)//lazy lazy lazy
33 {
34     if(height>h[rt]) h[rt]=height;
35     if(l+1==r) return (LL)(jh[r]-jh[l])*h[rt];
36     int m=(l+r)>>1;
37     LL a=Query(lson,h[rt]);
38     LL b=Query(rson,h[rt]);
39     return a+b;
40 }
41 
42 int bfind(int x)
43 {
44     int l,r;
45     l=0,r=M+1;
46     while(l<r)
47     {
48         int m=(l+r)>>1;
49         if(jh[m]==x) return m;
50         if(jh[m]<x) l=m;
51         else r=m;
52     }
53 }
54 
55 int main()
56 {
57     scanf("%d",&N);
58     for(int i=1;i<=N;i++)
59     {
60         scanf("%d %d %d",&a[i],&b[i],&c[i]);
61         jh[2*i-1]=a[i],jh[2*i]=b[i];
62     }
63     
64     sort(jh+1,jh+2*N+1);
65     for(int i=1;i<=2*N;i++)
66         if(jh[i+1]!=jh[i]) jh[++M]=jh[i];
67     for(int i=1;i<=N;i++)
68     {
69         if(a[i]>b[i]) swap(a[i],b[i]);
70         UpDate(bfind(a[i]),bfind(b[i]),c[i],1,M,1);
71     }
72     int m=(1+M)>>1;
73     cout<<Query(1,m,2,h[1])+Query(m,M,3,h[1]);
74     return 0;
75 }
View Code

技术分享

POJ 3277 City Horizon

标签:

原文地址:http://www.cnblogs.com/cnblogsLSY/p/5820146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!