HDU 1542 Atlantis(矩形面积并)

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3469    Accepted Submission(s): 1549


Problem 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
 

 

Source
 

 

Recommend
linle
 
 
 
矩形面积并,求至少覆盖一次的面积。
线段树经典题。重新做了下。
/*
HDU 1542
线段树求矩形面积并
kuangbin
*/


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAXN=220;
struct Node
{
    int l,r;//离散化后的左右端点
    int c;//记录重叠情况
    double cnt;//实在长度
    double lf,rf;
    //左右真实的浮点数端点
}segTree[MAXN*3];
struct Line
{
    double x,y1,y2;
    int f;
}line[MAXN];
//把一条平行于y轴的线段表示成数组
//一个矩形的左边f=1,右边f=-1;
//根据f来计算Node 中的c

bool cmp(Line a,Line b)
{
    return a.x<b.x;
}

double y[MAXN];

void Build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].cnt=0;
    segTree[i].c=0;
    segTree[i].lf=y[l];
    segTree[i].rf=y[r];
    if(l+1==r)return;
    int mid=(l+r)>>1;
    Build(i<<1,l,mid);
    Build((i<<1)|1,mid,r);
}

void calen(int i)//计算实在长度
{
    if(segTree[i].c>0)
    {
        segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        return;
    }
    if(segTree[i].l+1==segTree[i].r) segTree[i].cnt=0;
    else segTree[i].cnt=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
}


void update(int i,Line e)
{
    if(e.y1==segTree[i].lf&&e.y2==segTree[i].rf)
    {
        segTree[i].c+=e.f;
        calen(i);
        return;
    }
    if(e.y2<=segTree[i<<1].rf) update(i<<1,e);
    else if(e.y1>=segTree[(i<<1)|1].lf)  update((i<<1)|1,e);
    else
    {
        Line temp=e;
        temp.y2=segTree[i<<1].rf;
        update(i<<1,temp);
        temp=e;
        temp.y1=segTree[(i<<1)|1].lf;
        update((i<<1)|1,temp);
    }
    calen(i);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int iCase=0;
    double x1,y1,x2,y2;
    int n;
    while(scanf("%d",&n),n)
    {
        iCase++;
        int t=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[t].x=x1;
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].f=1;
            y[t]=y1;
            t++;
            line[t].x=x2;
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].f=-1;
            y[t]=y2;
            t++;
        }
        sort(line+1,line+t,cmp);
        sort(y+1,y+t);
        Build(1,1,t-1);
        update(1,line[1]);
        double ans=0;
        for(int i=2;i<t;i++)
        {
            ans+=segTree[1].cnt*(line[i].x-line[i-1].x);
            update(1,line[i]);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",iCase,ans);
    }
    return 0;
}

 

posted on 2012-08-15 22:00  kuangbin  阅读(1229)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: