Gordon's Beverage
Difficulty: Hard

Mr Gordon likes making beverages.
Mr Gordon is very rich, so he has a farm in space.
His farm is divided into sections wich are cubes of size 1*1*1.
There are *N* fields of different fruit trees.
Each field of trees is located in a big cube of size 4*4*4, with bottom-right-back corner at *(X$_f$,Y$_f$,Z$_f$)*.<br>
Mr Gordon wonders how many different fruit trees are completely contained in the big cube with  top-left-front corner at *(X$_1$,Y$_1$,Z$_1$)* and bottom-right-back corner at *(X$_2$,Y$_2$,Z$_2$)* .

Help him with that.

**Input format**  
The first line contains the number of fruit trees, *N*.  
Next *N* lines contain 3 integers each: *X$_{f_i}$,Y$_{f_i}$,Z$_{f_i}$*, coordinates of fruit field's bottom-right-back corner.  
Next line contains number of Mr Gordon requests *M*.  
Next *M* lines contains 6 integers each:   *X$_{1_i}$,Y$_{1_i}$,Z$_{1_i}$,X$_{2_i}$,Y$_{2_i}$,Z$_{2_i}$*, coordinates of two opposite corners of big cube.  

**Output Format**
You have to print *M* lines where the 
I-th line contains one integer, answer for the I-th request.

**Constraints**  
$1 \le N, M \le 10^5$  
$0 \le |X|, |Y|, |Z| \le 100$  
$X_{1_i} < X_{2_i}$  
$Y_{1_i} < Y_{2_i}$  
$Z_{1_i} < Z_{2_i}$  

**Sample Input**  

	7
	4 4 4
    7 7 7
    4 7 4
    7 4 7
    7 7 4
    4 4 7
    -4 -4 -4
    4
    -8 -8 -8 8 8 8
    0 0 0 7 7 5
    3 7 3 10 10 10
    3 2 0 8 8 6

**Sample Output**  

	7
	3
	0
	1
    
**Explanation**  
Here are the top-left-front and bottom-right-back coordinates of all the fruit fields:

    1. (0, 0, 0) (4, 4, 4)
    2. (3, 3, 3) (7, 7, 7)
    3. (0, 3, 0) (4, 7, 4)
    4. (3, 0, 3) (7, 4, 7)
    5. (3, 3, 0) (7, 7, 4)
    6. (0, 0, 3) (4, 4, 7)
    7. (-8, -8, -8) (-4, -4, -4)

- For query (-8, -8, -8) (8, 8, 8), all fruit fields are inside it. 

- For query (0, 0, 0) (7, 7, 5), the 1st, 3rd, 5th fruit fields are inside it. 

- For query (3, 7, 3) (10, 10, 10), no field is inside it, because every top-left-front vertex is outside of the query cube.

- For query (3, 2, 0) (8, 8, 6), the 5th fruit field is inside it. 

 

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	#include <cstdio>
	using namespace std;

	//N is an array size, choosen such way, that we can store values in indexes starting from 1.
	//For values [-100, 100] we'd like to store them as [1, 201].
	//We are leaving 0'th index unused just not to write 'if'`s when initializing.
	const int N = 202;
	//K is an offset that we'll add to our indexes.
	//In our case we need -100 to become 1, so that's 101.
	const int K = 101;

	//The value, stored at a[x][y][z] is the number of fields with bottom-right-back corner in a cube with top-left-front corner at (0,0,0) and bottom-right-back corner at(x,y,z).
	int a[N][N][N];

	int main()
	{
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
		int n,m;
		scanf("%d", &n);
		for (;n; --n)
		{
			int x,y,z;
			scanf("%d%d%d", &x, &y, &z);
			a[x+K][y+K][z+K]++;

		}
		//initialization
		
		for (int i = 1; i < N; ++i)
		{
			for (int j = 1; j < N; ++j)
			{
				for (int k = 1; k < N; ++k)
				{
					//Here we just use the formula we have got from the inclusion-exclusion principle.
					a[i][j][k] += a[i-1][j][k] + a[i][j-1][k] + a[i][j][k-1]
								- a[i-1][j-1][k] - a[i-1][j][k-1] - a[i][j-1][k-1]
								+ a[i-1][j-1][k-1];
				}
			}
		}

		scanf("%d", &m);
		for (;m; --m)
		{
			int x1, y1, z1, x2, y2, z2;
			scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
			//Here we are adding our offset to all indexes.
			x1 += K, y1 += K, z1 += K, x2 += K, y2 += K, z2 += K;
			//Instead of calculating the number of fields of size 4x4x4 which must fit in a cube [(x1,y1,z1),(x2,y2,z2)]
			//we just calculate the number of bottom-right-back corners in a cube [(x1+3,y1+3,z1+3),(x2,y2,z2)].
			x1+=3,y1+=3,z1+=3;
            
			int res = 0;
			
            if (x1<x2 && y1<y2 && z1<z2) 
			{
				//Again, we use the formula we have got from the inclusion-exclusion principle.
				res = a[x2][y2][z2]
						- a[x1-1][y2][z2] - a[x2][y1-1][z2] - a[x2][y2][z1-1]
						+ a[x1-1][y1-1][z2] + a[x1-1][y2][z1-1] + a[x2][y1-1][z1-1]
						- a[x1-1][y1-1][z1-1];
			}
			printf("%d\n", res);
		}
		return 0;
	}