关键词:线段树  扫描线  离散化 相关题目:[POJ1151]Atlantis

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

    The input consists of several test cases. Each test case starts with a line containing a single integer nn of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2x_{1};y_{1};x_{2};y_{2}$(0 ≤ x1 < x ≤ 100000;0 ≤ y{1} < y{2} ≤ 100000),notnecessarilyintegers.Thevalues,not necessarily integers. The values (x{1}; y{1})and and (x{2};y{2})arethecoordinatesofthetopleftresp.bottomrightcornerofthemappedarea.Theinputfileisterminatedby are the coordinates of the top-left resp. bottom-right corner of the mapped area. The input file is terminated by alinecontainingasingle 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 kk is the number of the test case (starting with 1). The second one must be "Total explored area: a", where aa 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

AC代码

Memory: 248KB / Time: 0~16 ms

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<cstdio>
#include<algorithm>
#define re register int
#define rep(i,a,b) for(re i=(a);i<=(b);++(i))
const int N=201,M=820;
struct Seg{
double h,a,b;
int z;
inline bool operator < (const Seg& rhy)const{
return h<rhy.h;
}
}s[N];
int n,l[M],r[M],c[M];
double len[M],ld[M],rd[M],x[N],ans=0;
#define lc(i) (i<<1)
#define rc(i) (i<<1|1)

inline void build(int index,int left,int right){
l[index]=left,r[index]=right;
len[index]=c[index]=0;
ld[index]=x[left],rd[index]=x[right];
if(left+1==right) return ;
int mid=(left+right)>>1;
build(lc(index),left,mid);
build(rc(index),mid,right);
}

inline void calc(int index){
if(c[index]){
len[index]=rd[index]-ld[index];
return ;
}
if(l[index]+1==r[index]) len[index]=0; //到叶子节点且
//该处信息被删除赋值=0
else len[index]=len[lc(index)]+len[rc(index)];
}

inline void update(int index,double led,double rid,int k){
if(led==ld[index] && rid==rd[index]){
c[index]+=k;
calc(index);
return ;
}
if(rid≤rd[lc(index)]) update(lc(index),led,rid,k);
else if(led>=ld[rc(index)]) update(rc(index),led,rid,k);
else {
update(lc(index),led,rd[lc(index)],k);
update(rc(index),ld[rc(index)],rid,k);
}
calc(index);
}

int main(){
int tim=0,cnt;
double lx,ly,rx,ry;
while(scanf("%d",&n),n){
++tim,cnt=0;
rep(i,1,n){
scanf("%lf%lf%lf%lf",&lx,&ly,&rx,&ry);
s[++cnt].z=1;
s[cnt].a=lx,s[cnt].b=rx,s[cnt].h=ly;
x[cnt]=lx;
s[++cnt].z=-1;
s[cnt].a=lx,s[cnt].b=rx,s[cnt].h=ry;
x[cnt]=rx;
}
std::sort(s+1,s+1+cnt);
std::sort(x+1,x+1+cnt);

build(1,1,cnt);
ans=0;
update(1,s[1].a,s[1].b,s[1].z);
rep(i,2,cnt){
ans+=len[1]*(s[i].h-s[i-1].h);
update(1,s[i].a,s[i].b,s[i].z);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",tim,ans);
}
}