HDU 1542

扫描线 + 线段树, 线段树写的有点儿退化,随便了- -。。。

  1 /*
  2 ID:esxgx1
  3 LANG:C++
  4 PROG:hdu1542
  5 */
  6 #include <cstdio>
  7 #include <cstring>
  8 #include <deque>
  9 #include <algorithm>
 10 using namespace std;
 11 
 12 #define eps        1e-5
 13 #define NN    1007
 14 
 15 struct rt{
 16     double x, y1, y2;
 17     int val;
 18 };
 19 rt rr[NN << 1];
 20 double mapx[NN << 1], mapy[NN << 1];
 21 
 22 
 23 #define MAXN    (NN << 1)
 24 
 25 int cnt[MAXN * 4];
 26 double sum[MAXN * 4];
 27 
 28 #define recursive_def    int l, int r, int i
 29 #define lsi            i<<1
 30 #define rsi            i<<1 | 1
 31 #define lsn            l, m, lsi
 32 #define rsn            m, r, rsi
 33 
 34 #define pushup        sum[i] = sum[lsi] + sum[rsi];
 35 
 36 void build(recursive_def)
 37 {
 38     cnt[i] = 0;
 39     sum[i] = 0;
 40     if (l == r) return;
 41     int m = (l+r) >> 1;
 42     build(lsn);
 43     if (l != m) build(rsn);
 44 }
 45 
 46 void update(int L, int R, int val, recursive_def)
 47 {
 48     if (l + 1 == r) {
 49         if (L <= l && r <= R) {
 50             if (cnt[i] * (cnt[i] + val)<=0)
 51                 sum[i] += val * (mapy[r] - mapy[l]);
 52             cnt[i] += val;
 53 //            printf("%f %f (%f)
", mapy[l], mapy[r], sum[i]);
 54         }
 55         return;
 56     }
 57     int m = (l+r) >> 1;
 58     if (L <= m) update(L, R, val, lsn);
 59     if (m <= R && l != m) update(L, R, val, rsn);
 60     pushup
 61 //    printf("%f %f (%f)
", mapy[l], mapy[r], sum[i]);
 62 }
 63 
 64 int cmp1(const rt &a, const rt &b)
 65 {
 66     return a.x < b.x;
 67 }
 68 
 69 int main(void)
 70 {
 71     #ifndef ONLINE_JUDGE
 72     freopen("in.txt", "r", stdin);
 73     #endif
 74 
 75     int Case = 1;
 76     int n;
 77     while(scanf("%d", &n), n) {
 78             int ri, px, py;
 79             ri = px = py = 0;
 80             for(int i=0; i<n; ++i) {
 81                     double x1, y1, x2, y2;
 82                     scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
 83                     rr[ri].x = x1, rr[ri].y1 = y1, rr[ri].y2 = y2;
 84                     rr[ri].val = 1; ++ri;
 85                     rr[ri].x = x2, rr[ri].y1 = y1, rr[ri].y2 = y2;
 86                     rr[ri].val = -1; ++ri;
 87                     mapx[px++] = x1; mapx[px++] = x2;
 88                     mapy[py++] = y1; mapy[py++] = y2;
 89             }
 90 
 91             sort(rr, rr + ri, cmp1);
 92             sort(mapx, mapx + px); px = unique(mapx, mapx + px) - mapx;
 93             sort(mapy, mapy + py); py = unique(mapy, mapy + py) - mapy;
 94             int j;
 95             double lx, S;
 96             lx = S = 0, j = 0;
 97             build(0, py - 1, 1);
 98             for(int i=0; i<ri; ++i) {
 99                 while ((mapx[j] - rr[i].x) <= eps) ++j;
100                 S += (rr[i].x - lx) * sum[1];
101 //                printf("%d %f %f
", i, sum[1], (rr[i].x - lx) * sum[1]);
102                 lx = rr[i].x;
103                 int y1 = lower_bound(mapy, mapy + py, rr[i].y1) - mapy;
104                 int y2 = lower_bound(mapy, mapy + py, rr[i].y2) - mapy;
105 //                printf("->%d %d %d
", y1, y2, rr[i].val);
106                 update(y1, y2, rr[i].val, 0, py - 1, 1);
107             }
108             printf("Test case #%d
Total explored area: %.2f

", Case, S);
109             ++Case;
110     }
111     return 0;
112 }
2014-07-28 17:39:31 Accepted 1542 0MS 324K 2503 B G++
原文地址:https://www.cnblogs.com/e0e1e/p/hdu_1542.html