码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-16 23:32:47      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:blank   处理   single   gre   相对   有一个   sample   after   while   

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;
}

 



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

标签:blank   处理   single   gre   相对   有一个   sample   after   while   

原文地址:http://www.cnblogs.com/ccut-ry/p/7679115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!