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

HDU 1542 Atlantis (求矩形面积并)

时间:2016-08-05 23:03:06      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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 file 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
 
题意:求矩形面积的并,可以参考这篇博客,写的很好:http://www.cnblogs.com/scau20110726/archive/2013/03/21/2972808.html
/*
1.保存矩形的上下边界,并且重要的,记录他们是属于上还是下,然后按高度升序排序
2.保存竖线坐标,并且去重,是为了离散化
3.以保存的上下边界数组去更新
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 110;

struct Node
{
    int l,r;
    double cnt;//该节点的覆盖情况
    double len;//该区间被覆盖的总长度
}segTree[2*MAXN*4];
struct Segment
{
    double l,r,h;
    int f;
}ss[MAXN*2];
double pos[MAXN*2];

bool cmp(const Segment& aa,const Segment& bb)
{
    return aa.h<bb.h;
}
void build(int id,int l,int r)
{
    segTree[id].l=l;
    segTree[id].r=r;
    segTree[id].cnt=0;
    segTree[id].len=0;
    if(l==r) return;
    int mid=(l+r)/2;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);
}
void calen(int id)
{
    if(segTree[id].cnt)
        segTree[id].len=pos[segTree[id].r+1]-pos[segTree[id].l];
    else if(segTree[id].l==segTree[id].r) segTree[id].len=0;
    else segTree[id].len=segTree[id*2].len+segTree[id*2+1].len;
}
void update(int id,int l,int r,int val)
{
    if(segTree[id].l==l&&segTree[id].r==r)
    {
        segTree[id].cnt+=val;
        calen(id);
        return;
    }
    int mid=(segTree[id].l+segTree[id].r)/2;
    if(l>mid) update(id*2+1,l,r,val);
    else if(r<=mid) update(id*2,l,r,val);
    else
    {
        update(id*2,l,mid,val);
        update(id*2+1,mid+1,r,val);
    }
    calen(id);
}

int main()
{
    int n;
    int iCase=0;
    double x1,y1,x2,y2;
    while(scanf("%d",&n)==1&&n)
    {
        int t=1;
        iCase++;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            ss[t].l=x1,ss[t].r=x2,ss[t].h=y1,ss[t].f=1,pos[t]=x1,t++;
            ss[t].l=x1,ss[t].r=x2,ss[t].h=y2,ss[t].f=-1,pos[t]=x2,t++;
        }
        sort(ss+1,ss+t,cmp);
        sort(pos+1,pos+t);
        //for(int i=1; i<t; i++) printf("%.2lf %.2lf  %.2lf\n",ss[i].l,ss[i].r,ss[i].h);
        int m=2;
        for(int i=2;i<t;i++)
            if(pos[i]!=pos[i-1])
                pos[m++]=pos[i];
        build(1,1,m-1);
        double ans=0;
        for(int i=1;i<t;i++)
        {
            int l=lower_bound(pos+1,pos+m,ss[i].l)-pos;
            int r=lower_bound(pos+1,pos+m,ss[i].r)-pos-1;
            //cout<<l<<" "<<r<<endl;
            update(1,l,r,ss[i].f);
            ans+=(ss[i+1].h-ss[i].h)*segTree[1].len;
        }
        printf("Test case #%d\n",iCase);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}

 

 
 

HDU 1542 Atlantis (求矩形面积并)

标签:

原文地址:http://www.cnblogs.com/wangdongkai/p/5742861.html

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