HDU1542 线段树+离散化+扫描线

来源:互联网 发布:matlab矩阵做聚类分析 编辑:程序博客网 时间:2024/04/25 12:56

 


Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11171    Accepted Submission(s): 4734


Problem 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 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.
 

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
210 10 20 2015 15 25 25.50
 

Sample Output
Test case #1Total explored area: 180.00

题目大意:平面上若干个矩形,求这些矩形所覆盖的面积为多少。

解题思路:因为这些点都是小数,所以需要离散化。离散化的时候利用unique去除重复的元素。利用二分快速查找。


所谓的扫描就是一条线从最底下依次扫描每一条边。每遇到一条边,就向这个容器里面注水(也就是面积)。如图分别注了四次水。每次的注水量就是底边乘以高度。高度是这一条边和下一条边的高度差。而对于底边长用线段树维护就可以了。每个举行有两条边,遇到下底边增加增加标记,遇到上底边减少标记。那些地方有标记那就计算这段长度。


#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define rson (id << 1) | 1#define lson (id << 1)#define mid ((l + r) >> 1)using namespace std;const int maxn = 40000;int n, m;double ha[maxn], sum[maxn];int mark[maxn];struct seg {    double l, r, h;    int d;    seg() {};    seg(double x, double y, double z, int a):l(x), r(y), h(z), d(a) {}    bool operator < (seg & a) const {        return h < a.h;    }} g[maxn];void push_up(int l, int r, int id) {    if(mark[id]) sum[id] = ha[r] - ha[l - 1];    else if(l == r) sum[id] = 0;    else sum[id] = sum[rson] + sum[lson];}void ins(int id, int l, int r, int ql, int qr, int tt) {    if(ql <= l && r <= qr) {        mark[id] += tt;        push_up(l, r, id);        return ;    }    if(ql <= mid) ins(lson, l, mid, ql, qr, tt);    if(qr >= mid + 1) ins(rson, mid + 1, r, ql, qr, tt);    push_up(l, r, id);}int main(){    double x1, x2, y1, y2, ans, l, r;    int t = 1;    while(scanf("%d", &n) != EOF && n) {        memset(mark, 0, sizeof(mark));        memset(sum, 0, sizeof(sum));        m = 0;        for(int i = 1; i <= n; ++i) {            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            g[m] = seg(x1, x2, y1, 1);            ha[m++] = x1;            g[m] = seg(x1, x2, y2, -1);            ha[m++] = x2;        }        sort(g, g + m);        sort(ha, ha + m);        m = unique(ha, ha + m) - ha;        ans = 0;        for(int i = 0; i < 2 * n - 1; ++i) {            l = lower_bound(ha, ha + m, g[i].l) - ha + 1;            r = lower_bound(ha, ha + m, g[i].r) - ha;            ins(1, 1, 2 * n, l, r, g[i].d);            ans += sum[1] * (g[i + 1].h - g[i].h);        }        printf("Test case #%d\nTotal explored area: %.2lf\n\n",t++,ans);    }    return 0;}


0 0
原创粉丝点击