【HDU】 1542 Atlantis

Atlantis

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


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

题解:这一题第一眼看到想用扫描法,但是又不知道怎么扫,本来想用线段树存线段然后求交点的,后来想想不现实...后来发现如果用Y轴作为线段树区间的话好写多了。

首先我们需要把所有的竖直线段存下来,两段点和X的值,然后按X排序,方便以后扫描。然后我们把所有的Y值离散化,作为我们存放在线段树里的区间(因为区间都是整数的),然后我们从第一个线段开始扫描所有线段,每遇到一条线段就将其放入或是从线段树中取出(是左边就放入,是右边就取出,像一个栈一样)同时计算这条边与上一条边之间的句型面积,直到扫到最后一条边为止。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

struct tree
{
    int l,r,k;
    double w;
};
tree d[500005];
struct line
{
    double y1,y2,x;
    int k;
};
line l[405];
int n,h1;
double ans,x1,y1,x2,y2,py[405],pos[405];

void buildtree(int i,int l,int r)
{
    d[i].l=l; d[i].r=r; d[i].k=0; d[i].w=0;
    if (l==r) return ;
    buildtree(i*2,l,(l+r)/2);
    buildtree(i*2+1,(l+r)/2+1,r);
}

void _insert(int i,int l,int r,int k)
{
    int mid=(d[i].l+d[i].r)/2;
    if (d[i].l==l && d[i].r==r)
    {
        d[i].k+=k;
        if (d[i].k) d[i].w=pos[r+1]-pos[l];
        else if (l!=r) d[i].w=d[i*2].w+d[i*2+1].w;
        else d[i].w=0;
        return;
    }
    else if (r<=mid) _insert(i*2,l,r,k);
    else if (l>mid) _insert(i*2+1,l,r,k);
    else
    {
        _insert(i*2,l,mid,k);
        _insert(i*2+1,mid+1,r,k);
    }
    if (d[i].k==0) d[i].w=d[i*2].w+d[i*2+1].w;
    else d[i].w=pos[d[i].r+1]-pos[d[i].l];
}

void qsort(int low,int high)
{
    int i=low,j=high;
    double t=l[(i+j)/2].x;
    while (i<=j)
    {
        while (l[i].x<t) i++;
        while (l[j].x>t) j--;
        if (i<=j)
        {
            double k;
            k=l[i].x; l[i].x=l[j].x; l[j].x=k;
            k=l[i].y1; l[i].y1=l[j].y1; l[j].y1=k;
            k=l[i].y2; l[i].y2=l[j].y2; l[j].y2=k;
            k=l[i].k; l[i].k=l[j].k; l[j].k=k;
            i++; j--;
        }
    }
    if (low<j) qsort(low,j);
    if (i<high) qsort(i,high);
}

int place(double k)
{
    int i=0,j=h1,mid=(i+j)/2;
    while (pos[mid]!=k)
    {
        if (pos[mid]>k) j=mid-1;
        if (pos[mid]<k) i=mid+1;
        mid=(i+j)/2;
    }
    return mid;
}

int main()
{
    int CASE=1;
    while (scanf("%d",&n),n)
    {
        memset(l,0,sizeof(l));
        memset(pos,0,sizeof(pos));
        int h=1;
        for (int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            l[h].y1=y1; l[h].y2=y2; py[h]=y1; l[h].k=1; l[h++].x=x1;
            l[h].y1=y1; l[h].y2=y2; py[h]=y2; l[h].k=-1; l[h++].x=x2;
        }
        qsort(1,h-1);
        sort(py+1,py+h+1); h1=1;
        for (int i=1;i<=h;i++) if (py[i]!=py[i-1]) pos[h1++]=py[i];
        h1--;
        buildtree(1,0,h1); ans=0;
        _insert(1,place(l[1].y1),place(l[1].y2)-1,1);
        for (int i=2;i<h;i++)
        {
            ans+=(l[i].x-l[i-1].x)*d[1].w;
            _insert(1,place(l[i].y1),place(l[i].y2)-1,l[i].k);
        }
        printf("Test case #%d\n",CASE++);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值