HDU1542 Atlantis (扫描线)

Atlantis

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

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
Mid-Central European Regional Contest 2000

Recommend
linle | We have carefully selected several similar problems for you: 1828 1255 1540 1823 2871
阿就是裸扫描线,需要离散化一下。套路还不是很熟做个记录。

#include <cstring>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#define max 205
using namespace std;
int cntline;
//mark 1表示下界 -1表示上界
typedef struct type_line
{
    double l,r,y;
    int mark;
}typle_line;
type_line line[max];
vector<double> x;
bool cmp(const type_line &a, const type_line &b)
{
    return a.y<b.y;
}
void addline(double l,double r,double y,int mark)
{
    line[cntline].l=l;
    line[cntline].r=r;
    line[cntline].y=y;
    line[cntline++].mark=mark;
    return;
}
typedef struct node
{
    int l,r,mark;
    double line_left,line_right,len;
}node;
node tree[max*4];
void build(int l,int r,int root)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].mark=tree[root].len=0;
    tree[root].line_left=x[l];
    tree[root].line_right=x[r];
    if(l+1==r)
        return;
    else
    {
        int mid=(tree[root].l+tree[root].r)/2;
        build(l,mid,root*2);
        build(mid,r,root*2+1);
    }
    return;
}
void update(int left,int right,int root,int mark)
{
    double l=tree[root].l;
    double r=tree[root].r;
    if(left<=l&&right>=r)
    {
        tree[root].mark+=mark;
    }else
    {
        int mid=(tree[root].r+tree[root].l)/2;
        if(left<mid)
            update(left,right,root*2,mark);
        if(right>mid)
            update(left,right,root*2+1,mark);
    }
    if(tree[root].mark)
        tree[root].len=tree[root].line_right-tree[root].line_left;
    else if(tree[root].l+1==tree[root].r)
    {
        tree[root].len=0;
    }
    else
        tree[root].len=tree[root*2].len+tree[root*2+1].len;

}

int main()
{
    int n;
    int cnt=0;
    while(scanf("%d",&n)&&n!=0)
    {
        cntline=0;
        x.clear();
        double x1,y1,x2,y2;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            addline(x1,x2,y1,1);
            addline(x1,x2,y2,-1);
            x.push_back(x1);
            x.push_back(x2);
        }
        sort(line,line+cntline,cmp);
        sort(x.begin(),x.end());
        x.erase(unique(x.begin(),x.end()),x.end());
        build(0,x.size()-1,1);
        double ans=0;
        for(int i=0;i<=cntline-1;i++)
        {
            int l_pos=lower_bound(x.begin(),x.end(),line[i].l)-x.begin();
            int r_pos=lower_bound(x.begin(),x.end(),line[i].r)-x.begin();
            if(i>0)
                ans+=(line[i].y-line[i-1].y)*tree[1].len;
            update(l_pos,r_pos,1,line[i].mark);
        }
        printf("Test case #%d\n",++cnt);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值