hdu 1542 Atlantis (线段树+扫描线)

Atlantis

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

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

题目来源:

http://acm.hdu.edu.cn/showproblem.php?pid=1542

题目大意:

再一个平面上给你一些矩形,要你求矩形一共覆盖的面积。

线段树扫描线求面积。

这个问题,还是推荐一篇博客,我就只谈谈对博客的感想:

【hdu1542】线段树求矩形面积并 - 拦路雨偏似雪花 - 博客园

首先,给的数据是浮点数,一定要离散化,最好要去重。我这里手写一份自己常用的适应性很高离散化代码(去重),大家可以自己拿去调试一下。到本题中还是要有不小的改动,代码后面也会贴出来,读者可以对比一下。

#include<cstdio>
#include<algorithm>

using namespace std;

const int N=100007;

int t[N],a[N];

int main()
{
      int n;
      scanf("%d",&n);
      for(int i=0;i<n;i++)
      {
            scanf("%d",a+i);
            t[i]=a[i];
      }
      sort(t,t+n);
      int m=unique(t,t+n)-t;//共有每个不重复的元素
      for(int i=0;i<n;i++)
            a[i]=lower_bound(t,t+m,a[i])-t+1;//从1开始编号
      for(int i=0;i<n;i++)
            printf("%d ",a[i]);//测试输出
      return 0;
}   

之前建线段树都是以某个点为叶子结点的,但是这次却不能这么写,而要以单位长度为叶子结点。

线段树主要维护cnt(-1表示覆盖不明确,>=0表示完全覆盖cnt次,模仿海报问题),len(区间长度,这就要求离散化的时候建立离散后的数值与原数值的映射)。

其他的参考那篇博客就好啦。

#include<cstdio>
#include<algorithm>

using namespace std;

double t[410];//排序数组
double a[410];//原始数据
int b[410];//离散后的数据
double c[410];//离散后的数据到原数据的映射

struct tsegment
{
    int x1,x2;
    int y;
    int flag;
};
tsegment segment[210];

int cmp(tsegment a,tsegment b)
{
    return a.y<b.y;
}

struct ttree
{
    int l,r;
    int cover;//-1不确定 >=0完全覆盖次数 就不打tag了,时间可能会长点
    double len;//区间长度
};
ttree tree[400*4+20];

void pushup(int x)
{
    if(tree[x].l+1==tree[x].r)
        return;
    if(tree[x*2].cover==tree[x*2+1].cover&&tree[x*2].cover>=0)
        tree[x].cover=tree[x*2].cover;
    else
        tree[x].cover=-1;
}

void pushdown(int x)
{
    if(tree[x].l+1==tree[x].r)
        return;
    if(tree[x].cover>=0)
        tree[x*2].cover=tree[x*2+1].cover=tree[x].cover;
}

void build(int x,int l,int r)
{
    tree[x].l=l;
    tree[x].r=r;
    tree[x].len=c[r]-c[l];
    tree[x].cover=0;
    if(l+1<r)
    {
        int mid=(l+r+1)/2;
        build(x*2,l,mid);
        build(x*2+1,mid,r);//叶子节点为一小段区间
    }
}

void modify(int x,int l,int r,int flag)
{
    if(l<=tree[x].l&&r>=tree[x].r&&tree[x].cover>=0)
    {
        tree[x].cover+=flag;
        //printf("%d %d\n",tree[x].l,tree[x].r);
    }
    else if(tree[x].l+1<tree[x].r)
    {
        pushdown(x);
        int mid=(tree[x].l+tree[x].r+1)/2;
        if(l<mid)
            modify(x*2,l,r,flag);
        if(r>mid)
            modify(x*2+1,l,r,flag);
        pushup(x);
    }
}

double query(int x)
{
    if(tree[x].cover>0)
        return tree[x].len;
    else if(tree[x].cover==0)
        return 0;
    else
    {
        pushdown(x);
        double ret=0;
        ret+=query(x*2);
        ret+=query(x*2+1);
        return ret;
    }
}

int main()
{
    int n,k=1;
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n*4;i++)
        {
            scanf("%lf",a+i);
            t[i]=a[i];
        }
        sort(t,t+n*4);
        int m=unique(t,t+n*4)-t;//共有m个不同的数据
        for(int i=0;i<n*4;i++)
        {
            b[i]=lower_bound(t,t+m,a[i])-t+1;//编号从1开始
            c[b[i]]=a[i];
        }
        //for(int i=1;i<=m;i++)    printf("%f\n",c[i]);

        for(int i=0;i<n;i++)
        {
            int x1=b[i*4],y1=b[i*4+1];
            int x2=b[i*4+2],y2=b[i*4+3];
            segment[i*2].x1=x1;segment[i*2].x2=x2;
            segment[i*2].y=y1;segment[i*2].flag=1;
            segment[i*2+1].x1=x1;segment[i*2+1].x2=x2;
            segment[i*2+1].y=y2;segment[i*2+1].flag=-1;
        }
        sort(segment,segment+n*2,cmp);

        build(1,1,m);
        modify(1,segment[0].x1,segment[0].x2,segment[0].flag);
        double ans=0;
        for(int i=1;i<n*2;i++)
        {
            //printf("%.2f\n",query(1));
            //printf("%d %d\n",segment[i].x1,segment[i].x2);
            ans=ans+query(1)*(c[segment[i].y]-c[segment[i-1].y]);
            modify(1,segment[i].x1,segment[i].x2,segment[i].flag);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",k++,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/acboyty/p/9532423.html

时间: 2024-10-07 06:12:13

hdu 1542 Atlantis (线段树+扫描线)的相关文章

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

hdu 1542 Atlantis(线段树)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6899    Accepted Submission(s): 3022 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线段树维护两个值:cover和len,cover表示该线段区间目前被覆盖的线段数目,len表示当前已覆盖的线段长度(化为离散前的真值),每次加入一条线段,将其y_low,y_high之间的区间染上line[i].cover,再以tree[1].len乘以接下来的线段的x坐标减去当前x坐标,即计算了一部

Atlantis HDU - 1542(线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23452    Accepted Submission(s): 9278 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 1542(线段树+扫描线 求矩形相交面积)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12059    Accepted Submission(s): 5083 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 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. You