[POJ 1151] Atlantis【扫描线+离散化+线段树】

• 2018-01-08
• 0
• 0

Problem:

 Time Limit: 1000MS Memory Limit: 10000K

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 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

Solution:

• [l, r] 在线段树上被分成 [l, m][m + 1, r]，导致 (m, m + 1) 的长度丢失。

Plus: 由于数据范围较小，这道题实际上可以直接将两个维度都离散化直接统计每一块是否被覆盖。。但这样复杂度就是 O(Tn2) 了。

Code: O(Tnlogn) [180K, 16MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define MAX2N 202
#define eps 1e-8
using namespace std;

inline int dsgn(const double &x){
if(fabs(x) <= eps) return 0;
return x < 0 ? -1 : 1;
}

int T = 0, n, topd;

struct _double{
double val;

_double() {}
_double(double val): val(val) {}

inline bool operator < (const _double &d2) const {return dsgn(val - d2.val) < 0;}

inline bool operator == (const _double &d2) const {return dsgn(val - d2.val) == 0;}
};  // Overload operator == for double comparison

_double disc[MAX2N];  // Discretization of the x-axis coordinate

struct Segment{
double _l, _r, h;
int l, r, v;

inline bool operator < (const Segment &seg2) const {return h < seg2.h;}
} seg[MAX2N];

struct Node{
int cov;
double sum;
};

struct Segment_Tree{
#define MAXNODE (MAX2N << 2)
#define lch (u << 1)
#define rch (u << 1 | 1)
#define smid (l + r >> 1)
Node node[MAXNODE];

inline void clear() {memset(node, 0, sizeof(node));}

inline void update(int u, int l, int r){
if(node[u].cov) node[u].sum = disc[r + 1].val - disc[l].val;  // To avoid gap missing
else if(l == r) node[u].sum = 0;
else node[u].sum = node[lch].sum + node[rch].sum;
}

inline void add(int u, int l, int r, int al, int ar, int v){
if(al <= l && r <= ar){
node[u].cov += v;
update(u, l, r);
return;
}
if(al <= smid) add(lch, l, smid, al, ar, v);
if(smid < ar) add(rch, smid + 1, r, al, ar, v);
update(u, l, r);
}
} sgt;

int main(){
while(scanf("%d", &n) != EOF && n){
for(register int i = 1; i <= n; i++){
double lx, ly, rx, ry;
scanf("%lf%lf%lf%lf", &lx, &ly, &rx, &ry);

Segment &uped = seg[(i << 1) - 1], &dwned = seg[i << 1];
uped.h = lx, uped._l = ly, uped._r = ry, uped.v = 1;
dwned.h = rx, dwned._l = ly, dwned._r = ry, dwned.v = -1;

disc[(i << 1) - 1].val = ly, disc[i << 1].val = ry;
}
n <<= 1, sort(disc + 1, disc + n + 1);
topd = unique(disc + 1, disc + n + 1) - disc - 1;
for(register int i = 1; i <= n; i += 2){
seg[i].l = seg[i + 1].l = lower_bound(disc + 1, disc + topd + 1, _double(seg[i]._l)) - disc;
seg[i].r = seg[i + 1].r = lower_bound(disc + 1, disc + topd + 1, _double(seg[i]._r)) - disc;
}
// Discretize the x-axis coordinate
sort(seg + 1, seg + n + 1);
double area = 0; sgt.clear();
for(register int i = 1; i < n; i++){
sgt.add(1, 1, topd, seg[i].l, seg[i].r - 1, seg[i].v);  // Upper bound decrease 1 for the right-extra interval !!!
area += (seg[i + 1].h - seg[i].h) * sgt.node[1].sum;
}
printf("Test case #%d\n", ++T);
printf("Total explored area: %.2f\n\n", area);  // Beware of the presentation !!!
}
return 0;
}
```

评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai