覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4481 Accepted Submission(s): 2218
Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据.
注意:本题的输入数据较多,推荐使用scanf读入数据.
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
Sample Output
7.63 0.00
Author
Ignatius.L & weigang Lee
Recommend
15108133 | 2015-10-14 18:26:38 | Accepted | 1255 | 452MS | 2172K | 1943 B | C++ | Who_you? |
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define N 5010
using namespace std;
struct s
{
double x1,x2,y;
int flag;
}a[N<<1];
double __hash[N<<2],sum[N<<2],once[N<<2];
int col[N<<2];
int cmp(s a,s b)
{
return a.y<b.y;
}
int bseach(double key,int n)
{
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(__hash[mid]==key)
return mid;
if(__hash[mid]<key)
l=mid+1;
else
r=mid-1;
}
return l;
}
void pushup(int tr,int l,int r)
{
if(col[tr]>1)
{
sum[tr]=once[tr]=__hash[r+1]-__hash[l];
}
else
if(col[tr]==1)
{
once[tr]=__hash[r+1]-__hash[l];
if(l==r)
sum[tr]=0;
else
sum[tr]=once[tr<<1]+once[tr<<1|1];
}
else
{
if(l==r)
sum[tr]=once[tr]=0;
else
{
sum[tr]=sum[tr<<1]+sum[tr<<1|1];
once[tr]=once[tr<<1]+once[tr<<1|1];
}
}
}
void update(int L,int R,int l,int r,int tr,int flag)
{
if(L<=l&&r<=R)
{
col[tr]+=flag;
pushup(tr,l,r);
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update(L,R,l,mid,tr<<1,flag);
if(R>mid)
update(L,R,mid+1,r,tr<<1|1,flag);
pushup(tr,l,r);
}
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(col,0,sizeof(col));
memset(sum,0,sizeof(sum));
memset(once,0,sizeof(once));
double x1,x2,y1,y2;
int i,k=1;
for(i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
a[k].x1=x1;
a[k].x2=x2;
a[k].y=y1;
a[k].flag=1;
__hash[k++]=x1;
a[k].x1=x1;
a[k].x2=x2;
a[k].y=y2;
a[k].flag=-1;
__hash[k++]=x2;
}
sort(a+1,a+k,cmp);
sort(__hash+1,__hash+k);
int cnt=unique(__hash+1,__hash+k)-(__hash+1);
double ans=0;
for(i=1;i<k-1;i++)
{
int x,y;
x=bseach(a[i].x1,cnt+1);
y=bseach(a[i].x2,cnt+1)-1;
if(x<=y)
update(x,y,1,cnt+1,1,a[i].flag);
ans+=sum[1]*(a[i+1].y-a[i].y);
}
printf("%.2lf\n",ans);
}
}