HDU 1542 Atlantis------线段树之扫描线求面积

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

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

题意:求多个矩形形成的面积(可以相交)这道题用到线段树利用扫描线求面积。这里采用的是从下往上扫描,我们这里定义一个mark数组,用来记录某个区间下底边比上底边多的个数,定义一个s结构体存储扫描到某一区域的最左侧横坐标、最右侧横坐标,和高度,以及标记上下边.下边相当于插入标记为1,上边相当于删除标记为-1.当扫描到下底边的时候mark[1]+=1,当扫描到上底边是mark[1]+=-1。

将线段排序后,扫描线段如上图所示

当扫到①时mark[1]=1(扫到矩形1的下边)面积为粉色部分

当扫到②时mark[1]=1+1(扫到矩形2的下边)面积为绿色部分

当扫到③时mark[1]=1+1-1(扫到矩形1的上边)面积为蓝色部分

当扫到④时mark[1]=1+1-1-1(扫到矩形2的上边)面积为0

将所有区域的面积相加即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 99999999
using namespace std;
const int MAX=100+10;
int mark[MAX<<4];//记录某个区间的下底边比上底边多的个数
double sum[MAX<<4];//记录某个区间下底边比上底边多的个数总长度
double Hash[MAX<<2];//对横坐标x离散化

struct seg 
{
    double l,r,h;//扫描后形成区域的左侧横坐标、右侧横坐标、高度
    int d;//标记矩形的上边和下边,下边标记为1,上边标记为-1
    seg() {}
    seg(double x1,double x2,double H,int D):l(x1),r(x2),h(H),d(D) {}
    bool operator<(const seg &a)const
    {
        return h<a.h;
    }
} s[MAX<<2];//记录扫描线

void Upfather(int n,int left,int right){
    if(mark[n])
        sum[n]=Hash[right+1]-Hash[left];//表示该区间整个线段长度可作为底边。right+1是因为区间左闭右开要避免区间缺失,区间缺失:[1,a][a+1,b][b+1,c]....没有[a,a+1]所以要左闭右开。
    else if(left == right)
        sum[n]=0;叶子结点区间长度为0,则底边长度为0
    else
        sum[n]=sum[n<<1]+sum[n<<1|1];
}

void Update(int L,int R,int d,int n,int left,int right){
    if(L<=left && right<=R) //该区间是当前扫描线段的一部分
    {
        mark[n]+=d;//更新上下底边之差
        Upfather(n,left,right);//更新可用底边长
        return;
    }
    int mid=left+right>>1;
    if(L<=mid)
        Update(L,R,d,n<<1,left,mid);
    if(R>mid)
        Update(L,R,d,n<<1|1,mid+1,right);
    Upfather(n,left,right);
}

int main(){
    double x1=0,y1=0,x2=0,y2=0;
    int c=0,n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        int siz=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            if(x1>x2)swap(x1,x2);
            if(y1>y2)swap(y1,y2);
            Hash[siz]=x1;
            s[siz++]=seg(x1,x2,y1,1);//标记下边
            Hash[siz]=x2;
            s[siz++]=seg(x1,x2,y2,-1);//标记上边
        }
        sort(Hash,Hash+siz);//把x坐标从小到大排序
        sort(s,s+siz);//把线段按高度h从小到大排序
        int k=1;
        for(int i=1; i<siz; ++i) //去重复端点
        {
            if(Hash[i] != Hash[i-1])
                 Hash[k++]=Hash[i];
        }
        double ans=0;
        for(int i=0; i<siz; ++i){
            int L=lower_bound(Hash,Hash+k,s[i].l)-Hash;
            int R=lower_bound(Hash,Hash+k,s[i].r)-Hash-1;//区间左闭右开
            Update(L,R,s[i].d,1,0,k-1);//扫描线段更新可用底边长
            ans+=sum[1]*(s[i+1].h-s[i].h);//新增加面积
        }
        printf("Test case #%d\n",++c);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

对于求矩形周长的,这里我就不加以讲解,可以推荐一个我觉得写的很详细,易懂的博文,我就是看这个看懂的。。

hdu1828线段树+扫描线 | 学步园

https://www.xuebuyuan.com/2102073.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值