ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj_1151

2019-05-02 14:55:20  阅读:222  来源: 互联网

标签:case cnt int double 1151 mid len poj


 

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\nTotal explored area: %.2lf\n\n", ++count, ans);
    }
    
    
    
    
//    fclose(stdin);
    return 0;
}

 

标签:case,cnt,int,double,1151,mid,len,poj
来源: https://www.cnblogs.com/phaLQ/p/10802293.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有