Atlantis (POJ-1151 (HDU-1542) )

Atlantis (POJ-1151 (HDU-1542) )

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

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

题目翻译:

有几个古希腊文本包含了对传说中的亚特兰蒂斯岛的描述。其中一些文本甚至包括该岛部分地区的地图。但不幸的是,这些地图描述了亚特兰蒂斯的不同区域。你的朋友比尔必须知道地图存在的总面积。你(不明智地)自愿编写一个程序来计算这个数量。

输入

输入文件由几个测试用例组成。每个测试用例都以一行开始,其中包含一个可用映射的整数n (1<=n<=100)。下面的n行描述了每个映射。每一行包含四个数字x1、y1、x2、y2 (0<=x1的值(x1;y1)和(x2;y2)是左上角resp的坐标。地图区域的右下角。

输入文件被包含一个0的行终止。不处理它。

输出

对于每个测试用例,程序应该输出一个部分。每个部分的第一行必须测试案例# k, k是测试用例的数量(从1开始)。第二个必须“探索总面积:”,其中一个是总开发面积(即联盟的矩形的面积在本测试用例),印刷精确到小数点右边的两个数字。

在每个测试用例之后输出一个空行。

思路:这是一道基础的扫描线法求矩形面积并的题,具体关于扫描线请看这篇博客:扫描线

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define PI 3.1415926
#define inf 0x3f3f3f3f
const int maxn=100010;
using namespace std;
struct GS
{
    int l,r;//线段树的左右整点
    int c;//c用来记录重叠情况
    double cnt,lf,rf;//cnt用来计算实在的长度,rf,lf分别是对应的左右真实的浮点数端点
}ST[maxn<<2];
struct SC
{
    double x,y1,y2;
    int f;
}Sc[maxn<<2];
//把一段段平行于y轴的线段表示成数组 ,
//x是线段的x坐标,y1,y2线段对应的下端点和上端点的坐标
//一个矩形 ,左边的那条边f为1,右边的为-1,
//用来记录重叠情况,可以根据这个来计算,GS节点中的c
bool cmp(SC a,SC b)
{
    return a.x < b.x;
}
double y[maxn<<2];//记录y坐标的数组
void Build(int t,int l,int r)
{
    ST[t].l=l;
    ST[t].r=r;
    ST[t].cnt=ST[t].c=0;
    ST[t].lf=y[l];
    ST[t].rf=y[r];
    if(l+1==r)
        return;
    int m=(l+r)>>1;
    Build(t<<1,l,m);
    Build(t<<1|1,m,r);
}
void PushUp(int t)//计算长度
{
    if(ST[t].c>0)
    {
        ST[t].cnt=ST[t].rf-ST[t].lf;
        return;
    }
    if(ST[t].l+1==ST[t].r)
        ST[t].cnt=0;
    else
    ST[t].cnt=ST[t<<1].cnt+ST[t<<1|1].cnt;
}
void Update(int t,SC k)//加入线段k,后更新线段树
{
    if(k.y1==ST[t].lf && k.y2==ST[t].rf)
    {
        ST[t].c+=k.f;
        PushUp(t);
        return;
    }
    if(k.y2<=ST[t<<1].rf)
        Update(t<<1,k);
    else if(k.y1>=ST[t<<1|1].lf)
    Update(t<<1|1,k);
    else
    {
        SC sum=k;
        sum.y2=ST[t<<1].rf;
        Update(t<<1,sum);
        sum=k;
        sum.y1=ST[t<<1|1].lf;
        Update(t<<1|1,sum);
    }
    PushUp(t);
}
int main()
{
    int i,n,t,ans=0;
    double x1,y1,x2,y2;
    while(scanf("%d",&n),n)
    {
        ans++;
        t=1;
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            Sc[t].x=x1;
            Sc[t].y1=y1;
            Sc[t].y2=y2;
            Sc[t].f=1;
            y[t]=y1;
            t++;
            Sc[t].x=x2;
            Sc[t].y1=y1;
            Sc[t].y2=y2;
            Sc[t].f=-1;
            y[t]=y2;
            t++;
        }
        sort(Sc+1,Sc+t,cmp);
        sort(y+1,y+t);
        Build(1,1,t-1);
        Update(1,Sc[1]);
        double num=0;
        for(i=2;i<t;i++)
        {
            num+=ST[1].cnt*(Sc[i].x-Sc[i-1].x);
            Update(1,Sc[i]);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",ans,num);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值