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.
题意:给出若干个矩形,求他们的总面积,即矩形的面积并
求面积并是线段树-扫描线的裸题
1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<math.h>
5 using namespace std;
6 const int maxm=20005;
7
8 int cov[maxm<<2];
9 double y[maxm],st[maxm];
10
11 struct seg{
12 double x,y1,y2;
13 int c;
14 bool operator < (const seg a)const{
15 return x<a.x;
16 }
17 }s[maxm];
18
19 void pushup(int o,int l,int r){
20 if(cov[o]>0)st[o]=y[r]-y[l];
21 else if(cov[o]==0){
22 if(l+1==r)st[o]=0;
23 else st[o]=st[o<<1]+st[o<<1|1];
24 }
25 }
26
27 void update(int o,int l,int r,seg a){
28 if(a.y1<=y[l]&&a.y2>=y[r]){
29 cov[o]+=a.c;
30 pushup(o,l,r);
31 return;
32 }
33 if(l+1==r)return;
34 int m=l+((r-l)>>1);
35 if(a.y1<y[m])update(o<<1,l,m,a);
36 if(a.y2>y[m])update(o<<1|1,m,r,a);
37 pushup(o,l,r);
38 }
39
40 int main(){
41 int n;
42 int c=0;
43 while(scanf("%d",&n)!=EOF&&n){
44 memset(st,0,sizeof(st));
45 memset(cov,0,sizeof(cov));
46 for(int i=1;i<=n;++i){
47 double x1,y1,x2,y2;
48 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
49 s[2*i-1].x=x1;s[2*i-1].y1=y1;s[2*i-1].y2=y2;s[2*i-1].c=1;
50 y[2*i-1]=y1;
51 s[2*i].x=x2;s[2*i].y1=y1;s[2*i].y2=y2;s[2*i].c=-1;
52 y[2*i]=y2;
53 }
54 sort(s+1,s+2*n+1);
55 sort(y+1,y+2*n+1);
56 double ans=0;
57 int cnt=1;
58 for(int i=2;i<=2*n;++i){
59 if(y[i]!=y[i-1])y[++cnt]=y[i];
60 }
61 for(int i=1;i<2*n;++i){
62 update(1,1,cnt,s[i]);
63 ans+=st[1]*(s[i+1].x-s[i].x);
64 }
65 printf("Test case #%d
",++c);
66 printf("Total explored area: %.2lf
",ans);
67 }
68 return 0;
69 }