求长方体相交3次以上的区域的体积
枚举相邻的两个z,选出z坐标包括枚举范围的长方体的底面,然后求面积覆盖3次以上的,再来乘以枚举的z之差,最后取和。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
typedef long long ll;
map<int,int>x;//用来离散x坐标
map<int,int>z;//用来离散z坐标,缩小枚举次数
map<int,int>::iterator it;
int cx[1010*2],cz[1010*2],kx,kz,t,n;
struct spot//长方体
{
int x1,y1,z1,x2,y2,z2;
}s[1010];
struct line
{
int s,t,y,cov;
}l[1010*2];
bool cmp(line i,line j)
{
return i.y<j.y;
}
struct node
{
int l,r,len1,len2,len3;
int cov;
}root[1010*2*6];
inline void build(int t,int x,int y)
{
root[t].l=x,root[t].r=y;
root[t].cov=root[t].len1=root[t].len2=root[t].len3=0;
if(x+1==y) return;
int m=(x+y)>>1;
build(t*2,x,m);
build(t*2+1,m,y);
}
inline void Push_up(int t)//pushup学得小媛的
{
if(root[t].cov>=3)
{
root[t].len3=cx[root[t].r]-cx[root[t].l];
root[t].len2=root[t].len1=0;
}
else if(root[t].cov==2)
{
if(root[t].r-1==root[t].l)
{
root[t].len2=cx[root[t].r]-cx[root[t].l];
root[t].len3=root[t].len1=0;
}
else
{
root[t].len3=root[t*2].len3+root[t*2+1].len3+root[t*2].len2+root[t*2+1].len2+root[t*2].len1+root[t*2+1].len1;
root[t].len2=cx[root[t].r]-cx[root[t].l]-root[t].len3;
root[t].len1=0;
}
}
else if(root[t].cov==1)
{
if(root[t].r-1==root[t].l)
{
root[t].len1=cx[root[t].r]-cx[root[t].l];
root[t].len3=root[t].len2=0;
}
else
{
root[t].len3=root[t*2].len3+root[t*2+1].len3+root[t*2].len2+root[t*2+1].len2;
root[t].len2=root[t*2].len1+root[t*2+1].len1;
root[t].len1=cx[root[t].r]-cx[root[t].l]-root[t].len3-root[t].len2;
}
}
else if(root[t].cov==0)
{
if(root[t].r-1==root[t].l)
{
root[t].len1=root[t].len3=root[t].len2=0;
}
else
{
root[t].len1=root[t*2].len1+root[t*2+1].len1;
root[t].len2=root[t*2].len2+root[t*2+1].len2;
root[t].len3=root[t*2].len3+root[t*2+1].len3;
}
}
}
inline void Modefiy(int t,int x,int y,int cov)
{
int l=root[t].l,r=root[t].r;
if(r<x||l>y) return;
if(l>=x&&r<=y)
{
root[t].cov+=cov;
Push_up(t);
return;
}
if(l+1==r) return;
Modefiy(t*2,x,y,cov);
Modefiy(t*2+1,x,y,cov);
Push_up(t);
}
int main()
{
scanf("%d",&t);
for(int Ca=1;Ca<=t;Ca++)
{
x.clear(),z.clear();
kx=1,kz=1;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d%d%d",&s[i].x1,&s[i].y1,&s[i].z1,&s[i].x2,&s[i].y2,&s[i].z2);
if(x[s[i].x1]==0) x[s[i].x1]=1;
if(x[s[i].x2]==0) x[s[i].x2]=1;
if(z[s[i].z1]==0) z[s[i].z1]=1;
if(z[s[i].z2]==0) z[s[i].z2]=1;
}
for(it=x.begin();it!=x.end();it++)//离散化
{
// printf("%d\n",it->first);
x[it->first]=kx;
cx[kx++]=it->first;
}
for(it=z.begin();it!=z.end();it++)
{
z[it->first]=kz;
cz[kz++]=it->first;
}
ll ans=0;
if(n>=3)
{
for(int i=1;i+1<kz;i++)
{
int sz=cz[i],tz=cz[i+1];
int num=0;
for(int j=0;j<n;j++)
{
if(s[j].z1<=sz&&s[j].z2>sz)
{
l[num].s=s[j].x1,l[num].t=s[j].x2,l[num].y=s[j].y1,l[num++].cov=1;
l[num].s=s[j].x1,l[num].t=s[j].x2,l[num].y=s[j].y2,l[num++].cov=-1;
}
}
sort(l,l+num,cmp);
build(1,1,kx);
ll temp=0;
for(int j=0;j+1<num;j++)
{
int a=x[l[j].s];
int b=x[l[j].t];
Modefiy(1,a,b,l[j].cov);
// if(root[t].len3<0) printf("z\n");
temp+=root[1].len3*1ll*(l[j+1].y-l[j].y);
}
ans+=(tz-sz)*temp;
}
}
printf("Case %d: %I64d\n",Ca,ans);
}
return 0;
}