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

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

时间:2014-08-11 17:51:13      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

Atlantis
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

 

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 

写代码是为了省事,没怎么考虑就写了update中的递归,找了一下午的错,才知道那个位置写错了。。。

扫描线第二弹

题意:给出了n个矩形的左下和右上的坐标,问所有的矩形面积的并是多少

1.在建立线段树是左子树为(l,mid)右子树为(mid,r),不能是mid+1,那样会使mid+1到mid一段不被统计到

2.按x由左向右扫描,因为给出的数是实数,所以除了要离散化x点外,对于线段树中的每一段的左右区间应该更新成实数y1,y2.,通过对区间的大小比对,判断直接得到值,还是继续深入,在向下一层深入的时候,要改变要判断的区间,使得区间在节点的控制内。

3.lazy数组标记了这条线段出现的次数,如果为0时,那么该节点等于左右字数节点的和,否则就是该节点的最大值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 300
struct node1{
    double l , r ;
    double sum ;
}cl[maxn<<3];
int lazy[maxn<<3] ;
struct node2{
    double x , y1 , y2 ;
    int flag ;
}p[maxn<<3];
double s[maxn<<3] ;
bool cmp(node2 a,node2 b)
{
    return a.x < b.x ;
}
void push_up(int rt)
{
    if( lazy[rt] > 0 )
        cl[rt].sum = cl[rt].r - cl[rt].l ;
    else
        cl[rt].sum = cl[rt*2].sum + cl[rt*2+1].sum ;
}
void creat(int rt,int l,int r)
{
    if( r - l > 1 )
    {
        cl[rt].l = s[l] ;
        cl[rt].r = s[r] ;
        creat(rt*2,l,(l+r)/2);
        creat(rt*2+1,(l+r)/2,r);
        push_up(rt);
    }
    else
    {
        cl[rt].l = s[l] ;
        cl[rt].r = s[r] ;
        cl[rt].sum = 0 ;
    }
    return ;
}
void update(int rt,double y1,double y2,int flag)
{
    if( cl[rt].l == y1 && cl[rt].r == y2 )
    {
        lazy[rt] += flag ;
        push_up(rt);
        return ;
    }
    else
    {
        if( cl[rt*2].r > y1 )
            update(rt*2,y1,min(cl[rt*2].r,y2),flag);
        if( cl[rt*2+1].l < y2 )
            update(rt*2+1,max(cl[rt*2+1].l,y1),y2,flag);
        push_up(rt);
    }
}
int main()
{
    int temp = 1 , n , i , j ;
    double x1 , y1 , x2 , y2 , ans ;
    while(scanf("%d", &n) && n)
    {
        ans = 0 ;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
            p[i].x = x1 ;
            p[i].y1 = y1 ;
            p[i].y2 = y2 ;
            p[i].flag = 1 ;
            p[i+n].x = x2 ;
            p[i+n].y1 = y1 ;
            p[i+n].y2 = y2 ;
            p[i+n].flag = -1 ;
            s[i+1] = y1 ;
            s[i+n+1] = y2 ;
        }
        sort(s+1,s+(2*n+1));
        sort(p,p+2*n,cmp);
        creat(1,1,2*n);
        memset(lazy,0,sizeof(lazy));
        update(1,p[0].y1,p[0].y2,p[0].flag);
        for(i = 1 ; i < 2*n ; i++)
        {
            ans += ( p[i].x-p[i-1].x )*cl[1].sum ;
            update(1,p[i].y1,p[i].y2,p[i].flag);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n", temp++, ans);
    }
    return 0;
}


 

poj1151-- Atlantis(线段树+离散化+扫描线),布布扣,bubuko.com

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

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://blog.csdn.net/winddreams/article/details/38495093

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