HDU 1542 Atlantis
Posted 一蓑烟雨任生平
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1542 Atlantis相关的知识,希望对你有一定的参考价值。
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13934 Accepted Submission(s): 5768
Problem 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 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.
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
Source
Recommend
思路:线段树+扫描线。
#include<iostream> #include<cstdio> #include<algorithm> #define N 300 using namespace std; double y[N]; struct Node{ double x;double y1;double y2; int flag; }node[N]; struct node{ int l;int r;double ml;double mr;int s;double len; }a[N*3]; bool cmp(Node a,Node b){ return a.x-b.x<0.0000001; } void build(int i,int left,int right){ a[i].l=left; a[i].r=right; a[i].ml=y[left]; a[i].mr=y[right]; a[i].s=0; a[i].len=0; if(a[i].l+1==a[i].r){ return ; } int mid=(left+right)>>1; build(i*2,left,mid); build(i*2+1,mid,right);//建树时注意这里不是mid+1,因为做相减的时候如果mid+1这么建回到值左孩子的右边与有孩子的左边无法进行运算 } void callen(int i){ if(a[i].s>0){//注意这里不是所有边都是左孩子的长度加上右孩子的长度,他存在一个覆盖问题 a[i].len=a[i].mr-a[i].ml; }else if(a[i].r-a[i].l==1){ a[i].len=0; }else{ a[i].len=a[i*2].len+a[i*2+1].len; } return ; } void updata(int i,Node b){ if(a[i].ml==b.y1&&a[i].mr==b.y2){ a[i].s+=b.flag; callen(i); return ; } if(b.y2<=a[i*2].mr) updata(i*2,b); else if(b.y1>=a[i*2+1].ml) updata(i*2+1,b); else{ Node temp=b; temp.y2=a[i*2].mr; updata(i*2,temp); temp=b; temp.y1=a[i*2+1].ml; updata(i*2+1,temp); } callen(i); return ; } int main(){ int n,t,p=1,te; double x1,x2,y1,y2; while(scanf("%d",&n),n){ t=1; for(int i=0;i<n;i++){ scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); node[t].x=x1; node[t].y1=y1; node[t].y2=y2; node[t].flag=1;//入边 y[t++]=y1; node[t].x=x2; node[t].y1=y1; node[t].y2=y2; node[t].flag=-1;//出边 y[t++]=y2; } sort(node+1,node+t,cmp); sort(y+1,y+t); build(1,1,t-1); updata(1,node[1]); double sum=0; for(int i=2;i<t;i++){ sum+=a[1].len*(node[i].x-node[i-1].x); updata(1,node[i]); } printf("Test case #%d\n",p++); printf("Total explored area: %.2lf\n\n",sum); } return 0; }
以上是关于HDU 1542 Atlantis的主要内容,如果未能解决你的问题,请参考以下文章