比单求一次覆盖多了一个ans2数组,用来存覆盖两次以上的区域长度,另外,覆盖一次的时候不用下推,就是说如果1-10区域被覆盖一次,表示1-5 和6-10的节点中表示覆盖的值仍然是0,而这次,必须要把1-10覆盖的区域下推,保证1-10其左子树右子树及以下所有树节点都必须更新,上推的时候记得当此节点被全部覆盖后ans2仍然要等于子节点之和。
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<ctime>
#include<algorithm>
#define eps 1e-14
#define pi acos(-1)
#define ll long long
#define RD T*(rand()*2-RAND_MAX)
#define Drand (long double)rand()/RAND_MAX
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1e4+100;
double ans[maxn<<3],x[maxn],ans2[maxn<<3];
int cov[maxn<<3],tot=1;
struct mm
{
double h,l,r;
int f;
mm(){}
mm(double l,double r,double h,int f):l(l),r(r),h(h),f(f){}
};
int cmp(mm a,mm b)
{
return a.h<b.h;
}
void pushup(int rt,int l,int r)
{
if(cov[rt]>0){
ans[rt]=x[r+1]-x[l];
ans2[rt]=ans2[rt<<1]+ans2[rt<<1|1];
if(cov[rt]>1){
ans2[rt]=ans[rt];
}
}
else if(l==r){
ans[rt]=0;
ans2[rt]=0;
}
else{
ans[rt]=ans[rt<<1]+ans[rt<<1|1];
ans2[rt]=ans2[rt<<1]+ans2[rt<<1|1];
}
}
void build(int l,int r,int rt)
{
ans[rt]=0;
cov[rt]=0;
ans2[rt]=0;
if(l==r)return;
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
}
void Update(int L,int R,int l,int r,int rt,int c)
{
if(L<=l && r<=R)
{
cov[rt]+=c;
pushup(rt,l,r);
if(l==r)return;
}
int mid=(l+r)>>1;
if(L<=mid)Update(L,R,l,mid,rt<<1,c);
if(R>mid)Update(L,R,mid+1,r,rt<<1|1,c);
pushup(rt,l,r);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
tot=1;
vector<mm>v;
for(int i=0;i<n;i++){
double b,c,d,e;
scanf("%lf%lf%lf%lf",&b,&c,&d,&e);
v.push_back(mm(b,d,c,1));
v.push_back(mm(b,d,e,-1));
x[tot++]=b;
x[tot++]=d;
}
sort(v.begin(),v.end(),cmp);
sort(x+1,x+tot);
int k=2;
for(int i=2;i<tot;i++){
if(x[i]!=x[i-1])x[k++]=x[i];
}
tot=k;
build(1,tot-2,1);
double area=0;
for(int i=0;i<v.size()-1;i++){
int l=lower_bound(x+1,x+tot,v[i].l)-x;
int r=lower_bound(x+1,x+tot,v[i].r)-x-1;
Update(l,r,1,tot-2,1,v[i].f);
area+=(v[i+1].h-v[i].h)*ans2[1];
}
printf("%.2lf\n",area);
}
return 0;
}