HDU 1542 Atlantis

Atlantis

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

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 1394 2795

思路:线段树+扫描线。

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 300
using namespace std;
double y[N];
struct Node{
    double x;double y1;double y2;
    int flag;
}node[N];
struct node{
    int l;int r;double ml;double mr;int s;double len;
}a[N*3];
bool cmp(Node a,Node b){
    return a.x-b.x<0.0000001;
}
void build(int i,int left,int right){
    a[i].l=left;
    a[i].r=right;
    a[i].ml=y[left];
    a[i].mr=y[right];
    a[i].s=0;
    a[i].len=0;
    if(a[i].l+1==a[i].r){
        return ;
    }
    int mid=(left+right)>>1;
    build(i*2,left,mid);
    build(i*2+1,mid,right);//建树时注意这里不是mid+1,因为做相减的时候如果mid+1这么建回到值左孩子的右边与有孩子的左边无法进行运算
}
void callen(int i){  

    if(a[i].s>0){//注意这里不是所有边都是左孩子的长度加上右孩子的长度,他存在一个覆盖问题
        a[i].len=a[i].mr-a[i].ml;
    }else if(a[i].r-a[i].l==1){
        a[i].len=0;
    }else{
        a[i].len=a[i*2].len+a[i*2+1].len;
    }
    return ;
}
void updata(int i,Node b){
    if(a[i].ml==b.y1&&a[i].mr==b.y2){
        a[i].s+=b.flag;
        callen(i);
        return ;
    }
    if(b.y2<=a[i*2].mr) updata(i*2,b);
    else if(b.y1>=a[i*2+1].ml) updata(i*2+1,b);
    else{
        Node temp=b;
        temp.y2=a[i*2].mr;
        updata(i*2,temp);
        temp=b;
        temp.y1=a[i*2+1].ml;
        updata(i*2+1,temp);
    }
    callen(i);
    return ;
}
int main(){
    int n,t,p=1,te;
    double x1,x2,y1,y2;
    while(scanf("%d",&n),n){
            t=1;
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            node[t].x=x1;
            node[t].y1=y1;
            node[t].y2=y2;
            node[t].flag=1;//入边
            y[t++]=y1;
            node[t].x=x2;
            node[t].y1=y1;
            node[t].y2=y2;
            node[t].flag=-1;//出边
            y[t++]=y2;
        }
        sort(node+1,node+t,cmp);
        sort(y+1,y+t);
        build(1,1,t-1);
        updata(1,node[1]);
        double sum=0;
        for(int i=2;i<t;i++){
            sum+=a[1].len*(node[i].x-node[i-1].x);
            updata(1,node[i]);
        }
        printf("Test case #%d\n",p++);
        printf("Total explored area: %.2lf\n\n",sum);
    }
    return 0;
}  
时间: 2024-10-15 15:01:00

HDU 1542 Atlantis的相关文章

HDU 1542 —— Atlantis 【矩形面积并:扫描线】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14795 #include <iostream> #include <cstring> #include <string> #include <algorithm> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAX

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(矩形面积并)

HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 205; const int M = 100005; const dou

HDU 1542 Atlantis (线段树求矩阵覆盖面积)

题意:给你n个矩阵求覆盖面积. 思路:看了别人的结题报告 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x2,y2),对这样的一个矩形,我们构造两条线段,一条定位在x1,它在y坐标的区间是[y1,y2],并且给定一个cover域值为1:另一条线段定位在x2,区间一样是[y1,y2],给定它一个cover值为-1.根据这样的方法对每个矩形都构造两个线段,最后将所有的线段根据所定位的x从左到右进行排序 #include <iostream> #include <stdio.h

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

线段树+扫描线+离散化解poj1151 hdu 1542 ( Atlantis )

受此链接很大启发才明白扫描线: http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 我的代码如下: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #i

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

HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

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 i