面积并+扫描线 覆盖的面积 HDU - 1255

时间:2023-03-10 03:19:58
面积并+扫描线 覆盖的面积 HDU - 1255

题目链接:https://cn.vjudge.net/problem/HDU-1255

题目大意:中文题目

具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两次以上的面积,我们可以在原来的基础上进行改进,原来的tree1储存的是覆盖一次的合理的面积,我们再加一个tree2求得是覆盖两次及以上的面积,具体的判断过程:

1,如果lazy[rt]>1,就代表这块区域完全的被覆盖了两次,那么这块区域的面积就是hash【r+1】-hash【l】。

2,如果是根节点,覆盖两次的面积是0,我们判断是l+1==r。

3,如果是lazy[rt]==1的话,我们就取原来覆盖一次的面积上已经被覆盖的,这样就相当于求的是覆盖两次的面积。

4,其余的话,就是两个根节点覆盖两次的面积了。

AC代码:

 #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <ctime>
#define ll long long
# define lson l,m,rt<<
# define rson m+,r,rt<<|
using namespace std;
const int maxn = +;
# define inf 0x3f3f3f3f
struct node
{
double l,r,h;
int d;
node() {}
node(double xx,double yy,double zz,int tt)
{
l=xx;
r=yy;
h=zz;
d=tt;
}
bool friend operator < (node t1,node t2)
{
return t1.h<t2.h;
}
} q[maxn<<];
double Hash[maxn<<],tree1[maxn<<],tree2[maxn<<],lazy[maxn<<];
void up(int l,int r,int rt)
{
if(lazy[rt])
tree1[rt]=Hash[r+]-Hash[l];
else if(l==r)
tree1[rt]=;
else
tree1[rt]=tree1[rt<<]+tree1[rt<<|];
if(lazy[rt]>)
tree2[rt]=Hash[r+]-Hash[l];
else if(l==r)
tree2[rt]=;
else if(lazy[rt]==)
tree2[rt]=tree1[rt<<]+tree1[rt<<|];
else
tree2[rt]=tree2[rt<<]+tree2[rt<<|];
} void update(int l,int r,int rt,int L,int R,int p)
{
if(L<=l&&R>=r)
{
lazy[rt]+=p;
up(l,r,rt);
return ;
}
int m=(l+r)>>;
if(L<=m)
update(lson,L,R,p);
if(R>m)
update(rson,L,R,p);
up(l,r,rt);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,num=;
scanf("%d",&n);
double x1,y1,x2,y2;
for(int i=; i<n; i++)
{
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
q[num]= {x1,x2,y1,};
Hash[num++]=x1;
q[num]= {x1,x2,y2,-};
Hash[num++]=x2;
}
sort(Hash,Hash+num);
sort(q,q+num);
int k=;
for(int i=; i<num; i++)
{
if(Hash[i]==Hash[i-])
continue;
Hash[k++]=Hash[i];
}
memset(tree1,,sizeof(tree1));
memset(tree2,,sizeof(tree2));
memset(lazy,,sizeof(lazy));
double ans=;
// cout<<1<<endl;
for(int i=; i<num; i++)
{
// cout<<i<<endl;
int l=lower_bound(Hash,Hash+k,q[i].l)-Hash;
int r=lower_bound(Hash,Hash+k,q[i].r)-Hash-;
update(,k-,,l,r,q[i].d);
ans+=tree2[]*(q[i+].h-q[i].h);
}
printf("%.2lf\n",ans);
}
return ;
}