hdu 1542 Atlantis(矩形面积并)

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8806    Accepted Submission(s): 3788

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

题意:给你n个矩形,求面积的并。

http://www.cnblogs.com/scau20110726/archive/2013/03/21/2972808.html

这里有句话对扫描线的理解很有帮助:就好比一个畸形的容器,往里面倒水,从最下面往上面涨,被水淹过的部分其实就是我们要求的面积。

其实就是线段树不断更新X轴被覆盖的情况,然后倒水,水深增加为a[i+1].y-a[i].y,这样面积就是所有x轴覆盖的长度*a[i+1].y-a[i].y。

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#define lc idx<<1
#define rc idx<<1|1
#define lson l,mid,lc
#define rson mid,r,rc
#define N 222

using namespace std;
int n,m;
double X[N];

struct Jidong {
    double lx,rx,y;
    int flag;
} a[N];

struct _Jidong {
    double len;
    int cnt;
} tree[N<<2];

bool cmp(Jidong a,Jidong b) {
    if(a.y==b.y)return a.flag>b.flag;
    return a.y<b.y;
}

void push_up(int l,int r,int idx) {
    if(tree[idx].cnt)
        tree[idx].len=X[r]-X[l];
    else if(l+1==r)tree[idx].len=0;
    else
        tree[idx].len=tree[lc].len+tree[rc].len;
}

void build(int l,int r,int idx) {
    tree[idx].len=tree[idx].cnt=0;
    if(l+1==r)return;
    int mid=(l+r)>>1;
    build(l,mid,lc);
    build(mid,r,rc);///这里必须注意
}

void update(int l,int r,int idx,int x,int y,int flag) {
    if(x<=l&&r<=y) {
        tree[idx].cnt+=flag;
        push_up(l,r,idx);
        return;
    }
    int mid=(r+l)>>1;
    if(x<mid)update(lson,x,y,flag);
    if(y>mid)update(rson,x,y,flag);
    push_up(l,r,idx);
}

int main() {
    //freopen("test.in","r",stdin);
    int ca=1;
    while(cin>>n) {
        if(!n)break;
        double x,y,_x,_y;
        m=1;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf%lf%lf",&x,&y,&_x,&_y);
            X[m]=x;
            a[m].lx=x,a[m].rx=_x;
            a[m].y=y,a[m++].flag=1;
            X[m]=_x;
            a[m].lx=x,a[m].rx=_x;
            a[m].y=_y,a[m++].flag=-1;
        }
        sort(X+1,X+m);//离散x,排序
        sort(a+1,a+m,cmp);///y从小到大排序
        int mm=1;
        X[mm++]=X[1];
        for(int i=2; i<m; i++) {///去重
            if(X[i]!=X[i-1])X[mm++]=X[i];
        }
        build(1,mm-1,1);
        double ans=0;
        for(int i=1; i<m-1; i++) {
            int l=lower_bound(X+1,X+mm,a[i].lx)-X;///找对应下标
            int r=lower_bound(X+1,X+mm,a[i].rx)-X;
            update(1,mm-1,1,l,r,a[i].flag);
            ans+=tree[1].len*(a[i+1].y-a[i].y);///ans+=X的覆盖长度*y增加高度
        }
        printf("Test case #%d\n",ca++);
        printf("Total explored area: %.2f\n\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 05:49:58

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

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(线段树&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 线段树+离散化+扫描线

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

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(线段树)

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

线段树+扫描线+离散化解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) 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