覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4417 Accepted Submission(s): 2180
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
Ignatius.L
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
int T;
int n;
struct Edge
{
double l,r,y;
int f;
Edge(){};
Edge(double ll,double rr,double yy,int ff):l(ll),r(rr),y(yy),f(ff){};
}edge[1010*4];
int len;
double X[1010*4];
double sum[1010*10];
double more[1010*10];
int col[1010*10];
int cmp(Edge a,Edge b)
{
return a.y < b.y;
}
void PushUp(int rt,int l,int r)
{
if(col[rt] >= 2)
{
sum[rt] = more[rt] = X[r]- X[l];
}
else if(col[rt] == 1)
{
sum[rt] = X[r] - X[l];
if(r - l!=1)
more[rt] = sum[rt<<1] + sum[rt<<1|1];
else
more[rt] = 0;
}
else
{
if(r - l == 1)
{
sum[rt] = more[rt] = 0;
}
else
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
more[rt] = more[rt<<1] + more[rt<<1|1];
}
}
}
void update(double L,double R,int c,int l,int r,int rt)
{
if(L <= X[l] && X[r] <= R )
{
col[rt] += c;
PushUp(rt,l,r);
return;
}
int m = (l + r) >> 1;
if(L < X[m]) update(L,min(R,X[m]),c,lson);
if(X[m] < R) update(max(L,X[m]),R,c,rson);
PushUp(rt,l,r);
}
int main()
{
scanf("%d",&T);
while(T --)
{
len = 0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1 > x2) swap(x1,x2);
if(y1 > y2) swap(y1,y2);
edge[len] = Edge(x1,x2,y1,1);
X[len++] = x1;
edge[len] = Edge(x1,x2,y2,-1);
X[len++] = x2;
}
sort(edge,edge+len,cmp);
sort(X,X+len);
int MAXN = 1;
for(int i=1;i<len;i++)
{
if(X[i] != X[i-1])
{
X[MAXN++] = X[i];
}
}
//build(0,MAXN-1,1);
memset(more,0,sizeof(more));
memset(sum,0,sizeof(sum));
memset(col,0,sizeof(col));
double ans = 0;
for(int i=0;i<len-1;i++)
{
update(edge[i].l,edge[i].r,edge[i].f,0,MAXN-1,1);
ans += more[1] * (edge[i+1].y - edge[i].y);
}
printf("%.2lf\n",ans);
}
}