POJ - 1151 Atlantis【扫描线】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/81985923

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.


题目分析

扫描线的入门裸题
详细讲解看这里

扫描线—学习笔记

WA了无数次,结果把%.2lf改成%.2f就过了???
以及注意每组数据要空一行


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef double dd;

int read()
{
    int x=0,f=1;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}

const int maxn=2010;
int n,cs;
int tot1,tot2,cnt;
struct node{dd x,y1,y2;int k;}rem[maxn];
bool cmp(node a,node b){return a.x<b.x;}
dd a[maxn],pos[maxn];
dd len[maxn<<2],ans;
int cov[maxn<<2];

void init()
{
    cnt=tot1=tot2=0; ans=0;
    memset(len,0,sizeof(len));
    memset(cov,0,sizeof(cov));
}

void pushup(int p,int s,int t)
{
    if(cov[p]>0) len[p]=pos[t+1]-pos[s];
    else if(s==t) len[p]=0;
    else len[p]=len[p<<1]+len[p<<1|1];
}

void update(int ll,int rr,int s,int t,int p,int v)
{
    if(ll<=s&&t<=rr){ cov[p]+=v; pushup(p,s,t); return;}
    int mid=s+t>>1;
    if(ll<=mid) update(ll,rr,s,mid,p<<1,v);
    if(rr>mid) update(ll,rr,mid+1,t,p<<1|1,v);
    pushup(p,s,t);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break; init();
        for(int i=1;i<=n;i++)
        {
            dd x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[++tot1]=y1; a[++tot1]=y2;
            rem[++tot2].x=x1; rem[tot2].y1=y1;  rem[tot2].y2=y2; rem[tot2].k=1;
            rem[++tot2].x=x2; rem[tot2].y1=y1;  rem[tot2].y2=y2; rem[tot2].k=-1;
        }

        sort(a+1,a+1+tot1);
        for(int i=1;i<=tot1;++i)
        if(i==1||a[i]!=a[i-1])
        pos[++cnt]=a[i];

        sort(rem+1,rem+1+tot2,cmp); 
        for(int i=1;i<tot2;i++)
        {
            int ll=lower_bound(pos+1,pos+1+cnt,rem[i].y1)-pos;
            int rr=lower_bound(pos+1,pos+1+cnt,rem[i].y2)-pos;
            update(ll,rr-1,1,cnt,1,rem[i].k);
            ans+=len[1]*(rem[i+1].x-rem[i].x);
        }
        printf("Test case #%d\n",++cs);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/81985923