poj 1151 Atlantis

Atlantis

http://poj.org/problem?id=1151

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22662   Accepted: 8478

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 

Source

Mid-Central European Regional Contest 2000

题意:求几个矩形覆盖的总面积

线段树+扫描线

离散化x坐标,扫描线自下而上扫

标记矩形下边界为1,上边界为-1,线段树维护当前这根线的长度

线段树的每个节点代表的是这个节点右边一小段的长度

所以修改区间[l,r]是修改区间[l,r-1]

每次的修改up上去,计算使用1号点的sum,

这样可以避免标记的下传

解释一下up函数:

如果这个区间有标记,即区间完全被矩形边界覆盖,无论标记有多少,他的长度只能是区间长度

如果这个区间没有标记,即区间部分被矩形边界覆盖,就由子节点合并

叶子节点没有子节点,所以特判

#include<cstdio>
#include<algorithm>
using namespace std;
int n,cnt;
int opl,opr,w;
double x1[101],x2[101],y1[101],y2[101],hash[201],ans;
struct LINE
{
    int xl,xr,f;
    double h;
}line[201];
struct node
{
    int l,r,f;
    double sum;
}tr[201*4];
bool cmp(LINE p,LINE q)
{
    return p.h<q.h;
}
void build(int k,int l,int r)
{
    tr[k].l=l; tr[k].r=r;
    if(l==r) return;
    int mid=l+r>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
}
void up(int k)
{
    if(tr[k].f) tr[k].sum=hash[tr[k].r+1]-hash[tr[k].l];
    else if(tr[k].l==tr[k].r) tr[k].sum=0;
    else tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
void change(int k)
{
    if(tr[k].l>=opl&&tr[k].r<=opr)
    {
        tr[k].f+=w;
        up(k);
        return;
    }
    int mid=tr[k].l+tr[k].r>>1;
    if(opl<=mid) change(k<<1);
    if(opr>mid) change(k<<1|1);
    up(k);
}
int main()
{
    int T=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n) return 0;
        T++; cnt=0; ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]);
            hash[++cnt]=x1[i]; hash[++cnt]=x2[i];
        }
        sort(hash+1,hash+cnt+1);
        cnt=unique(hash+1,hash+cnt+1)-(hash+1);
        for(int i=1;i<=n;i++)
        {
            line[i*2-1].xl=lower_bound(hash+1,hash+cnt+1,x1[i])-hash;
            line[i*2].xl=line[i*2-1].xl;
            line[i*2-1].xr=lower_bound(hash+1,hash+cnt+1,x2[i])-hash;
            line[i*2].xr=line[i*2-1].xr;
            line[i*2-1].h=y1[i];
            line[i*2].h=y2[i];
            line[i*2-1].f=1;
            line[i*2].f=-1;
        }
        sort(line+1,line+2*n+1,cmp);
        build(1,1,cnt);
        for(int i=1;i<=2*n;i++)
        {
            opl=line[i].xl; opr=line[i].xr-1;
            w=line[i].f;
            change(1);
            ans+=tr[1].sum*(line[i+1].h-line[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",T,ans);
    }
}
时间: 2024-10-11 08:00:17

poj 1151 Atlantis的相关文章

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

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

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

poj 1151 Atlantis 二分查找+离散化

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17464   Accepted: 6654 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

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 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25769   Accepted: 9477 Description 有几个古希腊文本包含传说中的亚特兰蒂斯岛的描述. 其中一些文本甚至包括岛屿部分地图. 但不幸的是,这些地图描述了亚特兰蒂斯的不同区域. 您的朋友Bill必须知道地图的总面积. 你(不

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,实际上就是一个方向直接算出,另一个方向使用线段树维护已经覆盖的底边的长度.具体操作不算复杂:假想一条扫描下从下往上开始进行扫描,初始时候

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

转载请注明原文:http://www.cnblogs.com/burning-flame/p/5934653.html 题目链接:http://poj.org/problem?id=1151 题意: 给你 n 个矩形的对角线坐标,求 n 个矩形并集的面积. 做法: 扫描线 + 线段树. 因为作线段树的题遇到了扫描线,只是粗浅的了解了一下. 就像字面上的:线性扫描一遍,对于每个单元,因为某些事件的发生会有一些变化. 举个例子: 现有长度为 n 的数组a,同时有 n 个区间覆盖[li, ri],对于

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

一样的题:HDU 1542 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18148   Accepted: 6902 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include map