poj 1151 Atlantis (线段树+扫描线+离散化)

本文介绍了一种计算多个矩形覆盖总面积的方法。通过将矩形分解为平行于坐标轴的线段,并使用线段树数据结构来跟踪这些线段的覆盖情况,最终计算出所有矩形覆盖区域的并集面积。

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18061 Accepted: 6873

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.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

求矩形覆盖下的面积。可以把矩形平行y轴的线段都删去,这样就剩下很多平行x轴的线段。把线段y值按从小到大排序,每次插入一条线段得到当前覆盖在x轴的长度,乘以该条线段和下一条线段的z纵坐标之差,就是面积。求和即可。见下图


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define N 410
#define ll __int64
double x[N];
struct line  //记录和x轴平行的线段信息
{
    int f;   //f=1为底边,长度增加;-1为顶边,长度消去
    double x1,x2,y;   // 线段从左到右的端点,在y轴上的位置
}a[N];
struct node  //线段树结构体
{
    int f;   //f>=0, f为正值,代表被完全覆盖
    double l,r,len;   //记录区间的左右端点,和实际覆盖长度
}f[N*3];
bool cmp(line a,line b)
{
    return a.y<b.y;
}
void creat(int t,int l,int r)
{
    f[t].l=x[l];
    f[t].r=x[r];
    f[t].len=f[t].f=0;
    //printf("t=%d l=%.2f r=%.2f\n",t,x[l],x[r]);
    if(l==r-1)
        return ;
    int tmp=t<<1,mid=(l+r)>>1;
    creat(tmp,l,mid);
    creat(tmp|1,mid,r);   //线段是连续的,长度为f[t].r-f[t].l
}
void pushup(int t)
{
    if(f[t].f) //t代表的线段被完全覆盖
        f[t].len=f[t].r-f[t].l;
    else
        f[t].len=f[t<<1].len+f[t<<1|1].len;
    //printf("t=%d len=%.2f\n",t,f[t].len);
}
void update(int t,line a)
{
    if(f[t].l==a.x1&&f[t].r==a.x2)
    {
        f[t].f+=a.f;
        pushup(t);
        return ;
    }
    int tmp=t<<1;
    if(a.x2<=f[tmp].r)   //线段落在左边子区间
        update(tmp,a);
    else if(a.x1>=f[tmp|1].l)  ////线段落在右边子区间
        update(tmp|1,a);
    else            // 线段落在左右子区间
    {
        line b=a;  //拆分线段为两段分属两个区间
        b.x2=f[tmp].r;
        update(tmp,b);
        b=a;
        b.x1=f[tmp].r;
        update(tmp|1,b);
    }
    pushup(t);  //向父节点求和
}
/*
2
10 10 20 20
15 15 25 25.5
0
*/
int main()
{
    int i,n,cnt=1,m;
    double x1,x2,y1,y2;
    while(scanf("%d",&n),n)
    {
        for(i=m=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[m].x1=x1;
            a[m].x2=x2;
            a[m].y=y1;
            a[m].f=1;
            x[m++]=x1;
            a[m].x1=x1;
            a[m].x2=x2;
            a[m].y=y2;
            a[m].f=-1;
            x[m++]=x2;
        }
        sort(x+1,x+m);  //排序
        sort(a+1,a+m,cmp);
        creat(1,1,m-1);
        double ans=0;
        for(i=1;i<m-1;i++)
        {
            update(1,a[i]);   //求出当前矩形的平行X轴的长度
            ans+=f[1].len*(a[i+1].y-a[i].y);
        }
        printf("Test case #%d\n",cnt++);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值