hdu 1255 覆盖的面积

时间:2023-03-09 16:02:28
hdu 1255 覆盖的面积

http://acm.hdu.edu.cn/showproblem.php?pid=1255

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std; int t,n;
double Y[maxn],X[maxn];
struct point
{
double x;
double y_up;
double y_down;
int lr;
bool operator <(const point &a) const
{
return x<a.x;
}
}p[maxn*]; struct node
{
double x;
double y_up;
double y_down;
int cover;
bool flag;
}tree[maxn*]; void build(int i,int l,int r)
{
tree[i].x=-;
tree[i].y_up=Y[r];
tree[i].y_down=Y[l];
tree[i].cover=;
tree[i].flag=true;
if(l+==r)
{
tree[i].flag=false;
return ;
}
int mid=(l+r)>>;
build(i<<,l,mid);
build(i<<|,mid,r);
} double update(int i,double l,double r,double x,int lr)
{
if(tree[i].y_down>=r||tree[i].y_up<=l) return ;
if(!tree[i].flag)
{
if(tree[i].cover>)
{
tree[i].cover+=lr;
double sum=(x-tree[i].x)*(tree[i].y_up-tree[i].y_down);
tree[i].x=x;
return sum;
}
else
{
tree[i].cover+=lr;
tree[i].x=x;
return ;
}
}
else return update(i<<,l,r,x,lr)+update(i<<|,l,r,x,lr);
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int cnt=;
for(int i=; i<=n; i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
p[cnt].x=x1;
p[cnt].y_down=y1;
p[cnt].y_up=y2;
p[cnt].lr=;
X[cnt]=x1;
Y[cnt++]=y1;
p[cnt].x=x2;
p[cnt].y_down=y1;
p[cnt].y_up=y2;
X[cnt]=x2;
p[cnt].lr=-;
Y[cnt++]=y2;
}
sort(Y,Y+cnt);
sort(X,X+cnt);
sort(p,p+cnt);
build(,,cnt-);
double ans=;
for(int i=; i<cnt; i++)
{
ans+=update(,p[i].y_down,p[i].y_up,p[i].x,p[i].lr);
}
printf("%.2lf\n",ans);
}
return ;
}