n个矩形 问他们覆盖的面积重复的就算一次
x数组存线段 然后根据横坐标排一下
z 线段树 l - r 就是1 ~ 2*n
#include<stdio.h>
#include<algorithm>
#include<string.h> using namespace std; #define MAXN 110
struct line
{
double x,y1,y2;
int flag;
}x[MAXN];
double y[MAXN<<]; struct node
{
int l,r,cov;
double x,y_up,y_down; }z[*MAXN]; bool cmp(line a,line b)
{
return a.x<b.x;
}
void Build(int l,int r,int a)
{
z[a].l=l;
z[a].r=r;
z[a].cov=;
z[a].x=-;
z[a].y_down=y[l];
z[a].y_up=y[r]; if(l+==r)//叶子节点
return ;
int mid=(l+r)>>;
Build(l,mid,a<<);
Build(mid,r,a<<|);//这边是mid
}
double Insert(int l,int r,int ind,int a)
{
if(x[ind].y1>=z[a].y_up||x[ind].y2<=z[a].y_down) //在外面
return ; if(l+==r)//叶子节点
{
if(z[a].cov>)
{
double sum=(x[ind].x-z[a].x)*(z[a].y_up-z[a].y_down);
z[a].x=x[ind].x; //这边要更新x
z[a].cov+=x[ind].flag;
return sum;
}
else
{
z[a].x=x[ind].x;
z[a].cov+=x[ind].flag;
return ;
}
}
double ans1,ans2;
int mid=(l+r)>>;
ans1=Insert(l,mid,ind,a<<);
ans2=Insert(mid,r,ind,a<<|);//这边是mid
return ans1+ans2; }
int main()
{
int n,ca=; while(scanf("%d",&n)!=EOF&&n)
{
int cnt=; for(int i=;i<=n;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
x[cnt].x=x1;
x[cnt].y1=y1;
x[cnt].y2=y2;
x[cnt].flag=;
y[cnt]=y1;
cnt++; x[cnt].x=x2;
x[cnt].y1=y1;
x[cnt].y2=y2;
x[cnt].flag=-;
y[cnt]=y2;
cnt++;
}
sort(y+,y+cnt);
sort(x+,x+cnt,cmp);
Build(,cnt-,);
double ans=;
for(int i=;i<cnt;i++)
{
ans+=Insert(,cnt-,i,);
}
printf("Test case #%d\n",ca++);
printf("Total explored area: %.2lf\n\n",ans);
}
return ;
}