【POJ 1151】 Atlantis(线段树,扫描线,离散化)

本文介绍了一种解决地图重叠区域计算问题的高效算法,通过离散化和扫描线技术,结合线段树数据结构,实现了O(nlogn)的时间复杂度,避免了O(n^3)的暴力解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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 consists of several test cases. Each test case starts with a line containing a single integer n ( 1 < = n < = 100 ) n (1 <= n <= 100) n(1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x 1 , y 1 , x 2 , y 2 ( 0 < = x 1 < x 2 < = 100000 ; 0 < = y 1 < y 2 < = 100000 ) x1,y1,x2,y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000) x1,y1,x2,y2(0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values ( x 1 , y 1 ) (x1, y1) (x1,y1) and ( x 2 , y 2 ) (x2,y2) (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 0 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 k k is the number of the test case (starting with 1 1 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

题目传送门
题目传送门(vjudge)

思路

线段树 + + + 扫描线 + + + 离散化。
如果这 n n n 个矩形的坐标都是整数,那么……:直接暴力打标记!
但是不是这样的。

坐标大小很大,而且是实数,于是我们想到离散化,然后……
我们继续BF,打标记
离散化玩暴力打标记的时间复杂度为 O ( n 3 ) O(n^{3}) O(n3),无法接受 (其实完全可以接受)
我们要的是正解,正解!!!

于是,map 扫描线就来了。
首先,我们对纵坐标离散化。
然后,每个纵向边作为扫描线,从左往右扫,当前扫描线和下一个扫描线的距离 × \times ×已覆盖长度就是一部分的面积,最后累加起来。
具体怎么实现呢?
线段树大法好!
只需要用一颗线段树维护即可。
时间复杂度 O ( n l o g n ) O(n log n) O(nlogn)

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;
double y[10010];
int cnt, cas, n;
struct node {
    double x;
    double y1, y2;
    bool lft;

    bool operator < (const node& e) const {
        return x < e.x;
    }
} a[10010];
struct Node {
    int L, R, cv;
    double len;
} tr[1010010];

int to(double x) {
    return lower_bound(y, cnt+y, x)-y;
}

void mlen(int k) {
    if (tr[k].cv > 0) tr[k].len = y[tr[k].R] - y[tr[k].L];
    else if (tr[k].L + 1 == tr[k].R) tr[k].len = 0;
    else tr[k].len = tr[k*2].len + tr[k*2+1].len;
}

void build(int k, int L, int R) {
    tr[k].L = L; tr[k].R = R;
    tr[k].len = tr[k].cv = 0;
    if (L + 1 == R) return;
    int mid = (L + R) >> 1;
    build(k*2, L, mid);
    build(k*2+1, mid, R);
}

void insert(int k, int ql, int qr) {
    if (ql <= tr[k].L && tr[k].R <= qr) {
        tr[k].cv++;
        mlen(k);
        return;
    }
    int mid = (tr[k].L+tr[k].R) >> 1;
    if (ql < mid) insert(k*2, ql, qr);
    if (qr > mid) insert(k*2+1, ql, qr);
    mlen(k);
}

void del(int k, int ql, int qr) {
    if (ql <= tr[k].L && tr[k].R <= qr) {
        tr[k].cv--;
        mlen(k);
        return;
    }
    int M = (tr[k].L+tr[k].R)  >>  1;
    if (ql < M) del(k*2, ql, qr);
    if (qr > M) del(k*2+1, ql, qr);
    mlen(k);
}

int main() {
    double x1, y1, x2, y2;
    while (~scanf("%d", &n)) {
    	if (n == 0) return 0;
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            a[i*2].x = x1; a[i*2].y1 = y1; a[i*2].y2 = y2; a[i*2].lft = true;
            a[(i*2)+1].x = x2; a[(i*2)+1].y1 = y1; a[(i*2)+1].y2 = y2; a[(i*2)+1].lft = false;
            y[i*2] = y1; y[(i*2)+1] = y2;
        }
        sort(y, y + 2*n);
        cnt = unique(y, y + 2*n) - y;
        sort(a, a + 2*n);
        build(1, 0, cnt-1);
        double ans = 0;
        for (int i = 0; i < 2*n-1; i++) {
            if (a[i].lft) insert(1, to(a[i].y1), to(a[i].y2));
            else del(1, to(a[i].y1), to(a[i].y2));
            ans += (a[i+1].x - a[i].x) * tr[1].len;
        }
        printf("Test case #%d\n", ++cas);
        printf("Total explored area: %.2f\n\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值