poj 1151 Atlantis(矩形面积并,线段树+离散化)

本文介绍了一种计算多个矩形并集面积的算法,通过线段树和扫描线方法实现,适用于解决地图区域叠加的问题。

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

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16282 Accepted: 6202

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) 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 
 
 
题意:输入n个矩形的左上角坐标和右下角坐标,求所有矩形的面积和。
思路:线段树求矩形面积并详解http://www.cnblogs.com/fenshen371/p/3214092.html,我这里采用从左往右扫的方法。
 
 
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
#define eps 1e-6;
using namespace std;

const int INF = INF;
const int maxn = 1005;

struct node{
    int l, r;
    int c;
    double cnt, lf, rf;
}tree[maxn * 4];
struct Line{
    double x, y1, y2;
    int f;
}line[maxn];
double y[maxn];
int n;
bool cmp(Line a, Line b){
    return a.x < b.x;
}
void build(int l, int r, int rt){
    tree[rt].l = l, tree[rt].r = r;
    tree[rt].cnt = tree[rt].c = 0;
    tree[rt].lf = y[l];
    tree[rt].rf = y[r];
    if(l + 1 == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, L(rt));
    build(mid, r, R(rt));
}
void cal(int rt){
    if(tree[rt].c > 0)
    {
        tree[rt].cnt = tree[rt].rf - tree[rt].lf;
        return;
    }
    if(tree[rt].l + 1 == tree[rt].r) tree[rt].cnt = 0;
    else tree[rt].cnt = tree[L(rt)].cnt + tree[R(rt)].cnt;
}
void update(double lf, double rf, int rt, int f){
    if(lf == tree[rt].lf && rf == tree[rt].rf)
    {
        tree[rt].c += f;
        cal(rt);
        return;
    }
    if(rf <= tree[L(rt)].rf) update(lf, rf, L(rt), f);
    else if(lf >= tree[R(rt)].lf) update(lf, rf, R(rt), f);
    else
    {
        update(lf, tree[L(rt)].rf, L(rt), f);
        update(tree[R(rt)].lf, rf, R(rt), f);
    }
    cal(rt);
}
int main()
{
    int ca = 0;
    double x1, y1, x2, y2;
    while(scanf("%d", &n), n)
    {
        int cnt = 1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            line[cnt].x = x1;
            line[cnt].y1 = y1;
            line[cnt].y2 = y2;
            line[cnt].f = 1;
            y[cnt++] = y1;
            line[cnt].x = x2;
            line[cnt].y1 = y1;
            line[cnt].y2 = y2;
            line[cnt].f = -1;
            y[cnt++] = y2;
        }
        sort(line + 1, line + cnt, cmp);
        sort(y + 1, y + cnt);
        build(1, cnt - 1, 1);
        update(line[1].y1, line[1].y2, 1, line[1].f);
        double ans = 0;
        for(int i = 2; i < cnt; i++)
        {
            ans += tree[1].cnt * (line[i].x - line[i - 1].x);
            update(line[i].y1, line[i].y2, 1, line[i].f);
        }
        printf("Test case #%d\n", ++ca);
        printf("Total explored area: %.2f\n\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值