线段树 - 矩形的面积并 (扫描线)

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.

InputThe 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.OutputFor 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://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html
       : http://blog.csdn.net/qq_18661257/article/details/47622677

  这题的解法感觉好神奇 , 又学到了一手 。这题要用到扫描线的理念去解题 , 即一根平行于 X 轴或平行于 Y 轴的直线去扫描这个图形,并且给矩形的上下边做上不同的标记,没扫描到一条边时计算一次其最近的的上方的面积。


  这个题目还有一个问题需要注意, 就是线段树所处理的区间是 [1, a] [a+1, b] [b+1, c] [c+1, d] ,但是这样算相对于此类问题是有缺陷的 , 因为你这样计算后 [a, a+1] 这个区间的长度被你无形中抹去了,造成区间的缺失 , 要怎么去解决呢 ?
在此处可以借助区间的性质 , [ ) , 左闭右开 。 例如你在计算 区间 2 到 4 ,你传过去的区间的参数只需要是 2 到 3 , 然后计算 2 - 2 区间时 , 是 2 - 3 的值, 计算 3 - 3 区间时 , 是 3 - 4 的值。

代码示例 :
/*
 * Author:  ry 
 * Created Time:  2017/10/16 18:06:48
 * File Name: 3.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e3+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

struct seg
{
    double l, r, h;
    int pt;
}po[eps];
double pre[eps];

bool cmp(seg a, seg b){
    return a.h < b.h;
}

struct node
{
    int l, r, f;
    double len;
}tree[eps];

void build(int l, int r, int k){
    tree[k].l = l;
    tree[k].r = r;
    tree[k].f = 0, tree[k].len = 0;
    if (l == r) return;
    int m = (l + r) >> 1;
    build(l, m, k<<1);
    build(m+1, r, k<<1|1);
}   

void down(int k){
    if (tree[k].f) {
        tree[k].len = pre[tree[k].r+1] - pre[tree[k].l];
    }
    else if (tree[k].l == tree[k].r) tree[k].len = 0;
    else {
        tree[k].len = tree[k<<1].len + tree[k<<1|1].len;
    }
}

void update(int l, int r, int k, int pt){
    if (l <= tree[k].l && tree[k].r <= r){
        tree[k].f += pt;
        down(k);
        return;
    }
    int m = (tree[k].l + tree[k].r) >> 1;
    if (l <= m) update(l, r, k<<1, pt);
    if (r > m) update(l, r, k<<1|1, pt);
    down(k);
}

int main() {
    int n;
    double a, b, c, d;
    
    int yy = 1;
    while (~scanf("%d", &n) && n){
        int k = 1;
        for(int i = 1; i <= n; i++){
            scanf("%lf%lf%lf%lf", &a, &b, &c, &d);        
            po[k].l = po[k+1].l = a;
            po[k].r = po[k+1].r = c;
            po[k].h = b, po[k+1].h = d;
            po[k].pt = 1, po[k+1].pt = -1;
            pre[k] = a, pre[k+1] = c;
            k += 2;
        }
        sort(pre+1, pre+k);
        sort(po+1, po+k, cmp);
        int t = 2;   // 区间去重,这个地方写一定要注意下 
        for(int i = 2; i < k; i++){
            if (pre[i] != pre[i-1]) pre[t++] = pre[i]; 
        } 
        build(1, t-1, 1);
        double ans = 0;
        for(int i = 1; i < k-1; i++){
            int l = lower_bound(pre+1, pre+t, po[i].l) - pre;
            int r = lower_bound(pre+1, pre+t, po[i].r) - pre - 1;    
            update(l, r, 1, po[i].pt);
            ans += tree[1].len * (po[i+1].h - po[i].h); 
        }
        printf("Test case #%d\n", yy++);
        printf("Total explored area: %.2f\n\n", ans);
       
    }

    return 0;
}

 



转载于:https://www.cnblogs.com/ccut-ry/p/7679115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bentley-Ottmann算法和扫描线算法都是计算机科学领域中的几何算法,主要用于解决平面上的几何问题。 Bentley-Ottmann算法是一种用于解线段交点问题的算法。它使用了扫描线的概念,通过扫描线从上到下遍历平面上的线段,并将其投影到水平的事件点序列上。在扫描线的过程中,通过维护一个有序的事件点集合以及一个有序的线段交点集合来找到所有的线段交点。该算法的时间复杂度为O((n+k) log n),其中n为线段的数量,k为交点的数量。 扫描线算法是一种通过扫描线的方式来解决一些几何问题的算法。其基本思想是将平面划分为许多水平的扫描线,并在每条扫描线上进行计算。算法从上到下按扫描线依次处理每个图形对象,记录下与当前扫描线相交的图形边界,并根据需要更新一些数据结构来保存相关信息。在处理完所有图形对象后,可以得到所需要的结果。扫描线算法主要应用于计算几何、计算机图形学等领域中的问题,例如解多边形交集、寻找包含某一点的图形等。由于其简洁高效的特点,扫描线算法在计算机图形学中的应用非常广泛。 综上所述,Bentley-Ottmann算法和扫描线算法都是用于解决平面上几何问题的算法。Bentley-Ottmann算法主要用于解线段交点问题,而扫描线算法适用于处理一些特定的几何问题。这两种算法都是在计算几何和计算机图形学等领域中非常有用的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值