覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5714 Accepted Submission(s): 2850Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.注意:本题的输入数据较多,推荐使用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 1Sample Output
7.63
0.00
题意:中文QAQ
题解:矩形的面积交(离散化+线段树),代码部分的注解已经很详细了
代码:和POJ 1151 Atlantis(矩形面积并)类似,只改动了很少一部分代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson t<<1,l,mid
#define rson t<<1|1,mid,r
/**
矩形面积交
给定一些矩形
求被这些矩形覆盖过至少两次的区域的面积*/
using namespace std;
const int maxn = 2000;
struct Line
{
//把一段段平行于y轴的线段表示成数组 ,
//x是线段的x坐标,y1,y2线段对应的下端点和上端点的坐标
//一个矩形 ,左边的那条边f为1,右边的为-1,
double x,y1,y2;
double flag;
bool operator < (const Line &a)const
{
return x<a.x;
}
}line[maxn];
struct Node
{
int l,r;
int cov;//cov用来记录重叠情况
double lf,rf;//rf,lf分别是对应的左右真实的浮点数端点
double covLenOne;//覆盖一次以上的长度
double covLenTwo;//覆盖两次以上的长度
}node[maxn<<2];
double y[maxn];
void length(int t)
{
if(node[t].cov >= 2)//说明该区间被覆盖两次或以上,那么长度就可以直接计算,就是该区间的长度
{
node[t].covLenTwo = node[t].covLenOne = node[t].rf-node[t].lf;
return;
}
else if( node[t].cov == 1)
{
node[t].covLenOne = node[t].rf-node[t].lf;
if(node[t].l + 1 == node[t].r)//叶子节点没有孩子了,所以被覆盖两次货以上的长度就是0,无论cov=1或cov=0都是0,因为是叶子。
node[t].covLenTwo = 0;
else//不是叶子节点 ,且cov=1,这个区间被完全覆盖了1次,而有没有被完全覆盖两次或以上则不知道无法确定
node[t].covLenTwo = node[t<<1].covLenOne + node[t<<1|1].covLenOne;//左右孩子区间被覆盖了一次或以上的长度,叠加在双亲上就是双亲被覆盖两次或以上的长度
}
else if(node[t].cov == 0)//cov=0不完全不知道被覆盖的情况
{
if(node[t].l + 1 == node[t].r)//叶子节点没有孩子
node[t].covLenOne = node[t].covLenTwo=0;
else//否则直接将左右孩子给覆盖了两次或以上的长度加起来
{
node[t].covLenOne = node[t<<1].covLenOne+node[t<<1|1].covLenOne;
node[t].covLenTwo = node[t<<1].covLenTwo+node[t<<1|1].covLenTwo;
}
}
}
void build(int t,int l,int r)//建树
{
node[t].l = l;
node[t].r = r;
node[t].covLenOne = node[t].covLenTwo = 0;//表示该区间上被覆盖几次
node[t].lf = y[l];
node[t].rf = y[r];
if(l+1 == r)
return;
int mid = (l+r)>>1;
build(lson);
build(rson);
}
void update(int t,Line e)//加入线段e,后更新线段树
{
if(e.y1 == node[t].lf&&node[t].rf == e.y2)
{
node[t].cov += e.flag;
length(t);
return ;
}
if(e.y2 <=node[t<<1].rf)
update(t<<1,e);
else if(e.y1>=node[t<<1|1].lf)
update(t<<1|1,e);
else
{
Line tmp = e;
tmp.y2 = node[t<<1].rf;
update(t<<1,tmp);
tmp = e;
tmp.y1 = node[t<<1|1].lf;
update(t<<1|1,tmp);
}
length(t);
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
int t = 1;
for(int i=1;i<=n;i++)
{
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
line[t].x = x1;
line[t].y1 = y1;
line[t].y2 = y2;
line[t].flag = 1;
y[t] = y1;
t++;
line[t].x = x2;
line[t].y1 = y1;
line[t].y2 = y2;
line[t].flag = -1;
y[t] = y2;
t++;
}
sort(y+1,t+y);
sort(line+1,line+t);
build(1,1,t-1);
update(1,line[1]);
double res = 0;
for(int i=2;i<t;i++)
{
res += node[1].covLenTwo*(line[i].x - line[i-1].x);
update(1,line[i]);
}
printf("%.2lf\n",res);
}
return 0;
}