【POJ 3277】 City Horizon(离散化+树状数组+二分)
来源:互联网 发布:2016网络作家排名 编辑:程序博客网 时间:2024/04/30 11:31
【POJ 3277】 City Horizon(离散化+树状数组+二分)
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 17846 Accepted: 4894
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. Buildingi's silhouette has a base that spans locations Ai throughBi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has heightHi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by allN 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 allN buildings
Sample Input
42 5 19 10 46 8 24 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;}
1 0
- 【POJ 3277】 City Horizon(离散化+树状数组+二分)
- POJ 3277 City Horizon (离散化线段树)
- (离散化 + 线段树) poj 3277 City Horizon
- poj 3277 City Horizon(线段树#2----离散化)
- POJ 3277 City Horizon 扫描线+离散化+线段树
- poj-3277-City Horizon-离散化+线段树区域更新
- poj 3277 City Horizon 线段树加上离散化
- #POJ 3277 City Horizon 【离散化、区间更新】
- POJ 3277 City Horizon(线段树+扫描线+离散化)
- POJ 题目3277 City Horizon(线段树+离散化+扫描线)
- poj 3277 City Horizon
- poj 3277 City Horizon
- POJ 3277 City Horizon
- poj 3277 City Horizon
- poj 3277 City Horizon
- POJ 3277 City Horizon
- POJ 3277 City Horizon
- POJ 3277 City Horizon
- 归并排序算法
- ROS摄像机的标定
- python 下载文件汇总
- ACM解题总结——HihoCoder1138
- Human-level control through deep reinforcement learning(中文翻译)
- 【POJ 3277】 City Horizon(离散化+树状数组+二分)
- ZOJ 3862 Intersection【贪心】【几何】【模拟】
- 第七周项目3多文件组织程序
- delete析构的调用
- 4.11搭建网站的两个小问题
- iOS中RunLoop机制的探索
- CSU-1715 Positive Con Sequences(模拟分四种情况 )From湖南省多校对抗赛E题(2016.03.27)
- HDU 4109 Instrction Arrangement
- overfeat 测试