hdu-1542 Atlantis(离散化+线段树+扫描线算法)

题目链接:

Atlantis

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/32768 K (Java/Others)


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
 
题意:
 
给了n个矩形的左下角和右上角的坐标,问这些矩形的面积和是多少;
 
思路:
 
离散化后用线段树结合扫描线算法可以做,是扫描线算法的模板题;
 
AC代码:
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n;
double rec[2*N],xa,xb,ya,yb;
struct Tree
{
    int l,r,cover;
    double sum;
};
Tree tree[4*N];
struct Line
{
    double l,r,h;
    int flag;
};
Line line[4*N];
int cmp(Line x,Line y)
{
    return x.h<y.h;
}
void build(int node,int L,int R)
{
    tree[node].sum=0;
    tree[node].cover=0;
    tree[node].l=L;
    tree[node].r=R;
    if(L>=R)return ;
    int mid=(L+R)>>1;
    build(2*node,L,mid);
    build(2*node+1,mid+1,R);
}
void Pushup(int node)
{
    if(tree[node].cover)
    {
        tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l];
    }
    else
    {
        if(tree[node].l!=tree[node].r)
        tree[node].sum=tree[2*node].sum+tree[2*node+1].sum;
        else tree[node].sum=0;
    }
}
void update(int node,int L,int R,int x)
{
    if(L<=tree[node].l&&R>=tree[node].r)
    {
        tree[node].cover+=x;
        Pushup(node);
        return ;
    }
    int mid=(tree[node].l+tree[node].r)>>1;
    if(R<=mid)update(2*node,L,R,x);
    else if(L>mid)update(2*node+1,L,R,x);
    else
    {
        update(2*node,L,mid,x);
        update(2*node+1,mid+1,R,x);
    }
    Pushup(node);
}
map<double,int>mp;
int main()
{
    int Case=1;
    while(1)
    {
        scanf("%d",&n);
        if(n==0)break;
        printf("Test case #%d\n",Case++);
        int cnt=1;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb);

            rec[cnt]=line[cnt].l=xa;
            line[cnt].r=xb;
            line[cnt].flag=1;
            line[cnt++].h=ya;

            line[cnt].l=xa;
            rec[cnt]=line[cnt].r=xb;
            line[cnt].h=yb;
            line[cnt++].flag=-1;
        }
        sort(line+1,line+cnt,cmp);
        sort(rec+1,rec+cnt);
        int num=2;
        for(int i=2;i<cnt;i++)
        {
            if(rec[i]!=rec[i-1])rec[num++]=rec[i];
        }
        for(int i=1;i<num;i++)
        {
            mp[rec[i]]=i;
        }
        build(1,1,num-1);
        double ans=0;

        for(int i=1;i<cnt-1;i++)
        {
            int fx=mp[line[i].l];
            int fy=mp[line[i].r];
            update(1,fx,fy-1,line[i].flag);
            ans+=tree[1].sum*(line[i+1].h-line[i].h);
        }
        printf("Total explored area: %.2lf\n",ans);
        printf("\n");
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5366207.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值