POJ1151-Atlantis

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17048 Accepted: 6493

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 
//AC代码 0MS
/*
题意:求重叠在一起矩形的面积
此题用:线段树+离散化+扫描线
这个虽然讲的很晦涩但是个人觉得看懂了前面一部分就理解什么是离散化和扫描线了
http://wenku.baidu.com/view/1856cf84b9d528ea81c77918.html
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=201;
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
struct Node
{
    int left;
    int right;//线段树的左右整点
    int flag;//记录重叠情况,大于零说明没有重叠
    double cnt;//记录实际的长度
    double lf;//左边端点真实的浮点数
    double rf;//右边端点真是的浮点数
}segTree[Max<<2];
struct Line
{
    double x;
    double y1;
    double y2;
    int ok;
}line[Max];
double y[Max];//记录y坐标的数组
//把一段段平行于y轴的线段表示成数组
//x是线段的x坐标,y1,y2线段对应的下端点和上端点的坐标
//一个矩形 ,左边的那条边ok为1,右边的为-1
//用来记录重叠情况,可以根据node节点中的flag这个来计算
bool cmp(Line u,Line v)//sort排序
{
    return u.x<v.x;
}
void Build_Tree(int t,int left,int right)
{
    segTree[t].left=left;
    segTree[t].right=right;
    segTree[t].lf=y[left];
    segTree[t].rf=y[right];
    if((left+1)==right) return;
    int mid=(left+right)>>1;
    Build_Tree(t<<1,left,mid);
    Build_Tree(t<<1|1,mid,right);//递归构造线段树,这里mid不能+1因为如果+1那么t<<1的右孩子和t<<1|1的左孩子不能产生联系最终更新父节点就是错误的数据
}
void Calen(int t)//计算长度
{
    if(segTree[t].flag>0)//如果没有重叠
    {
        segTree[t].cnt=segTree[t].rf-segTree[t].lf;
    }
    else if(segTree[t].left+1==segTree[t].right)//如果重叠了
    {
        segTree[t].cnt=0;
    }
    else
    {
        segTree[t].cnt=segTree[t<<1].cnt+segTree[t<<1|1].cnt;
    }
}
void Update(int t,Line L)//加入线段L,后更新线段树
{
    if(L.y1==segTree[t].lf&&L.y2==segTree[t].rf)
    {
        segTree[t].flag+=L.ok;
        Calen(t);
    }
    else if(L.y2<=segTree[t<<1].rf)
    {
        Update(t<<1,L);
    }
    else if(L.y1>=segTree[t<<1|1].lf)
    {
        Update(t<<1|1,L);
    }
    else
    {
        Line temp;
        temp=L;
        temp.y2=segTree[t<<1].rf;
        Update(t<<1,temp);
        temp=L;
        temp.y1=segTree[t<<1|1].lf;
        Update(t<<1|1,temp);
    }
    Calen(t);
}
void Init(int &m,double x1,double y1,double x2,double y2)
{
    line[m].x=x1;
    line[m].y1=y1;
    line[m].y2=y2;
    line[m].ok=1;
    y[m]=y1;
    m++;
    line[m].x=x2;
    line[m].y1=y1;
    line[m].y2=y2;
    line[m].ok=-1;
    y[m]=y2;
    m++;
}
int main()
{
    int n,m,i,j,Case;
    double x1,y1,x2,y2,area;
    Case=1;
    while(cin>>n&&n)
    {
        m=1;
        for(i=1;i<=n;i++)
        {
            cin>>x1>>y1>>x2>>y2;
            Init(m,x1,y1,x2,y2);
        }
        sort(line+1,line+m,cmp);
        sort(y+1,y+m);
        Build_Tree(1,1,m-1);
        Update(1,line[1]);
        area=0;
        for(i=2;i<m;i++)
        {
            //cout<<segTree[1].cnt<<" "<<line[i-1].x<<" "<<line[i].x<<endl;
            area+=segTree[1].cnt*(line[i].x-line[i-1].x);
            Update(1,line[i]);
        }
        cout<<"Test case #"<<Case++<<endl;
        cout<<"Total explored area: "<<setprecision(2)<<setiosflags(ios::fixed)<<area<<endl<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值