POJ 1151Atlantis 扫描线+线段树求矩形面积并

题目链接

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <map>
 8 #include <set>
 9 #include <string>
10 #include <queue>
11 #include <stack>
12 #include <bitset>
13 using namespace std;
14 #define pb(x) push_back(x)
15 #define ll long long
16 #define mk(x, y) make_pair(x, y)
17 #define lson l, m, rt<<1
18 #define mem(a) memset(a, 0, sizeof(a))
19 #define rson m+1, r, rt<<1|1
20 #define mem1(a) memset(a, -1, sizeof(a))
21 #define mem2(a) memset(a, 0x3f, sizeof(a))
22 #define rep(i, n, a) for(int i = a; i<n; i++)
23 #define fi first
24 #define se second
25 typedef pair<int, int> pll;
26 const double PI = acos(-1.0);
27 const double eps = 1e-8;
28 const int mod = 1e9+7;
29 const int inf = 1061109567;
30 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
31 const int maxn = 500;
32 struct segment
33 {
34     double l, r, h;
35     int flag;
36     segment(){}
37     segment(double l, double r, double h, int flag):l(l), r(r), h(h), flag(flag){}
38     bool operator < (segment a) const
39     {
40         return h<a.h;
41     }
42 }line[maxn];
43 double a[maxn], sum[maxn<<2];
44 int cover[maxn<<2];
45 void pushUp(int rt, int l, int r) {
46     if(cover[rt]) {
47         sum[rt] = a[r+1]-a[l];
48     } else if(l == r) {
49         sum[rt] = 0;
50     } else {
51         sum[rt] = sum[rt<<1]+sum[rt<<1|1];
52     }
53 }
54 void update(int L, int R, int l, int r, int rt, int flag) {
55     if(L<=l&&R>=r) {
56         cover[rt] += flag;
57         pushUp(rt, l-1, r-1);
58         return ;
59     }
60     int m = l+r>>1;
61     if(L<=m)
62         update(L, R, lson, flag);
63     if(R>m)
64         update(L, R, rson, flag);
65     pushUp(rt, l-1, r-1);
66 }
67 int main()
68 {
69     int n, cnt, k = 1;
70     double x1, y1, x2, y2;
71     while(scanf("%d", &n)&&n) {
72         cnt = 0;
73         mem(cover);
74         for(int i = 0; i<n; i++) {
75             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
76             line[cnt] = segment(x1, x2, y1, 1);
77             a[cnt++] = x1;
78             line[cnt] = segment(x1, x2, y2, -1);
79             a[cnt++] = x2;
80         }
81         sort(a, a+cnt);
82         sort(line, line+cnt);
83         double ans = 0;
84         int num = unique(a, a+cnt)-a;
85         for(int i = 0; i<cnt; i++) {
86             int L = lower_bound(a, a+num, line[i].l)-a+1;
87             int R = lower_bound(a, a+num, line[i].r)-a;
88             update(L, R, 1, num, 1, line[i].flag);
89             ans += sum[1]*(line[i+1].h-line[i].h);
90         }
91         printf("Test case #%d
", k++);
92         printf("Total explored area: %.2f

", ans);
93     }
94     return 0;
95 }
原文地址:https://www.cnblogs.com/yohaha/p/5081641.html