zoukankan      html  css  js  c++  java
  • poj_1151

     

    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 

    这道题是一道扫描线+线段树+二分+离散化 的题目
    思路:
    这道题目,我们用一个四元组来记录矩形的上下边界,用一个数组来记录每个矩形的左右两个横坐标,然后从高度从低到高队四元组进行排序,对数组从小到大进行排序,
    当扫描到一个矩形的下边界时,让整个区间的cnt++,当扫描到矩形的上边界时让整个横坐标区间的cnt--,这样当某一个区间的cnt>=1时,我们就可以使用线段树得到这个区间长度,
    这样不断的累加,最终就可以得到全部矩形的面积之和。
    四元组的实现方法:使用一个结构体;
    数组dis的意义:记录所有横坐标。
    离散化的意义:因为横坐标是double类型,并且可能由重复,所以需要离散化;
    离散化的方法:使用unique()函数;
    线段树的意义:线段树一般是用来做单点修改,区间查询;而这道题我们需要一次性更新一个区间的cnt,并且此处我并没有用到延迟标记,
    所以,此处线段树中一个节点的含义:这个节点所表示区间的中的len和cnt,所以就不用往下进行spread(因为每次对cnt进行++和--的区间一定是对称的,所以不用对左右子节点进行延迟更新)
    代码如下:
    #include<iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int maxn = 300;
    struct Line {
        double x1, x2, h;
        int f;
        void init(double xx1, double xx2, double hh, int ff) {
            x1 = xx1; x2 = xx2; h = hh; f = ff;
        }
    }line[4 * maxn];
    struct segmentTree {
        int l, r, f;
        double len;
    }t[maxn * 4];
    double dis[maxn * 4];
    
    inline bool cmp(Line a, Line b) {
        return a.h < b.h;
    }
    
    inline int find(double x, int l, int r) {
        while(l < r) {
            int mid = (l + r) >> 1;
            if(dis[mid] >= x) r = mid; else l = mid + 1;
        }
        
        return l;
    }
    
    inline void build(int s, int l, int r) {
        t[s].l = l; t[s].r = r; t[s].len = 0; t[s].f = 0;
        if(l == r) return;
        int mid = (l + r) / 2;
        build(s * 2, l, mid);
        build(s * 2 + 1, mid + 1, r);
    }
    
    inline void get_len(int s) {
        if(t[s].f >= 1) {
            t[s].len = dis[t[s].r + 1] - dis[t[s].l];
        }else {
            t[s].len = t[s * 2].len + t[s * 2 + 1].len;
        }
    }
    
    inline void change(int s, int l, int r, int f) {
        if(l <= t[s].l && r >= t[s].r) {
            t[s].f += f;
            get_len(s);
            return;
        }
        int mid = (t[s].l + t[s].r) / 2;
        if(l <= mid) change(s * 2, l, r, f);
        if(mid < r) change(s * 2 + 1, l, r, f);
        get_len(s);
    }
    
    int main(void) {
    //    freopen("in.txt", "r", stdin);
        register double x1, y1, x2, y2, ans;
        register int l, r, count = 0, n, dis_num, num;
        while(scanf("%d", &n) && n) {
            ans = 0; dis_num = 0; num = 0;
            for(int i = 1; i <= n; i++) {
                scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
                line[num].init(x1, x2, y1, 1);
                dis[num++] = x1;
                line[num].init(x1, x2, y2, -1);
                dis[num++] = x2;
            }
            sort(line, line + num, cmp);
            sort(dis, dis + num);
            dis_num = unique(dis, dis + num) - dis;
            build(1, 0, dis_num - 1);
            for(int i = 0; i < num - 1; i++) {
                l = find(line[i].x1, 0, dis_num - 1);
                r = find(line[i].x2, 0, dis_num - 1) - 1;
                change(1, l, r, line[i].f);
                ans = ans + t[1].len * (line[i + 1].h - line[i].h);
            }
            printf("Test case #%d
    Total explored area: %.2lf
    
    ", ++count, ans);
        }
        
        
        
        
    //    fclose(stdin);
        return 0;
    }
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/phaLQ/p/10802293.html
Copyright © 2011-2022 走看看