描述
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 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.
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.
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
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
Total explored area: 180.00
题意
给你N个矩形,每个矩形的左下和右上的点,求面积并
题解
由于点不是很多,可以对x轴进行离散化,去重
线段树面积并
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int N=; int col[N<<];
double sum[N<<],x[N<<]; struct seg
{
double l,r,h;
int s;
seg(){}
seg(double l,double r,double h,int s):l(l),r(r),h(h),s(s){}
bool operator<(const seg &D)const{
return h<D.h;
}
}s[N];
void PushUp(int rt,int l,int r)
{
if(col[rt])sum[rt]=x[r+]-x[l];
else if(l==r)sum[rt]=;
else sum[rt]=sum[rt<<]+sum[rt<<|];
}
void Update(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]+=C;
PushUp(rt,l,r);
return;
}
int mid=(l+r)>>;
if(L<=mid)Update(L,R,C,l,mid,rt<<);
if(R>mid)Update(L,R,C,mid+,r,rt<<|);
PushUp(rt,l,r);
}
int main()
{
int n,o=;
double a,b,c,d;
while(~scanf("%d",&n),n)
{
int cnt=;
for(int i=;i<n;i++)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
s[++cnt]=seg(a,c,b,);
x[cnt]=a;
s[++cnt]=seg(a,c,d,-);
x[cnt]=c;
}
sort(x+,x++cnt);
sort(s+,s++cnt);
int k=;
for(int i=;i<=cnt;i++)
if(x[i]!=x[i-])
x[++k]=x[i]; memset(sum,,sizeof sum);
memset(col,,sizeof col);
double ans=;
for(int i=;i<cnt;i++)
{
int l=lower_bound(x+,x++k,s[i].l)-x;
int r=lower_bound(x+,x++k,s[i].r)-x-;
Update(l,r,s[i].s,,k,);
ans+=sum[]*(s[i+].h-s[i].h);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",o++,ans);
}
return ;
}