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.
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.
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
该博客:
https://blog.csdn.net/u013480600/article/details/22625139
有详细分析
在此贴下代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int MAXN = 2500;
int cover[MAXN << 3],sum3[MAXN << 3],sum2[MAXN << 3],sum[MAXN << 3];
int X[MAXN],Z[MAXN];
int cnt_x,cnt_z;
struct seg{
int l,r,h;
int cnt;
seg(){}
seg(int l,int r,int h,int cnt):l(l),r(r),h(h),cnt(cnt){}
friend bool operator < (const seg &p,const seg &q){
return p.h < q.h;
}
}ss[MAXN];
struct point{
int a,b,c;
};
struct cube{
point p1,p2;
}cube[MAXN];
void pushup(int rt,int l,int r)
{
if(cover[rt] >= 3){
sum3[rt] = X[r + 1] - X[l];
sum2[rt] = sum[rt] = 0;
}else if(cover[rt] == 2){
sum3[rt] = sum3[rt<<1] + sum3[rt<<1|1] + sum2[rt<<1] + sum2[rt<<1|1] + sum[rt<<1] + sum[rt<<1|1];
sum2[rt] = X[r + 1] - X[l] - sum3[rt];
sum[rt] = 0;
}else if(cover[rt] == 1){
sum3[rt] = sum3[rt<<1] + sum3[rt<<1|1] + sum2[rt<<1] + sum2[rt<<1|1];
sum2[rt] = sum[rt<<1] + sum[rt<<1|1];
sum[rt] = X[r + 1] - X[l] - sum3[rt] - sum2[rt];
}else{
sum3[rt] = sum3[rt<<1] + sum3[rt<<1|1];
sum2[rt] = sum2[rt<<1] + sum2[rt<<1|1];
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
}
void update(int rt,int l,int r,int ql,int qr,int val)
{
if(l >= ql && r <= qr){
cover[rt] += val;
pushup(rt,l,r);
return ;
}
int mid = (l + r) >> 1;
if(ql <= mid) update(lson,ql,qr,val);
if(qr > mid) update(rson,ql,qr,val);
pushup(rt,l,r);
}
int main()
{
int t;
scanf("%d",&t);
int ptr = 0;
while(t--)
{
int n;
scanf("%d",&n);
cnt_x = cnt_z = 0;
for(int i = 1;i <= n;++i){
scanf("%d %d %d",&cube[i].p1.a,&cube[i].p1.b,&cube[i].p1.c);
scanf("%d %d %d",&cube[i].p2.a,&cube[i].p2.b,&cube[i].p2.c);
X[cnt_x++] = cube[i].p1.a;
X[cnt_x++] = cube[i].p2.a;
Z[cnt_z++] = cube[i].p1.c;
Z[cnt_z++] = cube[i].p2.c;
}
if(n < 3){
printf("Case %d: 0\n",++ptr);
continue;
}
sort(X,X + cnt_x);
sort(Z,Z + cnt_z);
cnt_x = unique(X,X + cnt_x) - X;
cnt_z = unique(Z,Z + cnt_z) - Z;
LL ans = 0;
for(int i = 0;i < cnt_z - 1;++i){
int cnt = 0;
LL res = 0;
for(int j = 1;j <= n;++j){
if(cube[j].p1.c <= Z[i] && cube[j].p2.c >= Z[i + 1]){
ss[cnt++] = seg(cube[j].p1.a,cube[j].p2.a,cube[j].p1.b,1);
ss[cnt++] = seg(cube[j].p1.a,cube[j].p2.a,cube[j].p2.b,-1);
}
}
sort(ss,ss + cnt);
memset(cover,0,sizeof(cover));
memset(sum3,0,sizeof(sum3));
memset(sum2,0,sizeof(sum2));
memset(sum,0,sizeof(sum));
for(int j = 0;j < cnt - 1;++j){
int l = lower_bound(X,X + cnt_x,ss[j].l) - X;
int r = lower_bound(X,X + cnt_x,ss[j].r) - X;
update(1,0,cnt_x - 1,l,r - 1,ss[j].cnt);
res += (LL)sum3[1] * (ss[j + 1].h - ss[j].h);
}
ans += res * (Z[i + 1] - Z[i]);
}
printf("Case %d: %lld\n",++ptr,ans);
}
return 0;
}