Get The TreasuryTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2341 Accepted Submission(s): 714
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.
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
Sample Output
|
题意:给你n个立方体,问你重复覆盖三次以及以上的体积。
思路:枚举z,然后就是求解二维平面重复覆盖三次以及以上的面积。理解HDU1255 就简单多了。
注意PushUp的操作,小心点就好了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (2000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
struct Tree{
int l, r, len;
int sum1, sum2, sum3;
int cover;
};
Tree tree[MAXN<<2];
struct Node{
int x1, x2, z1, z2, y;
int cover;
};
bool cmp(Node a, Node b){
return a.y < b.y;
}
Node num[MAXN], num1[MAXN];
void Build(int o, int l, int r)
{
tree[o].l = l; tree[o].r = r;
tree[o].sum1 = tree[o].sum2 = tree[o].sum3 = tree[o].cover = 0;
if(l == r)
return ;
int mid = (l + r) >> 1;
Build(lson); Build(rson);
}
int rec[MAXN], z[MAXN];
int Find(int l, int r, int val)
{
while(r >= l)
{
int mid = (l + r) >> 1;
if(rec[mid] == val)
return mid;
else if(rec[mid] > val)
r = mid-1;
else
l = mid+1;
}
}
void PushUp(int o)
{
if(tree[o].cover >= 3)
{
tree[o].sum3 = rec[tree[o].r+1] - rec[tree[o].l];
tree[o].sum1 = tree[o].sum2 = 0;
}
else if(tree[o].cover == 2)
{
if(tree[o].l == tree[o].r)
tree[o].sum3 = 0;
else
tree[o].sum3 = tree[ll].sum3 + tree[rr].sum3 + tree[ll].sum2 + tree[rr].sum2 + tree[ll].sum1 + tree[rr].sum1;
tree[o].sum2 = rec[tree[o].r+1] - rec[tree[o].l] - tree[o].sum3;
tree[o].sum1 = 0;
}
else if(tree[o].cover == 1)
{
if(tree[o].l == tree[o].r)
tree[o].sum3 = tree[o].sum2 = 0;
else
{
tree[o].sum3 = tree[ll].sum3 + tree[rr].sum3 + tree[ll].sum2 + tree[rr].sum2;
tree[o].sum2 = tree[ll].sum1 + tree[rr].sum1;
}
tree[o].sum1 = rec[tree[o].r+1] - rec[tree[o].l] - tree[o].sum3 - tree[o].sum2;
}
else
{
if(tree[o].l == tree[o].r)
tree[o].sum3 = tree[o].sum2 = tree[o].sum1 = 0;
else
{
tree[o].sum3 = tree[ll].sum3 + tree[rr].sum3;
tree[o].sum2 = tree[ll].sum2 + tree[rr].sum2;
tree[o].sum1 = tree[ll].sum1 + tree[rr].sum1;
}
}
}
void Update(int o, int L, int R, int v)
{
if(tree[o].l >= L && tree[o].r <= R)
{
tree[o].cover += v;
PushUp(o);
return ;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if(R <= mid)
Update(ll, L, R, v);
else if(L > mid)
Update(rr, L, R, v);
else
{
Update(ll, L, mid, v);
Update(rr, mid+1, R, v);
}
PushUp(o);
}
int main()
{
int t, kcase = 1; Ri(t);
W(t)
{
int n; Ri(n);
int k = 0, len = 1;
for(int i = 0; i < n; i++)
{
int x1, y1, z1, x2, y2, z2;
Ri(x1); Ri(y1); Ri(z1); Ri(x2); Ri(y2); Ri(z2);
num1[k].x1 = x1; num1[k].x2 = x2; num1[k].z1 = z1; num1[k].z2 = z2;
num1[k].y = y1; num1[k++].cover = 1;
num1[k].x1 = x1; num1[k].x2 = x2; num1[k].z1 = z1; num1[k].z2 = z2;
num1[k].y = y2; num1[k++].cover = -1;
z[len] = z1; rec[len++] = x1;
z[len] = z2; rec[len++] = x2;
}
sort(z+1, z+len); int Rz = 2;
for(int i = 2; i < len; i++)
if(z[i] != z[i-1])
z[Rz++] = z[i];
sort(rec+1, rec+len); int R = 2;
for(int i = 2; i < len; i++)
if(rec[i] != rec[i-1])
rec[R++] = rec[i];
sort(num1, num1+k, cmp);
Build(1, 1, R-1); LL ans = 0;
for(int i = 1; i < Rz-1; i++)
{
int top = 0;
for(int j = 0; j < k; j++)
if(num1[j].z1 <= z[i] && num1[j].z2 > z[i])
num[top++] = num1[j];
LL now = 0;
for(int j = 0; j < top; j++)
{
int x = Find(1, R-1, num[j].x1);
int y = Find(1, R-1, num[j].x2);
if(j) now += 1LL * tree[1].sum3 * (num[j].y - num[j-1].y);
if(x <= y-1)
Update(1, x, y-1, num[j].cover);
}
ans += 1LL * (z[i+1] - z[i]) * now;
}
printf("Case %d: %lld\n", kcase++, ans);
}
return 0;
}