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为0、1代表为底边或顶边
    double x1,x2,y;   // 线段从左到右的端点,在y轴上的位置
}a[N];
struct node  //线段树结构体
{
    int 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;
    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)
        f[t].len=f[t].r-f[t].l;
    else
        f[t].len=f[t<<1].len+f[t<<1|1].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);  //向父节点求和
}

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);
        update(1,a[1]);
        double ans=0;
        for(i=2;i<m;i++)
        {
            ans+=f[1].len*(a[i].y-a[i-1].y);
            update(1,a[i]);
        }
        printf("Test case #%d\n",cnt++);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}
时间: 2024-10-29 19:05:45

poj 1151 Atlantis (线段树+扫描线+离散化)的相关文章

POJ 1151 Atlantis( 线段树 + 扫描线 )

一维离散化, 扫描线扫另一维, 用线段树维护 POJ建议交C++...G++貌似double要用%f ? 反正同一份代码C++AC,G++WA ------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 109; struct Line { double p,

poj 1151 Atlantis (线段树+扫描线)

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

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K 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 is

POJ 1151 Atlantis 线段树+离散化

题目链接:http://poj.org/problem?id=1151  http://acm.hdu.edu.cn/showproblem.php?pid=1542 题目大意:给你几个矩形的坐标,求他们的面积的并. 解题思路:可以参考http://www.cnblogs.com/kuangbin/archive/2011/08/16/2140544.html,实际上就是一个方向直接算出,另一个方向使用线段树维护已经覆盖的底边的长度.具体操作不算复杂:假想一条扫描下从下往上开始进行扫描,初始时候

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <

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

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9032    Accepted Submission(s): 3873 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

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

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 i