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

poj1151 Atlantis

时间:2015-11-29 09:28:51      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

                                                                      Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19585   Accepted: 7436

Description

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.

Input

The input 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.

Output

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.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source

                                                         [Submit]   [Go Back]   [Status]   [Discuss]
 
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<cstring>
  7 using namespace std;
  8 const int N=210;
  9 struct node{
 10     int left,right,c; //c : 区间被覆盖的层数, m: 区间的测度 
 11     double m;
 12 }tree[N*4];
 13 struct Line{
 14     double x,y1,y2;  //纵方向直线, x:直线横坐标, y1 y2:直线上的下面与上面的两个纵坐标
 15     int s;   //s = 1 : 直线为矩形的左边, s = 0:直线为矩形的右边
 16 }line[N];
 17 bool cmp(Line a,Line b){
 18     return a.x<b.x;
 19 }
 20 double ty[N],y[N];   //y[] 整数与浮点数的对应数组; ty[]:用来求y[]的辅助数组
 21 void build(int t,int left,int right){
 22     tree[t].left=left;
 23     tree[t].right=right;
 24     tree[t].c=0;
 25     tree[t].m=0;
 26     if(left+1<right){
 27         int mid=(left+right)>>1;
 28         build(t<<1,left,mid);
 29         build((t<<1)+1,mid,right);
 30     }
 31 }
 32 void update(int t){
 33     if(tree[t].c>0) tree[t].m=y[tree[t].right]-y[tree[t].left];
 34     //将线段树上区间的端点分别映射到 y[]数组所对应的浮点数上,由此计算出测度 
 35     else if(tree[t].left+1==tree[t].right) tree[t].m=0; 
 36     else tree[t].m=tree[t<<1].m+tree[(t<<1)+1].m;
 37 }
 38 void insert(int t,int left,int right){
 39     if(left<=tree[t].left && tree[t].right<=right){
 40         tree[t].c++;
 41         update(t);
 42         return ;
 43     }
 44     int mid=(tree[t].left+tree[t].right)>>1;
 45     if(left<mid) insert(t<<1,left,right);
 46     if(right>mid) insert((t<<1)+1,left,right);
 47     update(t);
 48 }
 49 void del(int t,int left,int right){
 50     if(left<=tree[t].left && tree[t].right<=right){
 51         tree[t].c--;
 52         update(t);
 53         return ;
 54     }
 55     int mid=(tree[t].left+tree[t].right)>>1;
 56     if(left<mid) del(t<<1,left,right);
 57     if(right>mid) del((t<<1)+1,left,right);
 58     update(t);
 59 }
 60 
 61 int getindex(int n,double x){//二分查找出浮点数 t 在数组y[]中的位置(此即所谓的映射关系)
 62     int left,right,mid;
 63     left=1;right=n;
 64     while(left<=right){
 65         mid=(left+right)>>1;
 66         if(y[mid]<x) left=mid+1;
 67         else right=mid-1;
 68     }
 69     return left;
 70 }
 71 int main(){
 72     int n,i;
 73     double x1,y1,x2,y2;
 74     int cas=1;
 75     while(scanf("%d",&n)!=EOF){
 76         if(n==0) break;
 77         for(i=0;i<n;i++){
 78             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 79             line[i*2].x=x1; line[i*2].y1=y1; line[i*2].y2=y2; line[i*2].s=1;
 80             line[i*2+1].x=x2; line[i*2+1].y1=y1; line[i*2+1].y2=y2; line[i*2+1].s=0;
 81             ty[i*2]=y1;ty[i*2+1]=y2;
 82         }
 83         n<<=1;
 84         sort(line,line+n,cmp);
 85         sort(ty,ty+n);  //默认升序排序
 86         int num=1;
 87         y[1]=ty[0];
 88         for(i=1;i<n;i++){ //离散化处理数组 ty[]使之不含重覆元素,得到新的数组存放到数组y[]中 
 89             if(ty[i]!=ty[i-1]) y[++num]=ty[i];
 90         }
 91         build(1,1,num);//树的叶子节点与数组 y[]中的元素个数相同,以便建立一一对应的关系
 92         int left,right;
 93         double ans=0;
 94         for(i=0;i<n-1;i++){
 95             left=getindex(num,line[i].y1);  //由对应关系计算出线段两端在树中的位置
 96             right=getindex(num,line[i].y2);
 97             if(line[i].s==1)   //插入矩形的左边
 98                 insert(1,left,right);
 99             else         //删除矩形的右边 
100                 del(1,left,right);
101             ans+=tree[1].m*(line[i+1].x-line[i].x);
102         }
103         printf("Test case #%d\n",cas++);
104         printf("Total explored area: %.2lf\n\n",ans);
105     }
106     return 0;
107 }

 

poj1151 Atlantis

标签:

原文地址:http://www.cnblogs.com/CXCXCXC/p/5003389.html

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