Get The Treasury HDU - 3642 scan line + volume intersection

I. content

 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

Two, train of thought

  • Prefix Title: HDU1542 HDU1255
  • Since z has only 1000 layers at most, we can directly sum the area of each layer. But there is a special point to pay attention to when calculating the number of layers. For example, z contains 3.45 layers. When calculating, we only need to find 3 layers. In fact, we only need to find 2 layers for volume. So just judge. In this way, if there is only one layer, it will not be included, because one layer can not form volume. The others are similar to the basic scan line.
  • It is similar to HDU1255 to find the area of the third intersection.

Three, code

#include <cstdio>
#include <iostream>
#include <algorithm>
typedef long long ll;
using namespace std;
const int N = 2e4 + 5, M = 1005;
struct Line {
	int st;
	double s, e, x, z1, z2; //z1, z2 record the range of this line 
	bool operator < (const Line & w) const {
		return x < w.x;
	}
} line[N];
struct Node {
	int cnt, len1, len2, len3;
} tr[N << 2];
int t, n, cnt, m, x1, x2, y1, y2, z1, z2, fy[N];
int find(int y) {
	return lower_bound(fy + 1, fy + 1 + cnt, y) - fy;
}
void pushup(int id, int l, int r) {
	if (tr[id].cnt){ // The range representing id is covered  
		tr[id].len1 = fy[r + 1] - fy[l]; //Because a point represents an interval, the interval of r is the length of [fy[r], fy[r + 1]] 
	} else if (l != r){ //If the whole interval is not included, it's made up of son intervals 
		tr[id].len1 = tr[id << 1].len1 + tr[id << 1 | 1].len1; 
	} else tr[id].len1 = 0; //leaf node  
	//When cnt > = 2
	if (tr[id].cnt >= 2) {
		tr[id].len2 = fy[r + 1] - fy[l];
	} else if (l != r && tr[id].cnt == 1)  { //When cnt == 1, if the subinterval is included once, then add the father's once to be twice		
		tr[id].len2 = tr[id << 1].len1 + tr[id << 1 | 1].len1; 
	} else if (l != r){ //In this case, if it is not the root node, it can be obtained through len2 of the child node 
		tr[id].len2 = tr[id << 1].len2 + tr[id << 1 | 1].len2;   
	} else { //The root node or the one that does not meet the above conditions is directly equal to 0 
		tr[id].len2 = 0;
	} 
	//When cnt > = 3
	if (tr[id].cnt >= 3) {
		tr[id].len3 = fy[r + 1] - fy[l];
	} else if (l != r && tr[id].cnt == 2)  {  	
		tr[id].len3 = tr[id << 1].len1 + tr[id << 1 | 1].len1; 
	} else if (l != r && tr[id].cnt == 1)  {  	
		tr[id].len3 = tr[id << 1].len2 + tr[id << 1 | 1].len2; 
	} else if (l != r){ 
		tr[id].len3 = tr[id << 1].len3 + tr[id << 1 | 1].len3;   
	} else { 
		tr[id].len3 = 0;
	} 
}
void build(int id, int l, int  r) {
	tr[id].cnt = tr[id].len1 = tr[id].len2 = tr[id].len3 = 0;
	if (l == r) return;
	int mid = (l + r) >> 1;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
}
void update(int id, int l, int r, int x, int y, int d) {
	if (x <= l && r <= y) {
		tr[id].cnt += d;
		pushup(id, l, r);
		return;
	}
	int mid = (l + r) >> 1;
	if (x <= mid) update(id << 1, l, mid, x, y, d);
	if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, d);
	pushup(id, l, r); 
}
int main() {
	scanf("%d", &t); 
	for (int ci = 1; ci <= t; ci++) {
		scanf("%d", &n);
		cnt = 0; //How many y points are there 
		for (int i = 1; i <= n; i++) {
			scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
 			line[++cnt].s = y1; line[cnt].e = y2, line[cnt].x = x1, line[cnt].z1 = z1, line[cnt].z2 = z2, line[cnt].st = 1, fy[cnt] = y1; 
 			line[++cnt].s = y1; line[cnt].e = y2, line[cnt].x = x2, line[cnt].z1 = z1, line[cnt].z2 = z2, line[cnt].st = -1, fy[cnt] = y2; 
		} 
		//Discretization of y coordinate 
		sort(fy + 1, fy + 1 + cnt);
		sort(line + 1, line + 1 + cnt);
		m = cnt; //m represents how many lines there are 
		cnt = unique(fy + 1, fy + 1 + cnt) - fy - 1; //Represents the number of y points after discretization 
		ll ans = 0;
		for (int z = -500; z <= 500; z++) {
			build(1, 1, cnt - 1);
			int lastx = 0;
			for (int i = 1; i <= m; i++) {
				if (line[i].z1 <= z && line[i].z2 > z) {
					ans += (ll)tr[1].len3 * (line[i].x - lastx), lastx = line[i].x;
					update(1, 1, cnt - 1, find(line[i].s), find(line[i].e) - 1, line[i].st);
				} 
			}
 		}
 		printf("Case %d: %lld\n", ci, ans);
	}
	return 0;
}
Published 355 original articles, won praise 284, visited 50000+
Private letter follow

Posted by redbullmarky on Mon, 27 Jan 2020 07:22:11 -0800