hdu 1542 Atlantis(线段树&扫描线&面积并)

来源:互联网 发布:html下拉框显示数据库 编辑:程序博客网 时间:2024/05/17 00:59

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6386    Accepted Submission(s): 2814


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.
 

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
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00
 

Source
Mid-Central European Regional Contest 2000
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1828 1255 1823 1543 3333 
 题意:
给你一些矩形和坐标轴平行的矩形。告诉你们他们左下角和右上角的坐标。现在问你。整个坐标面被他们覆盖的面积。
思路:
由于直接找的线段树扫描线的题目。所以也没想其它思路。讲一下为什么可以用扫描线来做吧。首先是面积的求解。面积=底边长*高。这底边长不一定要求连续。所以如果我们知道某一时刻底边长和高就可以算出面积了。而线段树就可以出色的完成求出某个时间底边长(x轴被覆盖的长度)。所以我们只需要把所有平行与x轴的边按高度排序。然后依次插入到线段树中。遇到矩形的下边就加入到线段树中。遇到上边就把对应的下边从线段树中删除。文字可能不是很好理解。画图看看就知道了。
详细见代码:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=250;#define lson L,mid,ls#define rson mid+1,R,rsint n,m;int cov[maxn<<2];double len[maxn<<2],H[maxn];struct node{    double x1,x2,h;    int v;    node(double a=0,double b=0,double c=0,int d=0):x1(a),x2(b),h(c),v(d){}} seg[maxn];bool cmp(node a,node b){    return a.h<b.h;}void init(){    sort(H,H+m);    m=unique(H,H+m)-H;}int Hash(double x){    return lower_bound(H,H+m,x)-H;}void PushUp(int L,int R,int rt){    if(cov[rt])//有标记肯定整块覆盖了        len[rt]=H[R+1]-H[L];    else if(L==R)//没有左右儿子了。        len[rt]=0;    else//没有整块覆盖但是被部分覆盖了        len[rt]=len[rt<<1]+len[rt<<1|1];}void update(int L,int R,int rt,int l,int r,int d){    if(l<=L&&R<=r)    {        cov[rt]+=d;        PushUp(L,R,rt);        return;    }//这种标记是不用下传的。因为没删除一个上边。一定有一个下边与之对应。    int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;    if(l<=mid)        update(lson,l,r,d);    if(r>mid)        update(rson,l,r,d);    PushUp(L,R,rt);}int main(){    int cas=1,i,ptr;    double x1,x2,y1,y2,ans;    while(scanf("%d",&n),n)    {        printf("Test case #%d\n",cas++);        for(i=ptr=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            H[ptr]=x1;            seg[ptr++]=node(x1,x2,y1,1);            H[ptr]=x2;            seg[ptr++]=node(x1,x2,y2,-1);        }        m=ptr,ans=0;        init();        sort(seg,seg+ptr,cmp);        memset(len,0,sizeof len);        memset(cov,0,sizeof cov);        for(i=0,ptr--;i<ptr;i++)        {            update(0,m-1,1,Hash(seg[i].x1),Hash(seg[i].x2)-1,seg[i].v);//m个结点m-1条线段            ans+=(seg[i+1].h-seg[i].h)*len[1];        }        printf("Total explored area: %.2lf\n\n",ans);    }    return 0;}


2 0
原创粉丝点击