HDU-1542 Atlantis 线段树+扫描线

Posted R o b i n

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-1542 Atlantis 线段树+扫描线相关的知识,希望对你有一定的参考价值。

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
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<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 using namespace std;
  6 #define INF 0x3f3f3f3f
  7 #define M(a, b) memset(a, b, sizeof(a))
  8 #define lson o<<1
  9 #define rson o<<1|1
 10 const int N = 200 + 5;
 11 int cnt[N<<2], qL, qR, v;
 12 double X[N], sum[N<<2];
 13 struct Segment {
 14     double l, r, h;
 15     int f;
 16     Segment() {}
 17     Segment(double l, double r, double h, int f): l(l), r(r), h(h), f(f) {} 
 18     bool operator < (const Segment &rhs) const {
 19         return h < rhs.h;
 20     }
 21 }node[N];
 22 
 23 void pushdown(int o, int L, int R) {
 24     if (~cnt[o]) {
 25         int M = (L + R) >> 1;
 26         cnt[lson] = cnt[rson] = cnt[o];
 27         sum[lson] = cnt[lson] ? X[M+1]-X[L] : 0;
 28         sum[rson] = cnt[rson] ? X[R+1]-X[M+1] : 0;
 29     }
 30 }  
 31 
 32 void pushup(int o, int L, int R) {
 33     if (~cnt[lson] || ~cnt[rson]) cnt[o] = -1;
 34     else if (cnt[lson] != cnt[rson]) cnt[o] = -1;
 35     else {
 36         cnt[o] = cnt[lson];
 37     }
 38     sum[o] = sum[lson] + sum[rson];
 39 }
 40 
 41 void build(int o, int L, int R) {
 42     if (L == R) {cnt[o] = 0; sum[o] = 0;}
 43     else {
 44         int M = (L + R) >> 1;
 45         build(lson, L, M);
 46         build(rson, M+1, R);
 47         pushup(o, L, R);
 48     }
 49 }
 50 
 51 void update(int o, int L, int R) {
 52     if (qL <= L && R <= qR) {
 53         if (~cnt[o]) {
 54             cnt[o] += v;
 55             sum[o] = cnt[o] ? X[R+1]-X[L] : 0;
 56             return;
 57         }
 58     }
 59     int M = (L + R) >> 1;
 60     pushdown(o, L, R);
 61     if (qL <= M) update(lson, L, M);
 62     if (M < qR) update(rson, M+1, R);
 63     pushup(o, L, R);
 64 }
 65 
 66 int bin(int L, int R, double key) {
 67     while (L < R) {
 68         int M = (L + R) >> 1;
 69         if (X[M] == key) return M;
 70         else if (X[M] < key) L = M + 1;
 71         else R = M - 1; 
 72     }
 73     return L;
 74 }
 75 
 76 int main() {
 77     int T = 0, n;
 78     while (scanf("%d", &n), n) {
 79         double x1, x2, y1, y2;
 80         int num = 0, nn = 0;
 81         for (int i = 1; i <= n; ++i) {
 82             scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
 83             X[++num] = x1; X[++num] = x2;
 84             node[++nn] = Segment(x1, x2, y1, 1);
 85             node[++nn] = Segment(x1, x2, y2, -1);
 86         }
 87         sort(X+1, X+1+num); sort(node+1, node+1+nn);
 88         int k = 1;
 89         for (int i = 2; i <= nn; ++i)
 90             if (X[i] != X[i-1]) X[++k] = X[i];
 91         build(1, 1, k-1);
 92         double ans = 0;
 93         for (int i = 1; i < nn; ++i) {
 94             qL = bin(1, k, node[i].l);
 95             qR = bin(1, k, node[i].r)-1;
 96             v = node[i].f;
 97             if(qL <= qR) update(1, 1, k-1);
 98             ans += sum[1] * (node[i+1].h-node[i].h);
 99         }
100         printf("Test case #%d\n", ++T);
101         printf("Total explored area: %.2f\n\n", ans);
102     }
103 
104     return 0;
105 }

 

 

以上是关于HDU-1542 Atlantis 线段树+扫描线的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1542 Atlantis (线段树+扫描线)

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

HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

HDU - 1542 Atlantis[线段树+扫描线]

hdu1542 Atlantis (线段树+扫描线+离散化)

HDU 1542Atlantis 矩形面积并(线段树,扫描法)