码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1151 Atlantis 扫描线+线段树

时间:2014-08-10 21:36:10      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

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 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


求矩阵的并。
从第一条开始的竖边到最后一条竖边,逐条插入到线段树,并记录此时整个区间中被覆盖的实际长度,然后利用下一条竖边与当前竖边的x值差乘以现有的y轴覆盖实际长度就是此段覆盖的面积了。
//208K	16MS
#include<stdio.h>
#include<algorithm>
#define M 207
using namespace std;
double y[M];
struct Tree
{
    int l,r,mid,cover;
    double len;
}tree[M<<4];
struct Line
{
    double x,y1,y2;
    int dir;
}l[M<<4];
int cmp(Line a,Line b)
{
    return a.x<b.x;
}
void cal(int i)
{
    if(tree[i].cover>0)tree[i].len=y[tree[i].r]-y[tree[i].l];//如果被覆盖那么就是整段长度
    else if(tree[i].l+1==tree[i].r)tree[i].len=0;//未被覆盖且为叶子节点,则为0
    else tree[i].len=tree[i*2].len+tree[i*2+1].len;//非叶子节点即为下面子区间的覆盖总和
}
void build(int left,int right,int i)
{
    tree[i].l=left;tree[i].r=right;tree[i].mid=(left+right)>>1;
    tree[i].cover=0;tree[i].len=0;
    if(left+1!=right)
    {
        build(left,tree[i].mid,i*2);
        build(tree[i].mid,right,i*2+1);
    }
}
void insert(Line a,int i)
{
    Line t;
    if(y[tree[i].l]==a.y1&&y[tree[i].r]==a.y2)tree[i].cover+=a.dir;
    else
    {
        if(y[tree[i*2].r]>=a.y2)insert(a,i*2);
        else if(y[tree[i*2+1].l]<=a.y1)insert(a,i*2+1);
        else
        {
            t=a;
            t.y2=y[tree[i*2].r];
            insert(t,i*2);
            t=a;
            t.y1=y[tree[i*2+1].l];
            insert(t,i*2+1);
        }
    }
    cal(i);
}
int main()
{
    int n,cas=1;
    while(scanf("%d",&n),n)
    {
        double x1,y1,x2,y2,ans=0;
        int k=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            l[k].x=x1;l[k].y1=y1;l[k].y2=y2;l[k].dir=1;
            y[k++]=y1;
            l[k].x=x2;l[k].y1=y1;l[k].y2=y2;l[k].dir=-1;
            y[k++]=y2;
        }
        sort(l,l+k,cmp);
        sort(y,y+k);
        build(0,k-1,1);
        insert(l[0],1);
        for(int i=1;i<k;i++)
        {
            ans+=tree[1].len*(l[i].x-l[i-1].x);
            insert(l[i],1);
        }
        printf("Test case #%d\n",cas++);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}


POJ 1151 Atlantis 扫描线+线段树,布布扣,bubuko.com

POJ 1151 Atlantis 扫描线+线段树

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/crescent__moon/article/details/38473787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!