Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17380 | Accepted: 6623 |
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.
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.
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
<span style="font-size:18px;">#include <iostream> #include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; struct node { int l,r,c;//l,r记录左右节点,c记录覆盖情况 double cnt,rf,lf;//cnt记录当前一段的长度,lf,rf表示这一段实际的边 }tree[1000]; struct Line { double x,y1,y2;//x表示该段的横坐标,y1,y2分别表示该段投影的边界 int f;// 标记边,f=1表示线段的开始,f=-1表示线段的结束 }line[220]; bool cmp(Line a,Line b) { return a.x<b.x; } double y[220]; void build(int root,int l,int r) { tree[root].l=l; tree[root].r=r; tree[root].cnt=tree[root].c=0; tree[root].lf=y[l]; tree[root].rf=y[r];//相当于离散化,将实际的坐标离散为l,r if(l+1==r) return; int mid=(l+r)/2; build(root*2,l,mid); build(root*2+1,mid,r); } void calen(int root) { if(tree[root].c>0)//如果这段被覆盖,就更新其长度为实际长度 { tree[root].cnt=tree[root].rf-tree[root].lf; return ; } if(tree[root].l+1==tree[root].r)//被撤销后长度为0 tree[root].cnt=0; else tree[root].cnt=tree[root*2].cnt+tree[root*2+1].cnt; } void renew(int root,Line e)//加入或减去一条扫描线后的更新 { if(e.y1==tree[root].lf&&e.y2==tree[root].rf) { tree[root].c+=e.f;//改变覆盖情况 calen(root); return; } if(e.y2<=tree[root*2].rf)//左区间 renew(root*2,e); else if(e.y1>=tree[root*2+1].lf)//右区间 renew(root*2+1,e); else//跨区间 { Line tmp=e; tmp.y2=tree[root*2].rf; renew(root*2,tmp); tmp=e; tmp.y1=tree[root*2+1].lf; renew(root*2+1,tmp); } calen(root); } int main() { int n,i,cnt; double x1,y1,x2,y2,ans; int num=1; while(scanf("%d",&n)!=EOF) { cnt=1;ans=0; if(n==0) break; while(n--)//将每个点的坐标赋予结构体 { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[cnt].x=x1; line[cnt].y1=y1; line[cnt].y2=y2; line[cnt].f=1; y[cnt]=y1; cnt++; line[cnt].x=x2; line[cnt].y1=y1; line[cnt].y2=y2; line[cnt].f=-1; y[cnt]=y2; cnt++; } sort(line+1,line+cnt,cmp);//将区间按X排序 sort(y+1,y+cnt);//将Y排序 build(1,1,cnt-1); renew(1,line[1]); for(i=2;i<cnt;i++) { ans+=tree[1].cnt*(line[i].x-line[i-1].x);//将每个矩形的面积加起来 renew(1,line[i]); } printf("Test case #%d\n",num++); printf("Total explored area: %.2f\n\n",ans); } return 0; } </span>