HDOJ 1542 (POJ 1151) Atlantis 【线段树 离散化 扫描线 面积并】
2015-12-10 19:49
597 查看
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9662 Accepted Submission(s): 4129
[align=left]Problem Description[/align]
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your
friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
[align=left]Input[/align]
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2
(0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
[align=left]Output[/align]
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a
is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
[align=left]Sample Input[/align]
2 10 10 20 20 15 15 25 25.5 0
[align=left]Sample Output[/align]
Test case #1 Total explored area: 180.00
恩,题目大意就是说,求n个矩形的面积并,对于每个矩形,给出左下角坐标和右上角坐标,套的模板(学习的康神)poj上提交时最后输出是 “%.2f"不是 ”%.2lf"
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 2200 using namespace std; double rec[maxn],sum[maxn]; int tree[maxn]; struct node { double x1,x2,y; int s; }a[maxn]; bool cmp(node a,node b) { return a.y<b.y; } int bsearch(int l,int r,double aim) { while(l<=r) { int mid=(l+r)>>1; if(rec[mid]==aim) return mid; else if(rec[mid]>aim) r=mid-1; else l=mid+1; } } void pushup(int o,int l,int r) { if(tree[o]) sum[o]=rec[r+1]-rec[l]; else if(l==r) sum[o]=0; else sum[o]=sum[o<<1]+sum[o<<1|1]; } void update(int l,int r,int L,int R,int o,int sign) { if(l<=L&&r>=R) { tree[o]+=sign; pushup(o,L,R); return ; } int mid=(L+R)>>1; if(r<=mid) update(l,r,L,mid,o<<1,sign); else if(l>mid) update(l,r,mid+1,R,o<<1|1,sign); else { update(l,mid,L,mid,o<<1,sign); update(mid+1,r,mid+1,R,o<<1|1,sign); } pushup(o,L,R); } int main() { int n,cnt=0; double x1,y1,x2,y2; while(scanf("%d",&n)&&n) { memset(rec,0,sizeof(rec)); memset(tree,0,sizeof(tree)); memset(sum,0,sizeof(sum)); int k=1; for(int i=0;i<n;++i) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); a[k].x1=x1; a[k].x2=x2; a[k].y=y1; a[k].s=1; rec[k++]=x1; a[k].x1=x1; a[k].x2=x2; a[k].y=y2; a[k].s=-1; rec[k++]=x2; } sort(rec+1,rec+k); sort(a+1,a+k,cmp); int j=2; for(int i=2;i<k;++i) { if(rec[i]!=rec[i-1]) rec[j++]=rec[i]; } double ans=0; for(int i=1;i<k-1;++i) { int l=bsearch(1,j,a[i].x1); int r=bsearch(1,j,a[i].x2)-1; if(l<=r) update(l,r,1,k-1,1,a[i].s); ans+=sum[1]*(a[i+1].y-a[i].y); } printf("Test case #%d\n",++cnt); printf("Total explored area: %.2lf\n\n",ans); } return 0; }
相关文章推荐
- Java 类的命名规范(杂)
- [读书笔记]增长黑客:创业公司的用户与收入增长秘籍
- python基础教程共60课-第30课字符串的索引和切片
- 杭电题目分类
- week14---12月11日 JS内置对象——document文档对象、Dom对象
- 网络虚拟化之FlowVisor:网络虚拟层(下)
- ExtJS4为form表单必填项添加红色*标识
- Android学习笔记:用ExpandableListView实现qq好友列表
- Java中类的构造顺序
- CSS---定位
- 获取天气预报
- 《走开》反馈
- Git之忽略文件(ignore file)
- UVa-122 树的遍历
- ios软件开发在中国的前景
- Charm Bracelet-POJ3624(01背包)
- Android中Service生命周期
- 修复win7和wubi装的ubuntu双系统引导
- PCA降维算法总结以及matlab实现PCA(个人的一点理解)
- Swift中的断言assert