POJ 1151 Atlantis

Posted litos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1151 Atlantis相关的知识,希望对你有一定的参考价值。

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24566   Accepted: 9116

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 

题目链接
参考自:https://blog.csdn.net/u013480600/article/details/39322791


题意
给你n个矩形,求他们的总面积之和

分析

直接离散化,所有矩形的边界延伸将空间分隔成许多小矩形,我们需要的就是找出这些小矩形。将坐标离散化,排序后去重,
然后逐个大矩形来计算哪些小矩形被包含了,定义mp[i][j]为以(x[i],y[i])为左下角(原文这里应该是打错了),(x[i+1],y[j+1])为右上角的小矩形。
最后我们只需找出mp[i][j]==true的矩形并计算面积即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 300;
const int mod = 77200211+233;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1

struct Node{
    double x1,y1,x2,y2;
}node[maxn];
double x[maxn],y[maxn];
bool mp[maxn][maxn];

int bfind(double *a,double x,int n){
    int l=0,r=n-1,m;
    while(r>=l){
        m=(r+l)>>1;
        if(a[m]==x) return m;
        else if(a[m]<x) l=m+1;
        else r=m-1;
    }
    return -1;
}

int main(){
    int n,n1,n2,cas=0;
    while(scanf("%d",&n)==1&&n){
        n1=n2=0;
        ms(mp,false);
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf%lf",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2);
            x[n1++]=node[i].x1;
            x[n1++]=node[i].x2;
            y[n2++]=node[i].y1;
            y[n2++]=node[i].y2;
        }
        sort(x,x+n1);
        sort(y,y+n2);
        n1=unique(x,x+n1)-x;
        n2=unique(y,y+n2)-y;

        for(int i=0;i<n;i++){
            int Lx=bfind(x,node[i].x1,n1);
            int Rx=bfind(x,node[i].x2,n1);
            int Ly=bfind(y,node[i].y1,n2);
            int Ry=bfind(y,node[i].y2,n2);

            for(int k=Lx;k<Rx;k++){
                for(int j=Ly;j<Ry;j++){
                    mp[k][j]=true;
                }
            }
        }
        double ans=0;
        for(int i=0;i<n1;i++){
            for(int j=0;j<n2;j++){
                if(mp[i][j]){
                    ans += (x[i+1]-x[i])*(y[j+1]-y[j]);
                }
            }
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++cas,ans);
    }
}

 

其实还能用线段树扫描线来解决,实质上都是离散化。日后补上。。。


以上是关于POJ 1151 Atlantis的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1151 Atlantis(扫描线)

POJ 1151Atlantis

POJ1151 Atlantis

POJ 1151 Atlantis

POJ1151 Atlantis 扫描线

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