HDU-1542 Atlantis (线段树 求所有矩形面积和)

来源:互联网 发布:天天酷跑网络授权失败 编辑:程序博客网 时间:2024/05/25 23:58

Atlantis

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


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


#include <bits/stdc++.h>using namespace std;const int maxn = 101;struct Xpoint{double x, l, r;int v;bool operator < (const Xpoint& e){return x < e.x;}}line[maxn];struct tree{int cover;double len;}c[maxn << 4];double y[maxn << 2];void build(int o, int l, int r){c[o].cover = c[o].len = 0;if(l == r - 1) return;int mid = l + r >> 1;build(o << 1, l, mid);build(o << 1 | 1, mid, r);}void pushup(int o, int l, int r){if(c[o].cover > 0){c[o].len = y[r] - y[l]; return;}if(l == r - 1){c[o].len = 0; return;}c[o].len = c[o << 1].len + c[o << 1 | 1].len;}void add(int o, int l, int r, int L, int R, int v){if(l >= R || r <= L) return;if(l >= L && r <= R){c[o].cover += v;pushup(o, l, r);return;}int mid = l + r >> 1;add(o << 1, l, mid, L, R, v);add(o << 1 | 1, mid, r, L, R, v);pushup(o, l, r);}int main(){int n, casenum = 1;double x1, x2, y1, y2;while(scanf("%d", &n) != EOF){if(n == 0) return 0;int tot = 0;for(int i = 1; i <= n; ++i){scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);line[++tot].x = x1;line[tot].l = y1;line[tot].r = y2;line[tot].v = 1;y[tot] = y1;line[++tot].x = x2;line[tot].l = y1;line[tot].r = y2;line[tot].v = -1;y[tot] = y2;}build(1, 1, tot);sort(y + 1, y + 1 + tot);sort(line + 1, line + 1 + tot);double ans = 0;for(int i = 1; i < tot; ++i){y1 = lower_bound(y + 1, y + 1 + tot, line[i].l) - y;y2 = lower_bound(y + 1, y + 1 + tot, line[i].r) - y;add(1, 1, tot, y1, y2, line[i].v);//cout<<c[1].len<<endl;ans += c[1].len * (line[i + 1].x - line[i].x);}printf("Test case #%d\nTotal explored area: %.2lf\n\n", casenum++, ans);}}/*题意:100个矩形,坐标范围1e5,求所有矩形的总覆盖面积。思路:我们把y轴映射到线段树上,离散化一下,按照x的坐标从小到大依次处理所有矩形的平行于y轴的边。每一个矩形x坐标小的边表示后面这个区域是被覆盖的,直到遇到x大的边结束。那么我们对于所有矩形左边的边,我们讲其加到线段树的区间上,这样每处理一条边,区间上被覆盖的区间长度可以计算出来,即这一段y轴的区域是被覆盖的,再利用x算一下这一段的面积。一直处理完所有面积即可。*/


阅读全文
0 0
原创粉丝点击