Atlantis(扫描线面积并)

传送门 HDU1542

描述

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.

输入

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.

输出

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.

样例

  • 输入
    2
    10 10 20 20
    15 15 25 25.5
    0
  • 输出
    Test case #1
    Total explored area: 180.00

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
#define V vector
#define PB push_back
#define lson d<<1,l,mid
#define rson d<<1|1,mid+1,r
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e2+7;
const int mod=1e9+7;
struct Seg{
double x1,x2,y;
int flag; //1下边,-1上边
bool operator < (const Seg& a){return y<a.y;}
};
double len[maxn<<3]; //长度
int cnt[maxn<<3]; //标记,大于0时可取
V<Seg> seg; //边表
V<double> dx; //x离散化
void pushup(int d,int l,int r){
if(cnt[d]) len[d]=dx[r+1]-dx[l]; //不为0就说明整段可取
else if(l==r) len[d]=0;
else len[d]=len[d<<1]+len[d<<1|1];
}
void update(int d,int l,int r,int ql,int qr,int val){
if(ql<=l&&r<=qr) cnt[d]+=val;
else {
int mid=(l+r)>>1;
if(ql<=mid) update(lson,ql,qr,val);
if(qr>mid) update(rson,ql,qr,val);
}
pushup(d,l,r);
}
int main(){
int t=0,n;
while(~scanf("%d",&n)&&n){
t++;
seg.clear();dx.clear();
INIT(len,0);INIT(cnt,0);
double x1,y1,x2,y2;
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
seg.PB((Seg){x1,x2,y1,1});
seg.PB((Seg){x1,x2,y2,-1});
dx.PB(x1);
dx.PB(x2);
}
sort(seg.begin(),seg.end());
sort(dx.begin(),dx.end());
dx.erase(unique(dx.begin(),dx.end()),dx.end());//去重
double ans=0;
for(int i=0;i<seg.size()-1;i++){
int ql=lower_bound(dx.begin(),dx.end(),seg[i].x1)-dx.begin();
int qr=lower_bound(dx.begin(),dx.end(),seg[i].x2)-dx.begin()-1;
//左闭右开
update(1,0,dx.size()-1,ql,qr,seg[i].flag);
ans+=len[1]*(seg[i+1].y-seg[i].y);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",t,ans);
}
return 0;
}
Donate comment here