Atlantis
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
Source
Mid-Central European Regional Contest 2000
题目类型:离散化+线段树+扫描线
算法分析:首先将浮点型的坐标离散化成整型,然后从小到上扫描,如果扫到的是一个矩形的下边,则将该边投影到整个区间上,计算出整个区间上总的线段长度,然后用当前的线段的高度和下一个线段的高度做差并相乘。注意由于数据是经过离散化过的,所以对于a[mid]和a[mid+1]之间的长度需要特别处理!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 6000; struct Node { double l, r, h; int id; }; Node a[maxn]; int len, hhlen; double sum[maxn<<2], Hash[maxn<<2]; int lazy[maxn<<2]; bool cmp (Node aa, Node bb) { return aa.h < bb.h; } void PushUp (int rt, int l, int r) { if (lazy[rt]) sum[rt] = Hash[r+1] - Hash[l]; else if (l == r) sum[rt] = 0; else sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void UpDate (int rt, int l, int r, int L, int R, int id) { if (L <= l && r <= R) { lazy[rt] += id; PushUp (rt, l, r); return ; } int m = (l + r) >> 1; if (L <= m) UpDate (lson, L, R, id); if (R > m) UpDate (rson, L, R, id); PushUp (rt, l, r); } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int flag = 1, n; while (cin >> n) { if (n == 0) break; cout << "Test case #" << flag++ << endl; len = hhlen = 0; double x1, y1, x2, y2; for (int i = 1; i <= n; i++) { cin >> x1 >> y1 >> x2 >> y2; a[len].l = x1, a[len].r = x2, a[len].h =y2, a[len++].id = -1; a[len].l = x1, a[len].r = x2, a[len].h = y1, a[len++].id = 1; Hash[hhlen++] = x1, Hash[hhlen++] = x2; } sort (a, a + len, cmp); sort (Hash, Hash + hhlen); hhlen = unique (Hash, Hash + hhlen) - Hash; memset (sum, 0, sizeof (sum)); memset (lazy, 0, sizeof (lazy)); double res = 0; for (int i = 0; i < len - 1; i++) { // cout << a[i].l << " " << a[i].r << " " <<a[i].h<< " " << a[i].id << endl; // cout << res << endl; int sx = lower_bound (Hash, Hash + hhlen, a[i].l) - Hash; int sy = lower_bound (Hash, Hash + hhlen, a[i].r) - Hash - 1; if (sx <= sy) UpDate (1, 0, hhlen - 1, sx, sy, a[i].id); res += sum[1] * (a[i+1].h - a[i].h); } cout << "Total explored area: " << fixed << setprecision(2) << res << endl << endl; } return 0; } |
- « 上一篇:poj1149
- poj1157:下一篇 »