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

扫描线 [POJ 1151] Atlantis

时间:2014-12-29 18:20:31      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

一样的题: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 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 

求矩形并、线段树+扫描线
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210

int n;
int tot;
double sum;
double y[N];
double yl[N<<2];
double yr[N<<2];
int cover[N<<2];
double height[N<<2];
struct X
{
    int pos;
    double x,y1,y2;
    bool operator <(const X &t)const
    {
        return x<t.x;
    }
}x[N];

void pushup(int l,int r,int rt)
{
    if(cover[rt]>0) height[rt]=yr[rt]-yl[rt];
    else if(l+1==r) height[rt]=0;
    else height[rt]=height[rt<<1]+height[rt<<1|1];
}
void build(int l,int r,int rt)
{
    cover[rt]=0;
    yl[rt]=y[l];
    yr[rt]=y[r];
    if(l+1==r) return;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m,r,rt<<1|1);
}
void update(int l,int r,int rt,double L,double R,int c)
{
    if(yl[rt]==L && yr[rt]==R)
    {
        cover[rt]+=c;
        pushup(l,r,rt);
        return;
    }
    int m=(l+r)>>1;
    if(R<=y[m]) update(l,m,rt<<1,L,R,c);
    else if(L>=y[m]) update(m,r,rt<<1|1,L,R,c);
    else
    {
        update(l,m,rt<<1,L,y[m],c);
        update(m,r,rt<<1|1,y[m],R,c);
    }
    pushup(l,r,rt);
}
int main()
{
    int i,iCase=1;
    while(scanf("%d",&n),n)
    {
        tot=0;
        sum=0;
        memset(height,0,sizeof(height));
        for(i=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            y[tot]=y1;
            x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=1;
            y[tot]=y2;
            x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-1;
        }
        sort(x,x+tot);
        sort(y,y+tot);
        build(0,tot-1,1);
        for(i=1;i<tot;i++)
        {
            update(0,tot-1,1,x[i-1].y1,x[i-1].y2,x[i-1].pos);
            sum+=height[1]*(x[i].x-x[i-1].x);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",iCase++,sum);
    }
    return 0;
}

 

一样的题:POJ 1389

 

Area of Simple Polygons
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3193   Accepted: 1627

Description

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates. 

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. 

Example: Consider the following three rectangles: 

rectangle 1: < (0, 0) (4, 4) >, 

rectangle 2: < (1, 1) (5, 2) >, 

rectangle 3: < (1, 1) (2, 5) >. 

The total area of all simple polygons constructed by these rectangles is 18. 

Input

The input consists of multiple test cases. A line of 4 -1‘s separates each test case. An extra line of 4 -1‘s marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

Output

For each test case, output the total area of all simple polygons in a line. 

Sample Input

0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  

Sample Output

18
10 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210

int n;
int tot;
double sum;
double y[N];
double yl[N<<2];
double yr[N<<2];
int cover[N<<2];
double height[N<<2];
struct X
{
    int pos;
    double x,y1,y2;
    bool operator <(const X &t)const
    {
        return x<t.x;
    }
}x[N];

void pushup(int l,int r,int rt)
{
    if(cover[rt]>0) height[rt]=yr[rt]-yl[rt];
    else if(l+1==r) height[rt]=0;
    else height[rt]=height[rt<<1]+height[rt<<1|1];
}
void build(int l,int r,int rt)
{
    cover[rt]=0;
    yl[rt]=y[l];
    yr[rt]=y[r];
    if(l+1==r) return;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m,r,rt<<1|1);
}
void update(int l,int r,int rt,double L,double R,int c)
{
    if(yl[rt]==L && yr[rt]==R)
    {
        cover[rt]+=c;
        pushup(l,r,rt);
        return;
    }
    int m=(l+r)>>1;
    if(R<=y[m]) update(l,m,rt<<1,L,R,c);
    else if(L>=y[m]) update(m,r,rt<<1|1,L,R,c);
    else
    {
        update(l,m,rt<<1,L,y[m],c);
        update(m,r,rt<<1|1,y[m],R,c);
    }
    pushup(l,r,rt);
}
int main()
{
    int i;
    while(1)
    {
        tot=0;
        sum=0;
        memset(height,0,sizeof(height));
        n=0;
        double x1,y1,x2,y2;
        while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) && x1+x2+y1+y2!=-4)
        {
            y[tot]=y1;
            x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=1;
            y[tot]=y2;
            x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-1;
            n++;
        }
        if(n==0) break;
        sort(x,x+tot);
        sort(y,y+tot);
        build(0,tot-1,1);
        for(i=1;i<tot;i++)
        {
            update(0,tot-1,1,x[i-1].y1,x[i-1].y2,x[i-1].pos);
            sum+=height[1]*(x[i].x-x[i-1].x);
        }
        printf("%.0f\n",sum);
    }
    return 0;
}

 

扫描线 [POJ 1151] Atlantis

标签:

原文地址:http://www.cnblogs.com/hate13/p/4192142.html

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