Atlantis HDU - 1542 (线段树扫描线)

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. 

InputThe 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.OutputFor 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 

这题也是模板提

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <ctype.h>
 6 #include <set>
 7 #include <map>
 8 #include <queue>
 9 #include <stack>
10 #include <iostream>
11 using namespace std;
12 #define bug printf("******\n");
13 #define rtl  rt<<1
14 #define rtr  rt<<1|1
15 typedef long long LL;
16 const int maxn = 1e5 + 10;
17 struct LINE {
18     double x, y1, y2;
19     int flag;
20 } line[205];
21 int cmp(LINE a, LINE b) {
22     return a.x < b.x;
23 }
24 struct node {
25     double pre, l, r;
26     int cover, flag;
27 } tree[1210];
28 double y[205];
29 void build(int rt, int l, int r) {
30     tree[rt].l = y[l], tree[rt].r = y[r];
31     tree[rt].flag = -1, tree[rt].cover = 0, tree[rt].pre = -1;
32     if (l + 1 == r) {
33         tree[rt].flag = 1;
34         return ;
35     }
36     int m = (l + r) >> 1;
37     build(rtl, l, m);
38     build(rtr, m, r);
39 }
40 double query(int rt, double x, double y1, double y2, int flag) {
41     if (tree[rt].r <= y1 || tree[rt].l >= y2) return 0;
42     if (tree[rt].flag == 1) {
43         if (tree[rt].cover > 0) {
44             double pre = tree[rt].pre;
45             double ans = (x - pre) * (tree[rt].r - tree[rt].l);
46             tree[rt].pre = x;
47             tree[rt].cover += flag;
48             return ans;
49         } else {
50             tree[rt].cover += flag;
51             tree[rt].pre = x;
52             return 0;
53         }
54     }
55     return query(rtl, x, y1, y2, flag) + query(rtr, x, y1, y2, flag);
56 }
57 int main() {
58     int cas = 1, n;
59     while(scanf("%d", &n), n) {
60         int cnt = -1;
61         for (int i = 0 ; i < n ; i++) {
62             double x1, y1, x2, y2;
63             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
64             y[++cnt] = y1;
65             line[cnt].flag = 1;
66             line[cnt].x = x1;
67             line[cnt].y1 = y1;
68             line[cnt].y2 = y2;
69             y[++cnt] = y2;
70             line[cnt].flag = -1;
71             line[cnt].x = x2;
72             line[cnt].y1 = y1;
73             line[cnt].y2 = y2;
74         }
75         sort(y, y + cnt + 1);
76         sort(line, line + cnt + 1, cmp);
77         build(1, 0, cnt);
78         double ans = 0;
79         for (int i = 0 ; i <= cnt ; i++ )
80             ans += query(1, line[i].x, line[i].y1, line[i].y2, line[i].flag);
81         printf("Test case #%d\n", cas++);
82         printf("Total explored area: %.2lf\n\n", ans);
83     }
84     return 0;
85 }

猜你喜欢

转载自www.cnblogs.com/qldabiaoge/p/9388143.html
今日推荐