hdu 1542 Atlantis(线段树+扫描线——面积并)

来源:互联网 发布:c语言编译器哪个好用 编辑:程序博客网 时间:2024/06/06 00:48

Atlantis

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

ps:扫描线基础题

大神的分析:
1.矩形比较多,坐标也很大,所以横坐标需要离散化(纵坐标不需要),熟悉离散化后这个步骤不难,所以这里不详细讲解了,不明白的还请百度

2.重点:扫描线法:假想有一条扫描线,从左往右(从右往左),或者从下往上(从上往下)扫描过整个多边形(或者说畸形。。多个矩形叠加后的那个图形)。如果是竖直方向上扫描,则是离散化横坐标,如果是水平方向上扫描,则是离散化纵坐标。下面的分析都是离散化横坐标的,并且从下往上扫描的

扫描之前还需要做一个工作,就是保存好所有矩形的上下边,并且按照它们所处的高度进行排序,另外如果是上边我们给他一个值-1,下边给他一个值1,我们用一个结构体来保存所有的上下边

struct segment
{
double l,r,h; //l,r表示这条上下边的左右坐标,h是这条边所处的高度
int f; //所赋的值,1或-1
}

接着扫描线从下往上扫描,每遇到一条上下边就停下来,将这条线段投影到总区间上(总区间就是整个多边形横跨的长度),这个投影对应的其实是个插入和删除线段操作。还记得给他们赋的值1或-1吗,下边是1,扫描到下边的话相当于往总区间插入一条线段,上边-1,扫描到上边相当于在总区间删除一条线段(如果说插入删除比较抽象,那么就直白说,扫描到下边,投影到总区间,对应的那一段的值都要增1,扫描到上边对应的那一段的值都要减1,如果总区间某一段的值为0,说明其实没有线段覆盖到它,为正数则有,那会不会为负数呢?是不可能的,可以自己思考一下)。

每扫描到一条上下边后并投影到总区间后,就判断总区间现在被覆盖的总长度,然后用下一条边的高度减去当前这条边的高度,乘上总区间被覆盖的长度,就能得到一块面积,并依此做下去,就能得到最后的面积

(这个过程其实一点都不难,只是看文字较难体会,建议纸上画图,一画即可明白,下面献上一图希望有帮助)
这里写图片描述

从这个图,也可以感受到,就好比一个畸形的容器,往里面倒水,从最下面往上面涨,被水淹过的部分其实就是我们要求的面积

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 220struct segment//保存矩形的上下边{    double l,r,h;//左右横坐标,纵坐标    int f;//1为下边界,-1为上边界    segment() {};    segment(double x1,double x2,double y,int ic)    {        l=x1,r=x2,h=y,f=ic;    }    bool operator < (const segment&A)const//按高度从小到大排序    {        return h<A.h;    }} p[maxn];struct node{    int le,ri;    int cnt;//该节点被覆盖的情况    double len;//该区间被覆盖的总长度    int mid()    {        return (le+ri)>>1;    }} tree[maxn<<2];double pos[maxn];void Build(int rt,int le,int ri){    tree[rt].le=le;    tree[rt].ri=ri;    tree[rt].len=0;    tree[rt].cnt=0;    if(le==ri)        return ;    int mid=tree[rt].mid();    Build(rt<<1,le,mid);    Build(rt<<1|1,mid+1,ri);}void Upfather(int rt){    if(tree[rt].cnt)//非0,已经被整段覆盖        tree[rt].len=pos[tree[rt].ri+1]-pos[tree[rt].le];    else if(tree[rt].le==tree[rt].ri)//已经不是一条线段        tree[rt].len=0;    else//是一条线段但是又没有整段覆盖,那么只能从左右孩子的信息中获取        tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;}void Update(int rt,int val,int left,int right){    if(left<=tree[rt].le&&tree[rt].ri<=right)    {        tree[rt].cnt+=val;//更新这个区间被覆盖的情况        Upfather(rt);//更新这个区间被覆盖的总长度        return ;    }    int mid=tree[rt].mid();    if(left<=mid)        Update(rt<<1,val,left,right);    if(right>mid)        Update(rt<<1|1,val,left,right);    Upfather(rt);//计算该区间被覆盖的总长度}int Search(int le,int ri,double k){    while(le<=ri)    {        int mid=(le+ri)>>1;        if(pos[mid]==k)            return mid;        else if(pos[mid]>k)            ri=mid-1;        else            le=mid+1;    }}int main(){    int n,k=0;    double x1,x2,y1,y2;    while(~scanf("%d",&n),n)    {        int tot=0;        for(int i=0; i<n; ++i)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            pos[tot]=x1;            p[tot++]=segment(x1,x2,y1,1);//记录下边界的信息            pos[tot]=x2;            p[tot++]=segment(x1,x2,y2,-1);//记录上边界的信息        }        sort(pos,pos+tot);//横坐标升序排序        sort(p,p+tot);//按高度排序        int m=1;        for(int i=1; i<tot; ++i)//去重            if(pos[i]!=pos[i-1])                pos[m++]=pos[i];        Build(1,0,m-1);//离散化后的区间就是[0,m-1],以此建树        double ans=0;        for(int i=0; i<tot; ++i)//拿出每条横线并且更新        {            int le=Search(0,m-1,p[i].l);            int ri=Search(0,m-1,p[i].r)-1;            Update(1,p[i].f,le,ri);            ans+=tree[1].len*(p[i+1].h-p[i].h);//求面积        }        printf("Test case #%d\nTotal explored area: %.2f\n",++k,ans);        puts("");    }    return 0;}

参考博客:
Titanium

1 0
原创粉丝点击