Get The Treasury
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3023 Accepted Submission(s): 979
Problem Description
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 x
1, y
1, z
1, x
2, y
2 and z
2 (x
1<x
2, y
1<y
2, z
1<z
2). 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 x
1 to x
2. 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.
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.
Input
The first line of the input file contains a single integer t, the number of test cases, followed by the input data for each test case.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x 1, y 1, z 1, x 2, y 2 and z 2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, and that of z coordinate is no more than 500.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x 1, y 1, z 1, x 2, y 2 and z 2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, and that of z coordinate is no more than 500.
Output
For each test case, you should output “Case a: b” in a single line. a is the case number, and b is the minimum volume of treasury. The case number is counted from one.
Sample Input
2 1 0 0 0 5 6 4 3 0 0 0 5 5 5 3 3 3 9 10 11 3 3 3 13 20 45
Sample Output
Case 1: 0 Case 2: 8
题解:
因为x,y,z都较小,可以枚举每一个z,这样就转化成了求
每一平面中三个或以上矩形相交的面积。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1007;
#define ll long long
ll x1[maxn],y11[maxn],z1[maxn];
ll x2[maxn],y2[maxn],z2[maxn],y[maxn*2];
ll len1[(maxn*2)<<2],len2[(maxn*2)<<2];//覆盖一次覆盖两次的长度
int t;
struct LINE
{
ll x,y_up,y_down;
int flag;
}line[(maxn*2)<<2];
bool cmp(const LINE& a,const LINE& b)
{
return a.x<b.x;
}
struct TREE
{
int l,r;
ll len;
int cover;
}tree[(maxn*2)<<2];
void add(ll xx1,ll xx2,ll yy1,ll yy2)
{
t++;
y[t]=yy1;
line[t].x=xx1;
line[t].y_down=yy1;
line[t].y_up=yy2;
line[t].flag=1;
t++;
y[t]=yy2;
line[t].x=xx2;
line[t].y_down=yy1;
line[t].y_up=yy2;
line[t].flag=-1;
}
void build(int l,int r,int rt)
{
tree[rt].cover=0;
tree[rt].len=0;
tree[rt].l=l;
tree[rt].r=r;
if(l==r)return;
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
}
void cal(int rt)
{
//注意操作的先后顺序
if(tree[rt].cover>=3)
{
tree[rt].len=y[tree[rt].r+1]-y[tree[rt].l];
len1[rt]=len2[rt]=0;
}
else if(tree[rt].cover==2)
{
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len+len2[rt<<1]+len2[rt<<1|1]+len1[rt<<1]+len1[rt<<1|1];
len2[rt]=y[tree[rt].r+1]-y[tree[rt].l]-tree[rt].len;
len1[rt]=0;
}
else if(tree[rt].cover==1)
{
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len+len2[rt<<1]+len2[rt<<1|1];
len2[rt]=len1[rt<<1]+len1[rt<<1|1];
len1[rt]=y[tree[rt].r+1]-y[tree[rt].l]-tree[rt].len-len2[rt];
}
else
{
len1[rt]=len1[rt<<1]+len1[rt<<1|1];
len2[rt]=len2[rt<<1]+len2[rt<<1|1];
tree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;
}
}
void update(int l,int r,int rt,int flag)
{
if(l<=tree[rt].l&&r>=tree[rt].r)
{
tree[rt].cover+=flag;
cal(rt);
return;
}
int m=(tree[rt].l+tree[rt].r)>>1;
if(l<=m)update(l,r,rt<<1,flag);
if(r>m)update(l,r,rt<<1|1,flag);
cal(rt);
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int i,j;ll mi=555,ma=-555;
for(i=1; i<=n; i++)
{
scanf("%lld%lld%lld%lld%lld%lld",&x1[i],&y11[i],&z1[i],&x2[i],&y2[i],&z2[i]);
mi=min(mi,z1[i]);
ma=max(ma,z2[i]);
}
long long ans=0;
for(ll z=mi;z<=ma;z++)//枚举每一平面。
{
t=0;
for(i=1;i<=n;i++)
{
if(z1[i]<=z&&z2[i]>z)
add(x1[i],x2[i],y11[i],y2[i]);
}
if(t==0)continue;
sort(y+1,y+t+1);
sort(line+1,line+t+1,cmp);
int k=2;
for(i=2;i<=t;i++)
if(y[i]!=y[i-1])
y[k++]=y[i];
k--;
build(1,k,1);
memset(len1,0,sizeof(len1));
memset(len2,0,sizeof(len2));
for(i=1;i<=t;i++)
{
int a=lower_bound(y+1,y+k+1,line[i].y_down)-y;
int b=lower_bound(y+1,y+k+1,line[i].y_up)-y-1;
update(a,b,1,line[i].flag);
ans+=(line[i+1].x-line[i].x)*tree[1].len;
}
}
printf("Case %d: %lld\n",++cas,ans);
}
return 0;
}