poj1151 Atlantis

本文介绍了一个计算多个矩形地图覆盖总面积的算法实现。通过使用线段树来维护Y轴上的长度,并沿X轴进行扫描,有效地解决了计算不同地图区域覆盖总面积的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

用线段树维护y轴上的长度,然后沿x轴扫描。
具体的维护方法是用cov记录当前区间被完整的【拼起来的不算】线段覆盖的次数,sum记录被覆盖的长度。每次扫到线段就增减cov,如果cov大于0长度就是区间长度,否则就是左右两段的和。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct seg
{
    int y1,y2,k;
    double x,yy1,yy2;
    bool operator < (const seg & ss) const
    {
        return x<ss.x||(x==ss.x&&k<ss.k);
    }
}a[210];
int n,cov[1010],tot,m;
double ord[210],sum[1010];
void mark(int p,int L,int R,int l,int r,int k)
{
    if (L==l&&R==r)
    {
        cov[p]+=k;
        if (cov[p]) sum[p]=ord[R]-ord[L-1];
        else sum[p]=sum[p*2]+sum[p*2+1];
        return;
    }
    int M=(L+R)/2;
    if (r<=M) mark(p*2,L,M,l,r,k);
    else
    {
        if (l>M) mark(p*2+1,M+1,R,l,r,k);
        else
        {
            mark(p*2,L,M,l,M,k);
            mark(p*2+1,M+1,R,M+1,r,k);
        }
    }
    if (cov[p]) sum[p]=ord[R]-ord[L-1];
    else sum[p]=sum[p*2]+sum[p*2+1];
}
int main()
{
    int i,j,k,p,q,x,y,z,K=0;
    double x1,y1,x2,y2,ans;
    while (scanf("%d",&n)&&n)
    {
        tot=m=0;
        memset(cov,0,sizeof(cov));
        memset(sum,0,sizeof(sum));
        for (i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            ord[++tot]=y1;
            ord[++tot]=y2;
            a[++m]=(seg){0,0,1,x1,y1,y2};
            a[++m]=(seg){0,0,-1,x2,y1,y2};
        }
        sort(ord+1,ord+tot+1);
        tot=unique(ord+1,ord+tot+1)-ord-1;
        for (i=1;i<=m;i++)
        {
            a[i].y1=lower_bound(ord+1,ord+tot+1,a[i].yy1)-ord+1;
            a[i].y2=lower_bound(ord+1,ord+tot+1,a[i].yy2)-ord;
        }
        sort(a+1,a+m+1);
        ans=0;
        for (i=1;i<=m;i++)
        {
            mark(1,2,tot,a[i].y1,a[i].y2,a[i].k);
            if (i<m) ans+=sum[1]*(a[i+1].x-a[i].x);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++K,ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值