【POJ 3277】 City Horizon(离散化+树状数组+二分)
2016-04-11 21:13
405 查看
[align=center]【POJ 3277】 City Horizon(离散化+树状数组+二分)
[/align]
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
Sample Output
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.
题目大意:n个影子,分布在二维坐标中。
每个影子的表示方式为x1,x2,y,覆盖了矩形横坐标x1->x2 纵坐标为y的区间
这些影子会有重叠
问重叠后所有影子的面积。覆盖部分只统计一次
y离散化,这个没的说
影子拆成左右两边,左进右出,这个也没啥说的
按x排序,从左到右遍历。
每当遇到入边,在离散化后的数组中找到当前y的下标(二分) 然后在树状数组对应位置累加1
每当遇到出边,~,减1
当出现横向移动,也就是到了一个新x的时候,就要统计出这段影子的面积
也就是找到当前的最高的y(因为影子默认从y=0向上伸展到y 所以只统计当前最高的y即可 较低的都被覆盖掉了
这个y在树状数组内二分即可,先找到树状数组末尾的值,也就是整个区间的加和
然后找到最右端的非0位置(通过二分位置,然后求加和,跟刚才求的加和比较来得到
代码如下:
[/align]
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17846 | Accepted: 4894 |
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.
题目大意:n个影子,分布在二维坐标中。
每个影子的表示方式为x1,x2,y,覆盖了矩形横坐标x1->x2 纵坐标为y的区间
这些影子会有重叠
问重叠后所有影子的面积。覆盖部分只统计一次
y离散化,这个没的说
影子拆成左右两边,左进右出,这个也没啥说的
按x排序,从左到右遍历。
每当遇到入边,在离散化后的数组中找到当前y的下标(二分) 然后在树状数组对应位置累加1
每当遇到出边,~,减1
当出现横向移动,也就是到了一个新x的时候,就要统计出这段影子的面积
也就是找到当前的最高的y(因为影子默认从y=0向上伸展到y 所以只统计当前最高的y即可 较低的都被覆盖掉了
这个y在树状数组内二分即可,先找到树状数组末尾的值,也就是整个区间的加和
然后找到最右端的非0位置(通过二分位置,然后求加和,跟刚才求的加和比较来得到
代码如下:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread() freopen("in.in","r",stdin) #define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; struct Node { int x,y; bool ad; bool operator < (const struct Node a)const { return x < a.x; } }; Node nd[88888]; int ty[44444]; int bit[44444]; int tp; int Lowbit(int x) { return x&(-x); } void add(int x,int d) { while(x < tp) { bit[x] += d; x += Lowbit(x); } } int sum(int x) { int ans = 0; while(x) { ans += bit[x]; x -= Lowbit(x); } return ans; } int get() { int ans = sum(tp-1); if(ans == 0) return 0; int c; int l = 1, r = tp-1; while(l <= r) { int mid = (l+r)>>1; int k = sum(mid); if(k >= ans) { r = mid-1; if(k == ans) c = mid; }else l = mid+1; } return ty[c]; } int main() { //fread(); //fwrite(); int n,x1,x2,y; LL ans; while(~scanf("%d",&n)) { int tmp = 0; for(int i = 0; i < n; ++i) { scanf("%d%d%d",&x1,&x2,&y); nd[tmp].x = x1; nd[tmp].y = y; nd[tmp].ad = 1; ty[i+1] = y; tmp++; nd[tmp].x = x2; nd[tmp].y = y; nd[tmp].ad = 0; tmp++; } memset(bit,0,sizeof(bit)); sort(ty+1,ty+n+1); sort(nd,nd+tmp); tp = unique(ty+1,ty+n+1)-ty; ans = 0; set <int> s; int x; for(int i = 0; i < tmp; ++i) { //printf("x:%d y:%d ad:%s\n",nd[i].x,nd[i].y,nd[i].ad? "y": "n"); if(i && nd[i-1].x != nd[i].x) ans += 1LL*(nd[i].x-nd[i-1].x)*get(); int l = 1, r = tp-1; while(l <= r) { int mid = (l+r)>>1; if(ty[mid] <= nd[i].y) { l = mid+1; if(ty[mid] == nd[i].y) { x = mid; break; } }else r = mid-1; } if(nd[i].ad) { add(x,1); }else add(x,-1); } printf("%lld\n",ans); } return 0; }
相关文章推荐
- tomcat——简单的日志实现
- spring applicationContext.xml 配置文件 详解
- NYOJ564 最优对称路径(记忆化搜索+spfa)
- 2016-04-11/获取系统当前时间
- 用vector进行图的邻接表存储
- linux基础学习之 nfs挂载
- Hibernate管理Session
- 重学STM32---(九) ——CAN通信(一)
- 谷歌对面试者提出的15个让人疯狂的问题
- js练习题三:使div变色 密码提示框
- 联合巨头做庄开源,英特尔在云计算时代再造DIY模式
- 设计模式基础——UML类图
- Apache CXF实现的RESTful形式的webservices
- 从设计心理学理解交互设计的原则
- 值班管理数据表设计
- AOP execution
- jQuery Validate(表单验证)例子
- 二级缓存
- Hadoop优化与调整
- 汇编语言(王爽) 第11章 标志寄存器