POJ 1151 Atlantis( 线段树 + 扫描线 )

一维离散化, 扫描线扫另一维, 用线段树维护

POJ建议交C++...G++貌似double要用%f ? 反正同一份代码C++AC,G++WA

-------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 109;
 
struct Line {
double p, l, r;
int t;
Line(double _p = 0, double _l = 0, double _r = 0, int _t = 1) :
p(_p), l(_l), r(_r), t(_t) {
}
bool operator < (const Line &t) const {
return p < t.p;
}
} L[maxn << 1];
 
struct Node {
Node *l, *r;
int cnt;
double sum;
void upd(double v) {
if(cnt) {
sum = v;
} else {
sum = 0;
if(l) sum += l->sum;
if(r) sum += r->sum;
}
}
} pool[maxn << 2], *pt, *Root;
 
Node* newNode() {
pt->l = pt->r = NULL;
pt->sum = 0;
pt->cnt = 0;
return pt++;
}
 
void Build(Node* t, int l, int r) {
int m = (l + r) >> 1;
if(l + 1 == r) return;
if(m > l) Build(t->l = newNode(), l, m);
if(r > m) Build(t->r = newNode(), m, r);
}
 
int _l, _r, _v;
int N, n;
double pos[maxn << 1];
 
void Modify(Node* t, int l, int r) {
if(_l <= l && r <= _r) {
t->cnt += _v;
} else {
int m = (l + r) >> 1;
if(_l < m) Modify(t->l, l, m);
if(m < _r) Modify(t->r, m, r);
}
t->upd(pos[r - 1] - pos[l - 1]);
}
 
void Change(int l, int r, int v) {
_l = l; _r = r; _v = v;
Modify(Root, 1, n);
}
 
void Init() {
pt = pool;
n = 0;
for(int i = 0; i < N; i++) {
double x0, y0, x1, y1;
scanf("%lf%lf%lf%lf", &x0, &y0, &x1, &y1);
L[n] = Line(x0, y0, y1, 1); pos[n++] = y0;
L[n] = Line(x1, y0, y1, -1); pos[n++] = y1;
}
sort(L, L + n);
sort(pos, pos + n);
Build(Root = newNode(), 1, n);
}
 
int H(double t) {
return lower_bound(pos, pos + n, t) - pos + 1;
}
 
int main() {
int T = 0;
while(scanf("%d", &N) == 1 && N) {
Init();
double ans = 0;
Change(H(L[0].l) , H(L[0].r), L[0].t);
for(int i = 1; i < n; i++) {
ans += (L[i].p - L[i - 1].p) * Root->sum;
Change(H(L[i].l), H(L[i].r), L[i].t);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++T, ans);
}
return 0;
}

------------------------------------------- 

Atlantis
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 19566Accepted: 7431

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

 

posted @ 2015-11-25 19:37  JSZX11556  阅读(194)  评论(0编辑  收藏  举报