【POJ1151】【扫描线+线段树】Atlantis

时间:2022-05-15 03:19:01

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

【分析】
扫描线的基本应用,离散化一下按横坐标从左到右扫就行了。
 /*
元稹
《离思五首·其四》 曾经沧海难为水,除却巫山不是云。
取次花丛懒回顾,半缘修道半缘君。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
struct Line{//扫描线
int flag;//表示是出线还是入线
double x, y1, y2; Line (double a, double b, double c, double d){
flag = (int)d;
x = a;
y1 = b;
y2 = c;
}
bool operator < (const Line &b)const{
return x < b.x;
};
};
struct Seg_Tree{
struct Node{
int l, r, flag;
double ll, rr, len;
}tree[];
vector<double>y, data;
vector<Line>line;
map<double, int>Map;//离散化 void update(int t, int s, int e, int val){
int l = tree[t].l, r = tree[t].r;
if ((l + ) == r) {
tree[t].flag += val;
if (tree[t].flag == ) tree[t].len = ;
else tree[t].len = tree[t].rr - tree[t].ll;
}
else{
int mid = (l + r)>>;
if (s < mid) update(t << , s, e, val);
if (e > mid) update((t << ) | , s, e, val);
tree[t].len = tree[t << ].len + tree[(t << ) | ].len; }
}
void build(int t, int l, int r){
tree[t].l = l;
tree[t].r = r;
tree[t].flag = ;
tree[t].len = ;
//这两个是代表真实的值
tree[t].ll = y[l];
tree[t].rr = y[r];
if ((l + ) == r) return;
//左闭右开
int mid = (l + r)>>;
build((t<<), l, mid);
build((t<<) | , mid, r);
}
void init(){
Map.clear();;
line.clear();
y.clear();
data.clear();
}
}A;
int n; void init(){
A.init();
for (int i = ; i <= n; i++){
double x1, x2, y1, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
A.line.push_back(Line(x1, y1, y2, ));//1代表入线
A.line.push_back(Line(x2, y1, y2, -));
A.data.push_back(y1);
A.data.push_back(y2);
}
int cnt = ;//离散化
sort(A.data.begin(), A.data.end());
for (int i = ; i < A.data.size(); i++){
if (i == || A.data[i] != A.data[i - ]){
A.Map[A.data[i]] = cnt++;
A.y.push_back(A.data[i]);
}
}
}
void work(){
sort(A.line.begin(), A.line.end());
A.build(, , A.y.size() - );
double Ans = ;
for (int i = ; i < A.line.size(); i++){
if (i != ) Ans += (A.line[i].x - A.line[i - ].x) * A.tree[].len;
A.update(, A.Map[A.line[i].y1], A.Map[A.line[i].y2], A.line[i].flag);
}
printf("Total explored area: %.2lf\n\n" , Ans);
} int main(){ int t = ;
while (scanf("%d", &n) && n){
printf("Test case #%d\n", ++t);
init();
work();
}
return ;
}