Jack knows that there is a great underground treasury in a secret region. And he has a special device that can be used to detect treasury under the surface of the earth. One day he got outside with the device to ascertain the treasury. He chose many different locations on the surface of the earth near the secret region. And at each spot he used the device to detect treasury and got some data from it representing a region, which may contain treasury below the surface. The data from the device at each spot is six integers x1, y1, z1, x2, y2 and z2 (x1<x2, y1<y2, z1<z2). According to the instruction of the device they represent the range of x, y and z coordinates of the region. That is to say, the x coordinate of the region, which may contain treasury, ranges from x1 to x2. So do y and z coordinates. The origin of the coordinates is a fixed point under the ground.
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.
题意:问矩形三次以上覆盖的面积
线段树--扫描线裸题,统计1次、2次、3次含以上的长度。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxm=;
typedef long long ll; ll st1[maxm<<],st2[maxm<<],st3[maxm<<];
int cov[maxm<<],y[maxm],z[maxm]; struct rect{
int x1,y1,z1,x2,y2,z2;
}r[]; struct seg{
int x,y1,y2,c;
bool operator < (const seg a)const{
return x<a.x;
}
}s[maxm]; void pushup(int o,int l,int r){
if(cov[o]>=)st1[o]=st2[o]=st3[o]=y[r]-y[l];
else if(cov[o]==){
st1[o]=st2[o]=y[r]-y[l];
if(l+==r)st3[o]=;
else st3[o]=st1[o<<]+st1[o<<|];
}
else if(cov[o]==){
st1[o]=y[r]-y[l];
if(l+==r)st2[o]=st3[o]=;
else{
st2[o]=st1[o<<]+st1[o<<|];
st3[o]=st2[o<<]+st2[o<<|];
}
}
else{
if(l+==r)st1[o]=st2[o]=st3[o]=;
else{
st1[o]=st1[o<<]+st1[o<<|];
st2[o]=st2[o<<]+st2[o<<|];
st3[o]=st3[o<<]+st3[o<<|];
}
}
} void update(int o,int l,int r,seg a){
if(a.y1<=y[l]&&a.y2>=y[r]){
cov[o]+=a.c;
pushup(o,l,r);
return;
}
if(l+==r)return;
int m=l+((r-l)>>);
if(a.y1<y[m])update(o<<,l,m,a);
if(a.y2>y[m])update(o<<|,m,r,a);
pushup(o,l,r);
} int main(){
int T;
scanf("%d",&T);
for(int q=;q<=T;++q){
int n;
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%d%d%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].z1,&r[i].x2,&r[i].y2,&r[i].z2);
y[*i-]=r[i].y1;
y[*i]=r[i].y2;
z[*i-]=r[i].z1;
z[*i]=r[i].z2;
}
sort(y+,y+*n+);
sort(z+,z+*n+);
int cntz=,cnty=;
ll ans=;
for(int i=;i<=*n;++i){
if(y[i]!=y[i-])y[++cnty]=y[i];
if(z[i]!=z[i-])z[++cntz]=z[i];
}
for(int i=;i<cntz;++i){
int cnt=;
for(int j=;j<=n;++j){
if(r[j].z1<=z[i]&&r[j].z2>z[i]){
++cnt;
s[cnt].x=r[j].x1;s[cnt].y1=r[j].y1;s[cnt].y2=r[j].y2;
s[cnt].c=;
++cnt;
s[cnt].x=r[j].x2;s[cnt].y1=r[j].y1;s[cnt].y2=r[j].y2;
s[cnt].c=-;
}
}
sort(s+,s+cnt+);
ll sum=;
for(int j=;j<cnt;++j){
update(,,cnty,s[j]);
sum+=st3[]*(s[j+].x-s[j].x);
}
update(,,cnty,s[cnt]);
ans+=sum*(z[i+]-z[i]);
}
printf("Case %d: %lld\n",q,ans);
}
return ;
}