We need to solve problem: Given an n x n x n cube (composed of unit small cubes), there are marks on some faces. For each mark, we drill a hole that penetrates horizontally or vertically from the marked surface to the opposite surface. That means: For each mark on a given plane (xy, xz, yz), we drill a hole along a straight line through the cube, passing through the marked point and going perpendicular to that plane? Let's interpret from description.
We have a big cube with side length n (in unit cubes). The faces are at x=0? Actually coordinates from 1 to n inclusive for small cubes positions. The small cubes occupy integer coordinates (x,y,z) with 1 ≤ x,y,z ≤ n. The surfaces: The outer surfaces are at x=1, x=n, y=1, y=n, z=1, z=n. When we mark a surface, we indicate a specific unit square on that face, corresponding to coordinates on that face's plane. Then we drill a hole that goes from that marked face straight through the cube to the opposite face, perpendicular to that face. So it's like a tunnel that goes through the entire cube, along a line parallel to the axis perpendicular to the face. For example, if mark is on xy-plane at (a,b), that means the face at z=something? Wait: The coordinate axes: (x,y,z) = (1,1,1) is lower left cube. Usually, the cube occupies coordinates from 1 to n. The xy-plane mark: Which face? Possibly the face where z = n? Or maybe the face where z = 1? Need to interpret.
The example figure (not given but described). In the problem statement: "An example where n=4 is shown in the following figure. Then, as shown in the figure above (right), make a hole that penetrates horizontally or vertically from the marked surface to the opposite surface."
Likely: There are marks on three faces: one on the top (xy plane at z=n) maybe? But let's read: They give coordinates axes: (x,y,z) = (1,1,1) is lower left cube, and (x,y,z) = (n,n,n) is upper right cube. So presumably the cube sits with axes: x increases to the right, y increases depth? z increases upward? Could be standard.
Now they define marks with ci indicating plane: "xy", "xz", "yz". And ai, bi are coordinates on that plane. For example, in above figure, marks A, B, C are: "xy 4 4", "xz 1 2", "yz 2 3". We need to visualize.
We have n=4. Mark A: xy plane at (4,4). So that's the top? If xy-plane corresponds to the face where z = n? Actually the xy-plane (z constant) could be either the bottom face (z=1) or top face (z=n). But they said "marked surface", meaning outer surface of cube. There are two faces perpendicular to z-axis: one at z=1 (bottom) and one at z=n (top). When they say "xy" plane, they probably mean the face orthogonal to z-axis, i.e., the face where either z=1 or z=n. But which one? The hole penetrates from the marked surface to the opposite surface. So if you mark on the xy-plane, you drill a hole that goes from that marked face straight through the cube to the opposite face along the z-direction. So which of the two faces is considered the "marked surface"? The mark is placed on a specific face: the plane xy could be either the front/back? Actually there are two xy faces: one at z=0? No coordinates 1..n, so faces are at z=1 and z=n. Which one is referred to as "xy"? Usually in such problems, they refer to the faces by their orientation: "xy" means the face orthogonal to z, but we need to know whether they mean the face at z=n (top) or at z=1 (bottom)? Similarly for xz (orthogonal to y) there are two faces: y=1 and y=n. yz (orthogonal to x): x=1 and x=n.
Given that in example, mark A: "xy 4 4". That likely corresponds to the top face (z=n) because (4,4) would be the far corner? Possibly the figure shows a cube with a mark on the top face at the far corner. Similarly, B: "xz 1 2". xz plane: which face? If xz is orthogonal to y, there are two: y=1 and y=n. "1 2" maybe the coordinates (x,z) = (1,2) on the front face (y=1) or back. C: "yz 2 3" on yz plane (orthogonal to x). Could be left face (x=1) or right face (x=n). Need to deduce from output.
Given example dataset 1:
n=4, h=3
xy 4 4
xz 1 2
yz 2 3
Output: 52
What is total number of small cubes? n^3 = 64. So cubes with no holes = 52, thus cubes with holes (i.e., intersected by at least one tunnel) = 12. So these three holes intersect a total of 12 small cubes (maybe some overlap counted once? Actually we need cubes with no holes, meaning cubes that are not pierced by any hole. So the number of cubes that are pierced by one or more holes is 64-52=12. So three holes intersect 12 cubes total, possibly overlapping.
We need to model the holes. Each hole is a straight line through the cube from one face to the opposite face, passing through a specific unit square on the face. That unit square corresponds to a specific unit area on the surface. Since the cube is made of unit cubes, each hole will pass through a column of unit cubes that line up along that direction. But careful: The hole is a line? Or is it a rectangular tunnel? The phrase "make a hole that penetrates horizontally or vertically from the marked surface to the opposite surface." In the figure (probably), they show a square hole that goes through? Wait, "make a hole" might be a square tunnel that goes all the way through, like a drill that removes all material along a straight path, but what is its cross-section? Typically, in such problems (like "hole" in a cube of unit cubes), the hole is of size 1x1 across, i.e., it removes a whole line of small cubes along the direction. However, the phrasing "penetrates horizontally or vertically" suggests that the hole goes straight through, and the mark is on a face, specifying a particular small square on that face. So the hole is like a tunnel of cross-section exactly one unit square, from that face to the opposite face, aligned with the grid. So it will remove an entire line of n small cubes along that axis. However, careful: The small cubes have marks on their surfaces. The mark is placed on the surface of a small cube? Actually, the description: "Small cubes have marks on their surfaces. An example where n = 4 is shown in the following figure." So each small cube has marks? That seems odd. Let's re-read: "There is a cube which consists of n × n × n small cubes. Small cubes have marks on their surfaces. An example where n = 4 is shown in the following figure." Wait, maybe they mean that marks are placed on the surfaces of the big cube? Actually the figure likely shows marks on the outer surfaces. The next sentence: "Then, as shown in the figure above (right), make a hole that penetrates horizontally or vertically from the marked surface to the opposite surface." So they put marks on the surfaces, then drill holes. So the marks are on the surfaces of the big cube. So each mark corresponds to a unit square on a face. The hole is drilled from that marked square all the way through to the opposite face, along a straight line perpendicular to that face. So it's essentially a straight tunnel of cross-section 1x1 (a unit square) that goes through the entire cube. This would remove all small cubes that intersect the tunnel. But note: The tunnel goes through multiple small cubes. Since the tunnel is exactly aligned with the grid and is the width of one small cube, it will pass through exactly n small cubes (one per layer) along that direction, but careful: Actually if the tunnel is a 1x1 square cross-section, then the set of small cubes that are completely inside the tunnel (or intersected) would be those whose interior is removed. But counting "cubes with no holes" means we count small cubes that are not pierced by any hole. So if a small cube is intersected by a hole (i.e., the tunnel passes through it), it's considered perforated. So we need to determine which unit cubes (with integer coordinates) are intersected by any such tunnel.
If the tunnel is exactly a line of unit cross-section aligned with the axis, then the cubes that are pierced are those whose coordinates satisfy certain equalities. For example, a hole from the xy-plane: If the mark is on the face where? Let's define:
Assume that "xy" means the face where z = n (the top face) or maybe where z = 1? Which one? The opposite surface would be the other face orthogonal to z. So if the marked surface is the face with z = something, the hole goes to the opposite face (the other z-face). If we mark on xy-plane at (a,b), that could correspond to either the top face at z = n, at coordinates (a,b) on that face, or the bottom at z=1. But which one is considered the "xy" plane? The problem description: "ci is a string indicating the plane marked with the i-th. ci is one of "xy", "xz", and "yz", indicating that the i-th mark is on the xy, xz, and yz planes, respectively. ai and bi indicate the coordinates on the plane indicated by ci. For the xy, xz, and yz planes, ai and bi indicate the plane coordinates (x, y), (x, z), and (y, z), respectively."
It does not specify which of the two opposite faces (e.g., front vs back) the mark is on. However, the figure likely shows marks on three visible faces: top (xy), front (xz?), and side (yz?). In typical 3D representation, you might see three faces: the top (xy plane at z=n), the front (maybe xz plane at y=1?), and the right side (yz plane at x=n). Let's guess: The example: marks: A: xy 4 4 (top right corner), B: xz 1 2 (maybe front left something), C: yz 2 3 (side). Then the holes are drilled from those marked faces through to the opposite faces.
But we need to determine the coordinates of cubes that are pierced.
We can approach by considering the holes as sets of unit cubes. If we drill from the face at, say, x=1? or x=n? Need to figure out consistent mapping.
Observation: When you drill from a marked face to the opposite face, you will remove a line of cubes that all share the same coordinates on the marked plane. For example, if you mark the xy-plane at (a,b), and if that corresponds to the face where z = n (top), then the hole will go downward (decreasing z) to the bottom face (z=1). So the hole will pass through all cubes with x = a, y = b, and z ranging from 1 to n (or maybe from 1 to n? Actually the cubes are at integer coordinates. The cube at (a,b,z) for each z from 1 to n will be intersected. But if the hole originates from the top face, the cube at (a,b,n) has its top face marked, so that cube is definitely pierced. Similarly, the cube at (a,b,1) is at the opposite face. So indeed, the set of cubes is {(a,b,z) | 1 ≤ z ≤ n}. That's n cubes.
Alternatively, if the mark is on the xy-plane but on the bottom face (z=1), the hole goes upward to the top face (z=n), and the set would be the same: (a,b,z) for z=1..n. So it's independent of which face, as long as we consider the hole goes through the entire thickness. So the set of pierced cubes for an xy mark is always all cubes with that x and y coordinate. But careful: Are there two xy faces? The mark could be on either, but the hole always goes from the marked surface to the opposite surface. So yes, regardless of which of the two opposite faces is marked, the hole goes through the whole cube along the z-direction, affecting the column of cubes with that fixed (x,y). So for a given (a,b) on the xy-plane, the hole will pierce all cubes with x=a, y=b, for z from 1 to n.
Similarly, for an xz mark (plane orthogonal to y), the hole will go through all cubes with fixed (x,z) and y from 1 to n. So set: {(x=a, y=?, z=b) | 1 ≤ y ≤ n}.
For a yz mark (plane orthogonal to x), the hole will go through all cubes with fixed (y,z) and x from 1 to n. Set: {(x=?, y=a, z=b) | 1 ≤ x ≤ n}.
But wait: For xz mark, they give ai and bi as coordinates on the xz plane, i.e., (x,z). So a is x coordinate, b is z coordinate? In description: "For the xy, xz, and yz planes, ai and bi indicate the plane coordinates (x, y), (x, z), and (y, z), respectively." So yes: For xz: (ai, bi) = (x, z). For yz: (ai, bi) = (y, z). For xy: (ai, bi) = (x, y).
Thus, each mark defines a line of cubes: a whole column through the cube along the missing axis.
Given this, the problem reduces to: We have an n x n x n grid of unit cubes. For each mark, we remove all cubes that lie on a specific axis-aligned line (a "line" of n cubes). However, multiple marks may remove the same cubes (overlap). We need to count cubes that are not removed by any mark. So number of cubes left = total cubes (n^3) minus number of distinct cubes that lie on any of these lines.
So the problem seems simple: For each dataset, given h lines, each specifying a line (with fixed two coordinates, variable the third), we need to count the union of the sets of cubes. Since n ≤ 500, total cubes up to 125 million, which is large but could be represented as a boolean 3D array of size 500^3 = 125e6, which is 125 million bytes if using 1 bit? Actually 125 million bytes = 125 MB, borderline but possibly acceptable? However, typical memory limits in such problems may be around 256 MB, so 125 MB boolean array (using char) could be okay, but we need to consider multiple datasets and up to n=500. Also we need to process up to maybe several datasets. 125 MB of bool (uint8) is okay, but we need to be efficient. However, we can do better by using sets or bitsets? Actually we can compute the number of distinct cubes removed without a full 3D array by using inclusion-exclusion? But the lines overlap in a structured way: each line is a set of n cubes along an axis. Overlaps between lines: two lines may intersect at at most one cube if they are not parallel, or possibly more if they are parallel and same line? But note: lines are along axes; they are either x-parallel, y-parallel, or z-parallel? Actually each line is along one axis direction: xy mark gives line along z (vary z). xz mark gives line along y (vary y). yz mark gives line along x (vary x). So they are axis-aligned lines: each is a "line" parallel to one axis. So they are essentially "lines" in the grid.
Overlap between two such lines: If they are parallel (both along same axis), they can either be the same line (if they have the same fixed coordinates) or be disjoint (if different fixed coordinates) because they are parallel and in a grid, two distinct lines parallel to the same axis do not intersect (they are like disjoint columns). If they are along different axes, they may intersect at at most one point (one cube) if the fixed coordinates match appropriately. For example, an xy line with fixed (x0, y0) (varying z) and an xz line with fixed (x1, z1) (varying y) intersect if x0 = x1 and y0 = (the varying y) must equal? Actually intersection cube coordinates (x, y, z) must satisfy x = x0 (from xy), y = free? Wait: xy line: (x0, y0, z) for all z. xz line: (x1, y, z1) for all y. Intersection: need x0 = x1, y0 = y, and z = z1. So yes, if x0 = x1 and y0 is within 1..n and z1 within 1..n, then there is exactly one cube: (x0, y0, z1) provided both lines exist. So intersection of two non-parallel lines is at most one cube.
Three lines (all three different axes) can intersect at one cube if coordinates match accordingly.
Thus, union size can be computed using principle of inclusion-exclusion: total removed = sum of sizes of lines (each n) - sum of pairwise intersections + sum of triple intersections.
But careful: Overlap of two lines could be more than one cube if they are the same line? But are two marks ever the same? Possibly duplicate marks? The input may contain duplicate marks? Not specified, but could happen? Possibly we should handle duplicates. But we can treat duplicates as same line? However, the union should count each cube only once. If two identical marks are given (same ci and same ai,bi), then they represent the same line, so the line's cubes would be added twice if we sum sizes naively, but the intersection of two identical lines would be the whole line (size n) not just single cubes? Because if both lines are the same (parallel, same fixed coordinates), they are not distinct lines? In set terms, it's the same set. But if we treat them as separate sets, the intersection of identical sets is the set itself, so inclusion-exclusion would need to account for that. But it's simpler to first deduplicate marks: keep each distinct line only once. Since holes are drilled based on marks; if you drill the same hole twice, it's still the same set of cubes removed. So we should treat duplicates as one line. But is it allowed? The problem statement: "h is an integer indicating the number of marks. The h lines that follow enter the positions of the h marks." There's no guarantee marks are distinct. They could be repeated? Possibly not, but to be safe we can deduplicate.
However, even without deduplication, we could still compute union using set union of lines. But perhaps it's simpler to use a boolean 3D array to mark pierced cubes, but with n up to 500, n^3=125e6, which is okay memory wise (125 MB) if using bits (1 bit per cube) would be ~16 MB. Actually 125e6 bits = about 15.6 MB, which is good. But we need to store as bitset. However, implementing a 3D bitset of size 500^3 bits is doable. But we need to be careful with time: For each hole, we need to mark n cubes. With h ≤ 200, total marking operations up to 200 * 500 = 100,000, which is trivial. And after marking, we count unmarked cubes: we need to iterate over all n^3 cubes? That's up to 125 million, which is okay in C++ within time (maybe 0.5 sec? Actually 125 million increments is okay if optimized). But there could be multiple datasets, but total input size small; worst-case maybe 10 datasets? That's 1.25 billion operations, maybe borderline but still possible in C++ with fast loops and compiler optimizations. But we can do smarter counting: Since we know total cubes and the number of distinct cubes removed, we can compute unmarked = n^3 - |union|. So we don't need to iterate over all cubes if we can compute union size efficiently.
But computing union size via inclusion-exclusion with dedup may be easier: total distinct removed cubes = size of union of lines (sets of size n). Since lines are axis-aligned, we can compute using sets of lines and overlaps. However, duplicates cause double counting. So we can deduplicate lines: For each type (xy, xz, yz), we maintain a set of fixed coordinate pairs. For xy lines: set of (x,y). For xz: set of (x,z). For yz: set of (y,z). Then the union size = sum_{lines in X} n - sum_{overlaps between lines of different types} 1 + sum_{triple overlaps where all three lines coincide} 1? Actually careful: The principle of inclusion-exclusion for three families of sets? Actually we have multiple sets, but they are not arbitrary; they are lines. But we can compute union as: total = |∪{L in L_xy} L| + |∪{L in L_xz} L| + |∪_{L in L_yz} L|, but they overlap across families. However, it's easier: The union of all lines = the set of all cubes that lie on at least one of these axis-aligned lines.
We can compute it by counting cubes that satisfy: either (x,y) in S_xy, or (x,z) in S_xz, or (y,z) in S_yz, where S_xy is set of (x,y) pairs from xy marks, etc. And we need to count number of integer triples (x,y,z) in [1,n]^3 satisfying at least one of these conditions.
We can compute total cubes = n^3, subtract cubes that satisfy none. But cubes that satisfy none are those with (x,y) not in S_xy, (x,z) not in S_xz, and (y,z) not in S_yz. So number of safe cubes = count of (x,y,z) such that all three pairs are absent from respective sets. That's more complicated.
Alternatively, compute number of cubes removed = |A|, where A = { (x,y,z) : (x,y) in S_xy or (x,z) in S_xz or (y,z) in S_yz }.
We can compute |A| via inclusion-exclusion:
|A| = |A_xy ∪ A_xz ∪ A_yz|, where A_xy = { (x,y,z) : (x,y) in S_xy } (i.e., all cubes with that (x,y) pair). Similarly, A_xz = { (x,y,z) : (x,z) in S_xz }, A_yz = { (x,y,z) : (y,z) in S_yz }.
Now these are sets of cubes.
By inclusion-exclusion:
|A| = |A_xy| + |A_xz| + |A_yz| - |A_xy ∩ A_xz| - |A_xy ∩ A_yz| - |A_xz ∩ A_yz| + |A_xy ∩ A_xz ∩ A_yz|.
Now compute each term.
First, |A_xy|: For each (x,y) in S_xy, the line includes all z from 1 to n, so |A_xy| = |S_xy| * n.
Similarly, |A_xz| = |S_xz| * n, |A_yz| = |S_yz| * n.
Now intersections:
A_xy ∩ A_xz: cubes that satisfy both (x,y) in S_xy and (x,z) in S_xz. That means there exists a mark xy with (x,y) and a mark xz with (x,z) (could be same x). For a particular cube (x,y,z) to be in the intersection, it must be that (x,y) is in S_xy and (x,z) is in S_xz. Note that it's not necessary that the marks are distinct; the sets are defined by existence of at least one mark with those coordinates. So the intersection size equals number of triples (x,y,z) such that (x,y)∈S_xy and (x,z)∈S_xz. This can be computed as sum over x of ( number of y such that (x,y)∈S_xy ) * ( number of z such that (x,z)∈S_xz ). Because for a given x, the conditions involve y and z independently. So:
|A_xy ∩ A_xz| = Σ_{x=1..n} ( count_xy[x] * count_xz[x] ), where count_xy[x] = number of distinct y such that (x,y)∈S_xy; count_xz[x] = number of distinct z such that (x,z)∈S_xz.
Note: This counts each cube (x,y,z) exactly once if both conditions hold. Indeed, for fixed x, y must be in set of y's paired with x in S_xy, and z must be in set of z's paired with x in S_xz. So product gives number of (y,z) combinations. So sum over x.
Similarly, A_xy ∩ A_yz: cubes satisfying (x,y)∈S_xy and (y,z)∈S_yz. For each y, count number of x such that (x,y)∈S_xy (call count_xy_rev[y]?) and number of z such that (y,z)∈S_yz. So:
|A_xy ∩ A_yz| = Σ_{y=1..n} ( count_xy_y[y] * count_yz_y[y] ), where count_xy_y[y] = number of distinct x with (x,y)∈S_xy, and count_yz_y[y] = number of distinct z with (y,z)∈S_yz.
Similarly, A_xz ∩ A_yz: cubes satisfying (x,z)∈S_xz and (y,z)∈S_yz. For each z, count number of x such that (x,z)∈S_xz, and number of y such that (y,z)∈S_yz. So:
|A_xz ∩ A_yz| = Σ_{z=1..n} ( count_xz_z[z] * count_yz_z[z] ), where count_xz_z[z] = number of distinct x with (x,z)∈S_xz, count_yz_z[z] = number of distinct y with (y,z)∈S_yz.
Now the triple intersection: A_xy ∩ A_xz ∩ A_yz: cubes satisfying (x,y)∈S_xy, (x,z)∈S_xz, (y,z)∈S_yz simultaneously. This is equivalent to existence of marks for those pairs. The set of cubes is those (x,y,z) such that (x,y)∈S_xy, (x,z)∈S_xz, (y,z)∈S_yz. We can count this by iterating over all possible (x,y,z) but that's O(n^3) potentially large. But we can compute more efficiently: Since S_xy, S_xz, S_yz are sets of size up to h each (h ≤ 200). The triple intersection size is limited because it requires all three pairs to be marked. The number of possible triples that could satisfy all three is at most min(|S_xy|*n? Actually each triple must correspond to a combination where all three pairs are present. Since h ≤ 200, the number of possible triples that might satisfy could be bounded by h^3? But more precisely, each mark defines a line; the triple intersection cubes are those that lie at the intersection of three lines, one from each family. They occur when we have an xy line with (x,y), an xz line with (x,z), and a yz line with (y,z). So it's like if there exist marks such that for some (x,y,z), (x,y)∈S_xy, (x,z)∈S_xz, (y,z)∈S_yz. That's essentially the set of "triangles" formed by marks.
Given that h ≤ 200, we can simply iterate over all combinations of an xy mark, an xz mark, and a yz mark, and check if they are consistent (i.e., same x for xy and xz, same y for xy and yz, same z for xz and yz). But careful: The triple intersection condition is not that there exists a mark for each pair; it's that the three sets themselves contain these pairs. Since the sets are not necessarily from distinct marks? Actually A_xy is defined by existence of at least one xy mark with that (x,y). So if there are multiple marks, the condition is just that the pair is present. So for triple intersection, we need (x,y) present in S_xy, (x,z) present in S_xz, (y,z) present in S_yz. So for a given triple (x,y,z), it is included if all three pairs are present. So we can count by enumerating all triples formed by combining marks? But note: There could be multiple marks giving same pair; we just need presence. So we can precompute sets of pairs.
Because n can be up to 500 but h is small, the triple intersection size can be computed by iterating over all pairs from S_xy and S_xz that share the same x, then checking if (y,z) is in S_yz. Alternatively, we can iterate over all possible x from 1 to n? But n up to 500, and each count of marks per x is small. But given h is small, a triple nested loop over marks (max 200 each) would be 200^3 = 8 million, which is acceptable. However, we must also ensure we don't double count a cube that might be generated from multiple combinations? Actually if multiple marks exist for same pair, the condition is still just presence. But if we iterate over all combinations of marks, we might count the same (x,y,z) multiple times if there are multiple marks giving the same pairs. For example, suppose S_xy has two marks with same (x,y) (duplicate), S_xz has two marks with same (x,z), S_yz has one mark (y,z). Then (x,y,z) qualifies. If we iterate over all combinations (including duplicates), we would count it multiple times (221=4). So we need to count each cube exactly once. So we need to count distinct (x,y,z) triples.
Given h ≤ 200, we can afford to maintain a set of triples (or boolean 3D array) to count triple intersections? But n is up to 500, so we could use a 3D boolean for triple intersection? That would be large. But we can count by iterating over all possible x,y,z? 500^3 =125M, which is large but maybe okay if only done once? But that could be heavy if many datasets. Better to compute using combinatorial counting.
Since h is small, we can compute triple intersection size by iterating over all distinct x that appear in S_xy and S_xz? Actually we can compute it by:
For each x in 1..n? n=500, which is small-ish. We could iterate x from 1 to n, then for each x, consider all y such that (x,y)∈S_xy, all z such that (x,z)∈S_xz, and then check if (y,z)∈S_yz. That would be sum over x of (deg_xy(x) * deg_xz(x)), where deg_xy(x) = number of y with (x,y) in S_xy, deg_xz(x) similarly. For each (y,z) combination, we need to check if (y,z)∈S_yz. So we can pre-store S_yz as a set of pairs (maybe boolean 2D array 500x500). Since n=500, 500x500=250k, which is fine. So we can do:
Initialize a boolean matrix yz_present[501][501] = false. For each (y,z) in S_yz, set true.
Then triple_count = 0.
For x from 1 to n:
for each y in list_xy[x] (i.e., all y such that (x,y) in S_xy):
for each z in list_xz[x] (i.e., all z such that (x,z) in S_xz):
if yz_present[y][z] then triple_count++.
This will count each triple (x,y,z) exactly once because we iterate over x and check combination. The total number of iterations is sum over x of (|list_xy[x]| * |list_xz[x]|). Since total pairs in S_xy is |S_xy| ≤ h (≤200), and similarly S_xz ≤ h, the sum over x of product could be up to? Worst-case if all xy pairs have same x, and all xz pairs have same x, then product could be up to 200*200=40000 for that x, but other x have 0. So max 40000 per x, but h=200, so maximum product sum is at most? The total number of (y,z) combinations across all x is at most |S_xy| * |S_xz| = up to 40000. So triple_count computation is at most 40000 checks, trivial.
But careful: The triple intersection may also be counted via other combinations? Our method counts each (x,y,z) exactly once because we fix x. Since x is part of the triple, no duplicates. So triple_count is correct.
Thus, we can compute union size using inclusion-exclusion with deduplicated sets.
Now need to check with the example to see if that yields 64-52=12 removed cubes.
Example 1: n=4, marks: xy 4 4; xz 1 2; yz 2 3.
So S_xy = {(4,4)} size 1
S_xz = {(1,2)} size 1
S_yz = {(2,3)} size 1
Compute:
|A_xy| = 1 * 4 = 4
|A_xz| = 1 * 4 = 4
|A_yz| = 1 * 4 = 4
Sum = 12
Pairwise intersections:
A_xy ∩ A_xz: Need (x,y) in S_xy and (x,z) in S_xz. Here S_xy has x=4,y=4; S_xz has x=1,z=2. No common x, so intersection empty? Actually we need a cube (x,y,z) such that (x,y)∈S_xy => x=4,y=4; and (x,z)∈S_xz => x=1, z=2. x can't be both 4 and 1, so no intersection. So |A_xy∩A_xz|=0.
Similarly, A_xy ∩ A_yz: S_xy: (4,4). S_yz: (2,3). Condition: (x,y)=(4,4) and (y,z)=(2,3) => need y=4 from first, y=2 from second, conflict => 0.
A_xz ∩ A_yz: S_xz: (1,2). S_yz: (2,3). Condition: (x,z)=(1,2) and (y,z)=(2,3) => need z=2 from first, z=3 from second, conflict => 0.
Triple intersection: need all three: (4,4) (1,2) (2,3) => impossible because x conflict etc => 0.
Thus union size = 12. So removed cubes = 12, safe = 64-12=52. That matches output. Good.
Second example:
4 5
xy 1 1
xy 3 3
xz 3 3
yz 2 1
yz 3 3
Output: 46. So total cubes = 64, removed = 18.
Let's test with our method.
Marks:
S_xy: (1,1), (3,3) -> size 2
S_xz: (3,3) -> size 1
S_yz: (2,1), (3,3) -> size 2
Compute:
|A_xy| = 24 = 8
|A_xz| = 14 = 4
|A_yz| = 2*4 = 8
Sum = 20
Now pairwise intersections:
Compute count_xy per x: For each x, list of y.
x=1: y=1
x=3: y=3
others: none.
So count_xy[x] = number of y for that x: x=1:1, x=3:1.
Similarly, S_xz: (3,3) => x=3, z=3. So count_xz[x]: x=3:1; others 0.
Now A_xy ∩ A_xz: sum over x of count_xy[x]count_xz[x] = for x=1:10=0; x=2:0; x=3:1*1=1; x=4:0 => total 1. So |A_xy∩A_xz| = 1.
Which cube is that? x=3, y must be from S_xy for x=3: y=3; z must be from S_xz for x=3: z=3. So cube (3,3,3). Indeed, that cube is on both lines.
Now A_xy ∩ A_yz: Need for each y, count of x from S_xy with that y, and count of z from S_yz with that y.
First, compute for each y: from S_xy: y=1 -> x=1; y=3 -> x=3. So count_xy_y[y]: y=1:1; y=3:1.
From S_yz: (2,1) -> y=2, z=1; (3,3) -> y=3, z=3. So count_yz_y[y]: y=2:1 (z=1), y=3:1 (z=3), y=1:0.
Now sum over y: count_xy_y[y] * count_yz_y[y] = y=1:10=0; y=2:01=0; y=3:1*1=1; y=4:0 => total 1. So |A_xy∩A_yz| = 1. Cube: y=3, from xy: x=3, from yz: z=3 => (3,3,3). So again (3,3,3).
Now A_xz ∩ A_yz: For each z, count of x from S_xz, count of y from S_yz.
From S_xz: (3,3) => z=3: x=3 => count_xz_z[3]=1.
From S_yz: (2,1) => z=1: y=2; (3,3) => z=3: y=3 => count_yz_z[1]=1, count_yz_z[3]=1.
Sum over z: for z=1: 01=0 (since count_xz_z[1]=0), z=3: 11=1 => total 1. So |A_xz∩A_yz| = 1. Cube: z=3, from xz: x=3, from yz: y=3 => (3,3,3).
Now triple intersection: need (x,y) in S_xy, (x,z) in S_xz, (y,z) in S_yz. Let's compute using method: For each x: from S_xy list of y, from S_xz list of z, check yz_present[y][z]. We'll compute.
Set yz_present: (2,1) and (3,3) as true.
For x=1: S_xy gives y=1, S_xz gives no z => skip.
x=2: S_xy none, skip.
x=3: S_xy gives y=3, S_xz gives z=3. Check yz_present[3][3]? (3,3) is present. So triple_count = 1. (Also maybe other combos? x=3 only one y and one z). So triple intersection size = 1.
Now inclusion-exclusion:
|A| = (8+4+8) - (1+1+1) + (1) = 20 - 3 + 1 = 18. So removed = 18, safe = 64-18=46. Matches.
Thus the formula works.
Now we must consider duplicates: If there are duplicate marks, we should deduplicate to avoid overcounting in the set sizes. In the inclusion-exclusion, we used S_xy, S_xz, S_yz as sets of unique pairs. If we included duplicates, |A_xy| would be larger (since each duplicate would add n again). But the actual union does not increase. So we must deduplicate.
Thus algorithm per dataset:
-
Read n, h.
-
If both 0, exit.
-
Initialize:
S_xy = set of (x,y)
S_xz = set of (x,z)
S_yz = set of (y,z)
-
For each of h lines: read ci, a, b. Based on ci:
if "xy": insert (a,b) into S_xy
if "xz": insert (a,b) into S_xz
if "yz": insert (a,b) into S_yz
-
Now compute sizes: |S_xy|, |S_xz|, |S_yz|.
-
Compute |A_xy| = |S_xy| * n
-
|A_xz| = |S_xz| * n
-
|A_yz| = |S_yz| * n
-
Compute pairwise intersections:
For xy∩xz: Need for each x, count_xy[x] = number of distinct y such that (x,y) in S_xy. Similarly count_xz[x] = number of distinct z such that (x,z) in S_xz.
Compute sum_{x=1..n} count_xy[x] * count_xz[x].
For xy∩yz: For each y, count_xy_y[y] = number of distinct x such that (x,y) in S_xy. count_yz_y[y] = number of distinct z such that (y,z) in S_yz. Sum over y.
For xz∩yz: For each z, count_xz_z[z] = number of distinct x such that (x,z) in S_xz. count_yz_z[z] = number of distinct y such that (y,z) in S_yz. Sum over z.
-
Compute triple intersection:
Option 1: Use the method described: Build yz_present as boolean matrix size (n+1)x(n+1) (1-indexed). Then triple_count = 0.
For each x from 1 to n:
if count_xy[x] > 0 and count_xz[x] > 0:
for each y in list_xy[x] (we can store list per x):
for each z in list_xz[x]:
if yz_present[y][z] then triple_count++.
Alternatively, we can generate triple intersection by iterating over all combinations of marks from S_xy and S_xz that share x, then check yz. But our method with per x lists is efficient.
Note: We must be careful that we don't double count the same (x,y,z) from multiple x because x is fixed. However, what if there are multiple y values for same x and multiple z values? The loops will generate all combos, but each distinct (y,z) yields a distinct cube? Actually for given x, each (y,z) combination with y from list_xy[x] and z from list_xz[x] gives a distinct cube because y and z vary. So no duplicates.
But what if the same cube can be obtained from two different x? That's impossible because cube has a unique x coordinate. So fine.
Thus triple_count computed as above.
-
Then union size = (|A_xy|+|A_xz|+|A_yz|) - (sum of pairwise intersections) + triple_count.
-
Removed = union size.
-
Answer = n^3 - removed.
Edge Cases: Overlaps between lines of same type are already accounted? Actually lines of same type (e.g., two xy lines) are disjoint unless they are exactly the same (x,y) pair. If they are distinct (x,y), they are disjoint because they vary z, and at fixed (x,y) each covers all z, but different (x,y) sets are disjoint (since they have different (x,y) coordinates). However, two distinct xy lines can overlap if they share the same (x,y) pair, but we deduplicate, so they are same line. So disjointness holds for distinct lines. So pairwise intersections between lines of same type are zero unless they are same line, but we treat sets as unique, so no intersection between distinct lines of same type. In inclusion-exclusion we didn't subtract intersections of same-type lines because we treat them as separate sets? Actually we are using inclusion-exclusion over the three families A_xy, A_xz, A_yz where each A_xy is the union of all xy lines (i.e., all cubes with (x,y) in S_xy). Similarly A_xz, A_yz. We are not treating each line as a separate set; we treat each family as a set (the union of all lines of that type). So intersection between A_xy and A_xz is as computed. Within A_xy itself, it's a union already. So we don't need to consider intersections of two xy lines because they are already merged. That's correct.
But note: In inclusion-exclusion across three sets, we need to compute pairwise intersections like A_xy ∩ A_xz, which we did. However, there is also potential intersection within A_xy (i.e., overlapping of two different xy lines) but since they are disjoint (different (x,y)), they don't overlap, so union size of xy lines is sum of sizes. That's fine. If there were duplicate lines, the union would still be sum of distinct lines, so we deduplicate.
Thus our calculation is correct.
Now we should verify that the formula holds even if there are multiple marks causing overlapping of lines within a family that are not disjoint? Actually two different xy lines are defined by different (x,y) pairs. The cubes covered by one line are those with that specific (x,y) and all z. Another line with different (x,y) covers cubes with that other (x,y) and all z. These two sets are disjoint because they differ in either x or y coordinate, so no cube can have two different (x,y) simultaneously. So they are disjoint. So the union of xy lines is exactly sum of n per distinct (x,y). Good.
Similarly for other types.
Thus the problem reduces to the above counting.
Now we need to confirm with more cases and potential pitfalls.
One pitfall: The coordinate values ai, bi are presumably between 1 and n inclusive. Input constraints: n ≤ 500, so coordinates valid.
We must also consider that marks may be given on same plane but could be on opposite faces? Actually our interpretation that the hole goes through entire cube regardless of which of the two faces is marked is inherent because the hole penetrates from marked surface to opposite surface. However, what if both faces of the same orientation are marked? For example, a mark on xy-plane at (x,y) could be on the top face, and another mark on xy-plane at (x,y) could be on the bottom face? But they are both on the same plane "xy"? The problem doesn't differentiate which of the two faces; it just says "xy" plane. Does that mean that a mark on "xy" always refers to the same face? Possibly "xy" means the face where z = n (or z = 1). Usually in such coordinate representation, they define the faces as the three visible ones: maybe the "xy" plane is the top face (z = n). However, it might not matter because the hole always goes to the opposite face, so the line of cubes is the same. But if they consider "xy" as a specific face (say top), then a mark on bottom face would be considered a different plane? Actually bottom face is also an xy-plane (z=1). But they only list three planes: xy, xz, yz. So they are likely referring to the three visible faces on the diagram, which are all on the "positive" sides: maybe the faces at x=n, y=n, z=n? Or the ones shown in figure: They show a cube with three marked faces visible: top, front, right side. Usually in such problems, they mark on the three faces that are visible, and the hole goes through to the opposite hidden faces. So they consider the three planes: xy (top), xz (front), yz (right). The opposite faces would be bottom, back, left respectively. So the marks are always on the "positive" faces (the ones with maximum coordinate). But does the input guarantee that? Not necessarily; it might be that a mark could be on any of the six faces, but they only categorize by plane orientation, not by which side. However, they give only three strings: xy, xz, yz, not specifying which side. In typical problems, they often consider that the mark is on the visible face (the one with higher coordinate), and the hole goes to the opposite face. But the description: "ci is a string indicating the plane marked with the i-th. ci is one of "xy", "xz", and "yz", indicating that the i-th mark is on the xy, xz, and yz planes, respectively." This suggests that there are only three planes considered, but a cube has six faces, so they must be specifying which face by the plane orientation, but not which side. Possibly they assume that the mark is on the face that is visible in the figure, i.e., the faces with the outward normal in positive direction? However, the coordinate axes: (1,1,1) lower left, (n,n,n) upper right. In such orientation, the faces are: x=1 (left), x=n (right), y=1 (front? maybe?), y=n (back), z=1 (bottom), z=n (top). They might define the planes: xy-plane could be the one where z = n (top) or z = 1 (bottom)? Which one is usually called "xy plane" in 3D? In coordinate geometry, the xy-plane is where z=0, but here we have coordinates 1..n. They might be using "xy" to refer to the face that is orthogonal to the z-axis, i.e., the two faces with constant z. But they didn't differentiate. However, earlier they gave an example: marks A, B, C are "xy 4 4", "xz 1 2", "yz 2 3". In the figure, we can guess which faces are marked. Possibly A is on the top face at far corner (x=4,y=4), B on the front face (x=1,z=2) (maybe left bottom front?), C on the right side (y=2,z=3). That seems plausible: top face (xy) with coordinates (4,4) (the far corner?), front face (xz) with x=1 (left) and z=2 (mid height), right face (yz) with y=2 (some depth) and z=3. So that matches marking on the three faces: top (z=n), front (y=1), right (x=n). But the coordinates: For front face, y=1, but they gave "xz 1 2". So that would be plane xz, which is orthogonal to y. If front face is y=1, then the coordinates on that face are (x,z). So (1,2) indicates the leftmost bottom? Actually x=1, z=2. That makes sense. For right face, x=n=4, but they gave "yz 2 3" with coordinates (y,z) = (2,3). So if right face is x=n, that would be consistent.
Thus, it seems they assume that:
- "xy" refers to the face where z = n (the top face)
- "xz" refers to the face where y = 1 (the front face)
- "yz" refers to the face where x = n (the right face)
But is that always the case? Let's check the coordinate axes: They said (x,y,z) = (1,1,1) is lower left cube, and (n,n,n) is upper right cube. So typically, if we view the cube from a front-right-top perspective, the visible faces are: the face with x = n (right side), the face with y = 1 (front), and the face with z = n (top). So those are the three faces visible. So likely the marks are placed on these visible faces. The problem mentions "the plane indicated by ci", but in the figure, there are three marked surfaces A, B, C which are on those three faces. So indeed, the planes "xy", "xz", "yz" correspond to the top, front, and right faces respectively. So the hole then goes from that face to the opposite face: For xy (top), hole goes vertically down to bottom face (z=1). For xz (front), hole goes back to rear face (y=n). For yz (right), hole goes left to left face (x=1).
Thus, the set of cubes pierced by an xy mark (a,b) is: all cubes with x=a, y=b, z from 1 to n. That matches earlier interpretation.
For an xz mark (a,b): (x,z) = (a,b). The hole goes from front face (y=1) to back face (y=n). So cubes pierced: all cubes with x=a, z=b, y from 1 to n.
For yz mark (a,b): (y,z) = (a,b). Hole goes from right face (x=n) to left face (x=1). So cubes pierced: all cubes with y=a, z=b, x from 1 to n.
This is exactly what we used.
Now we should double-check with the second example: marks: xy 1 1 (top face at (1,1)), xy 3 3 (top face at (3,3)), xz 3 3 (front face at (3,3)), yz 2 1 (right face at (2,1)), yz 3 3 (right face at (3,3)). That matches our sets.
So our interpretation is consistent.
Thus the solution is straightforward.
Now we need to implement.
But wait: There is a nuance: The hole penetrates "horizontally or vertically". In our interpretation, holes are axis-aligned. That is correct.
Now confirm with other potential scenarios: Could there be a case where a mark is on a face and the hole is drilled from that face to the opposite face, but the hole goes through the cube and might be considered to remove cubes even if the mark is on the opposite side? No, it's consistent.
Now we should ensure we handle large n up to 500, but h up to 200, so sets small.
Implementation details:
-
We'll read n and h, loop until both zero.
-
For each dataset:
- Initialize three dictionaries or arrays to collect unique pairs. Since n ≤ 500, we can use boolean 2D arrays for each pair set: xy[501][501] = false, xz[501][501] = false, yz[501][501] = false. This uses about 3 * 500500 = 750k booleans, which is fine. Actually 500500=250k, times 3 = 750k, each boolean could be 1 byte, so <1 MB. That's efficient.
- Also we may want to store per x lists of y for xy, and per x lists of z for xz, etc. We can compute counts on the fly.
Process each input line: read ci, a, b. Convert a,b to ints. If ci == "xy": if xy[a][b] is false, set true and also increment count_xy_total and add to list_xy[a]. We can maintain arrays:
xy_exists[a][b] = true
list_xy[a].push_back(b)
Similarly for xz: xz_exists[a][b] = true; list_xz[a].push_back(b)
For yz: yz_exists[a][b] = true; list_yz[a].push_back(b) but careful: For yz, the first coordinate is y, second is z. So we might maintain yz_exists[y][z] and per y list of z? For pairwise intersections we need per y count of yz pairs, and per z count of yz pairs. But we can compute from the boolean matrix or lists.
Actually we can compute all counts after reading all marks, by scanning the boolean matrices.
Since n ≤ 500, we can just loop over all x from 1 to n to compute count_xy[x] as number of y with xy[x][y] true. Similarly for xz, yz. But that would be O(n^2) per dataset, which is 250k operations, trivial. So we don't need to maintain per x lists; we can just compute later.
However, for triple intersection we need to iterate over combinations of y and z for each x. Using loops over x and then over y and z where xy[x][y] and xz[x][z] true. We can do that by: for x in 1..n: if count_xy[x] >0 and count_xz[x] >0: then for y in 1..n: if xy[x][y] then for z in 1..n: if xz[x][z] then if yz[y][z] then triple_count++. That would be O(n * (count_xy[x] * count_xz[x]?) Actually scanning all y and z would be O(n^2) per x, which is too much (500500500=125M) possibly okay but may be borderline. But we can optimize: Instead of scanning all y and z, we can generate the lists for each x. That's why we want to store per x the list of y where xy[x][y] is true, and per x the list of z where xz[x][z] is true. Then inner loops are product of list sizes, which sum to at most |S_xy||S_xz| ≤ 200200 = 40000. So it's trivial.
So we should store these lists. Since h ≤ 200, per x lists will be small. We'll maintain vector xy_list[501] (or maybe array of vectors). But we could also after building boolean matrices, we can build lists by scanning over y for each x where xy[x][y] true. That scanning is O(n^2)=250k, still trivial. But then for triple intersection, we would still need to get the y's and z's for each x. We could do: for each x, for y in 1..n: if xy[x][y] then for z in 1..n: if xz[x][z] ... That's O(n^3) if done naively? Actually nested loops: x from 1 to n, y from 1 to n, then inner loop over z from 1 to n? That's 125M per dataset, which might be okay in C++ if optimized? 125 million operations per dataset is maybe okay if few datasets, but could be borderline. However, we can reduce to at most 40k checks using lists. Since it's easy, we'll adopt the list approach.
Now we should also consider if there is any scenario where a hole is not a full line? The description: "make a hole that penetrates horizontally or vertically from the marked surface to the opposite surface." That suggests the hole goes through entirely. So yes.
Now we must be careful about input format: The lines contain ci (string) and two integers ai, bi. There may be extra spaces. Also there may be multiple datasets, terminated by 0 0.
Implementation details:
We'll read using standard input. Possibly use cin >> string >> a >> b. Since h ≤ 200, it's fine.
We'll allocate arrays of size n+1 (since 1-indexed). We'll reset for each dataset.
Because n can be up to 500, and we have to allocate vectors of size 501 each, that's fine.
Now we must ensure we use 64-bit integers for counting because n^3 can be up to 125,000,000 (500^3 = 125,000,000) which fits within 32-bit signed int (max 2.1e9). Actually 125 million is within 32-bit. But sum of sizes may be up to h*n where h=200, n=500 => 100,000; intersections are small; so 32-bit int is sufficient. However, to be safe, we can use long long.
Now confirm constraints: n ≤ 500, h ≤ 200. So max n^3 = 125e6 fits in int. But anyway.
Now produce code.
Pseudo:
while (true) {
int n, h;
cin >> n >> h;
if (n==0 && h==0) break;
// Initialize
bool xy_ex[501][501] = {false};
bool xz_ex[501][501] = {false};
bool yz_ex[501][501] = {false};
vector<int> xy_by_x[501]; // index 1..n
vector<int> xz_by_x[501];
vector<int> yz_by_y[501];
int cnt_xy = 0, cnt_xz = 0, cnt_yz = 0;
for (int i = 0; i < h; ++i) {
string ci;
int a, b;
cin >> ci >> a >> b;
if (ci == "xy") {
if (!xy_ex[a][b]) {
xy_ex[a][b] = true;
xy_by_x[a].push_back(b);
cnt_xy++;
}
} else if (ci == "xz") {
if (!xz_ex[a][b]) {
xz_ex[a][b] = true;
xz_by_x[a].push_back(b);
cnt_xz++;
}
} else if (ci == "yz") {
if (!yz_ex[a][b]) {
yz_ex[a][b] = true;
yz_by_y[a].push_back(b); // note: first coordinate is y
cnt_yz++;
}
}
}
// Compute counts for pairwise intersections
// For xy_xz: already have per x sizes.
long long inter_xy_xz = 0;
for (int x = 1; x <= n; ++x) {
int s_xy = xy_by_x[x].size();
int s_xz = xz_by_x[x].size();
inter_xy_xz += (long long)s_xy * s_xz;
}
// For xy_yz: need cnt_xy_y[y] and yz_by_y[y].size()
int cnt_xy_y[501] = {0};
for (int x = 1; x <= n; ++x) {
for (int y : xy_by_x[x]) {
cnt_xy_y[y]++;
}
}
long long inter_xy_yz = 0;
for (int y = 1; y <= n; ++y) {
int s_xy_y = cnt_xy_y[y];
int s_yz_y = yz_by_y[y].size();
inter_xy_yz += (long long)s_xy_y * s_yz_y;
}
// For xz_yz: need cnt_xz_z[z] and cnt_yz_z[z]
int cnt_xz_z[501] = {0};
for (int x = 1; x <= n; ++x) {
for (int z : xz_by_x[x]) {
cnt_xz_z[z]++;
}
}
int cnt_yz_z[501] = {0};
for (int y = 1; y <= n; ++y) {
for (int z : yz_by_y[y]) {
cnt_yz_z[z]++;
}
}
long long inter_xz_yz = 0;
for (int z = 1; z <= n; ++z) {
inter_xz_yz += (long long)cnt_xz_z[z] * cnt_yz_z[z];
}
// Triple intersection
long long triple = 0;
for (int x = 1; x <= n; ++x) {
if (xy_by_x[x].empty() || xz_by_x[x].empty()) continue;
// For each y in xy_by_x[x], each z in xz_by_x[x], check yz_ex[y][z]
for (int y : xy_by_x[x]) {
for (int z : xz_by_x[x]) {
if (yz_ex[y][z]) {
triple++;
}
}
}
}
long long total_cubes = (long long)n * n * n;
long long removed = (long long)cnt_xy * n + (long long)cnt_xz * n + (long long)cnt_yz * n
- inter_xy_xz - inter_xy_yz - inter_xz_yz
+ triple;
long long answer = total_cubes - removed;
cout << answer << endl;
}
Make sure to reset data structures for each dataset. For vectors, we can declare new each iteration (vector xy_by_x[501];). However, we need to clear them; we can just declare as local arrays of vectors (each vector default empty). That's fine.
But note: In C++, using vector xy_by_x[501]; inside loop will create 501 vectors each time; that's okay. However, we must ensure they are empty. Since they are newly created, they are empty.
Alternatively, we can declare as arrays and clear them manually. But easier to declare inside loop.
But we need to ensure the arrays are sized n+1? We can just size 501 always for simplicity.
One more nuance: For yz_ex, we defined as bool [501][501]. But we only need up to n. So okay.
But we need to initialize them to false. Since they are local variables, we can use memset or fill. Because we cannot initialize with = {false} for 2D array inside loop? In C++ we can do bool yz_ex[501][501] = {}; which zero-initializes. That is acceptable. However, for each dataset we are allocating a large stack array of size 501x501 ~ 251k bools, three such arrays: 3 * 251k = 753k bytes, plus vectors, all on stack, which is fine. But careful: 501x501 = 251001, times 3 = 753003 bytes. That's less than 1 MB. Stack limit is usually several MB, so okay. But if we allocate many datasets, it's recreated each loop.
Alternatively, we could allocate dynamically or use global arrays and reset only used part. But local is simpler.
However, initializing large arrays with = {} each loop may cause overhead, but number of datasets is small. Acceptable.
But we must be careful: The boolean arrays are 2D; we may need to reset them each dataset. Using = {} will zero-initialize, but that's done at declaration, so it's okay. But if we declare inside loop, it's reinitialized each iteration. That's fine.
Now test with sample input.
We already computed that.
Now consider possibility that the marks could be given with coordinates > n? No, they are within 1..n as per problem.
Now confirm that we didn't miss any intersections: What about the intersection of a line with itself? Already handled.
Now check with a case where there are many marks but duplicates: e.g., same xy mark repeated many times. Our dedup ensures we count it once. That should be correct.
Now check potential overflow: n up to 500, n^3 = 125e6, fits int. But we use long long to be safe.
Now we should also ensure that inter_xy_xz etc are long long.
Now check if there could be an off-by-one: Coordinates are 1..n, and the line includes all z from 1 to n. So the number of cubes along the line is n. Good.
Now what about the case where a hole is drilled from a marked surface to the opposite surface, but the marked surface is not necessarily the one we assumed? Could it be that for xz, the marked face is the back face (y=n) rather than front? In our interpretation, we assumed xz mark on front (y=1) and hole goes to back (y=n). However, the problem statement doesn't specify which of the two opposite faces is marked; it just says "marked surface" and then "opposite surface". If the mark is on the back face (y=n) instead, the hole would go to the front face (y=1). That would still be the same set of cubes: all cubes with x=a, z=b, and y from 1 to n. So it doesn't matter which side. So our set remains unchanged. So interpretation holds.
Thus solution stands.
Now we might think about time complexity: For each dataset, reading marks O(h). Then computing per x sizes and intersections O(n + total marks) ~ O(500 + 200) = trivial. Triple intersection loops over sum of products of list sizes, at most 40000. So extremely fast.
Now implement in C++.
Edge Cases to test:
-
n=1, h=1, xy 1 1. Then total cubes =1. xy line: (1,1,1) removed. So answer 0. Our calculation: cnt_xy=1, |A_xy|=1; cnt_xz=0, cnt_yz=0; inter all 0, triple 0; removed=1; answer=0. Good.
-
n=1, h=2, xy 1 1, xz 1 1. Since cube (1,1,1) covered by both. union size should be 1. Our calculation: cnt_xy=1, cnt_xz=1; |A_xy|=1, |A_xz|=1, |A_yz|=0 sum=2; inter_xy_xz: x=1: count_xy[1]=1, count_xz[1]=1 => 1; others 0; inter others 0; triple? need yz_ex? but yz none, triple=0; removed=2-1=1; answer=1-1=0. Good.
-
n=2, all six marks? But h≤200 so can have many. Let's test with S_xy={(1,1),(1,2),(2,1),(2,2)} (all 4 xy marks). That would cover all cubes because each cube has some (x,y) pair? Actually xy marks cover columns along z. If all possible xy pairs are marked, then every cube (x,y,z) is covered because for any (x,y), there is a line. So union would be all cubes. Let's compute: size_xy=4, size_xz=0, size_yz=0, removed=4*2=8? But total cubes = 8, so removed = 8? Actually xy lines cover all cubes: For each (x,y) there is a line covering all z. So indeed all cubes covered, removed = n^3 = 8. Our calculation: |A_xy| = 4 * 2 = 8. Others 0. Intersections 0. So removed = 8, answer 0. Good.
Now test with mixture: S_xy all rows? Actually all possible (x,y) would cover all cubes, so answer 0. Similarly if we have all xz or all yz, also cover all cubes.
Now test overlapping: If we have all xy and all xz, then union is all cubes still. Our inclusion-exclusion: |A_xy| = 42=8, |A_xz| = 42=8 sum=16. inter_xy_xz = sum_x count_xy[x]count_xz[x]. For n=2, count_xy[x] = number of y with (x,y) marked = 2 for each x (since all y). count_xz[x] = number of z with (x,z) marked = 2 for each x. So inter_xy_xz = for x=1:22=4, x=2:2*2=4, total 8. inter_xy_yz? Need cnt_xy_y and yz_by_y; yz none => 0. inter_xz_yz=0. triple? yz none => 0. removed = 16 - 8 = 8, answer=0. Good.
Now test with all three sets complete (all xy, xz, yz marks). That would be all cubes. Then we must compute inclusion-exclusion correctly.
Let's try n=2, with all 4 xy, 4 xz, 4 yz. We'll compute:
cnt_xy=4, cnt_xz=4, cnt_yz=4. |A_xy| = 4*2=8, |A_xz|=8, |A_yz|=8 sum=24.
Now inter_xy_xz: count_xy[x]=2 each, count_xz[x]=2 each, sum = 22 + 22 = 8.
inter_xy_yz: need cnt_xy_y[y] = for each y, number of x with (x,y) marked = 2 (since all x). yz_by_y[y].size() = number of z for that y = 2 (since all yz). So each y:2*2=4, two y's total=8.
inter_xz_yz: cnt_xz_z[z] = for each z, number of x =2; cnt_yz_z[z] = for each z, number of y =2; each z: 2*2=4, two z's total=8.
Triple intersection: For each x, xy_by_x[x] has y={1,2}, xz_by_x[x] has z={1,2}, and yz_ex[y][z] true for all y,z. So for x=1: 2 y * 2 z =4 combos all true => 4; x=2 similarly 4 => total triple=8.
Now union = 24 - (8+8+8) + 8 = 24 - 24 + 8 = 8. Wait that gives removed=8? But we expect removed = total cubes 8. So union should be 8, but we got 8, good! Actually 24 - 24 + 8 = 8. So answer = 0. So correct.
Our triple counted 8, which equals total cubes? Actually all 8 cubes are triple intersections. Good.
Thus formula works.
Now we should ensure to use appropriate data types.
Now implement.
One more check: In the triple intersection loop, we rely on yz_ex[y][z] boolean. But yz_ex is defined for marks only. That's correct because we need (y,z) present in S_yz.
Now we must be careful: For yz_ex, we used first coordinate as y, second as z. So indexing yz_ex[y][z] is correct.
Now we also need to consider that in some languages (C++), array sizes should be constant. We'll use global constant MAXN=500 or 501. But we can also allocate with n+1; however, we need to reset each time. Since n varies, we could allocate vectors of size n+1, but then for boolean matrices we need to know n. We can allocate as bool yz_ex[501][501] as said. That's fine.
Now we should watch for reading input: after reading n and h, we have h lines. We'll read using cin >> ci >> a >> b.
Potential pitfalls: ci might be read as string, and there might be extra whitespace. Use cin.
Now test with sample.
Now we can produce final answer.
But we should also think about alternative approach: Could there be cubes that are pierced by multiple holes but still counted correctly? Yes.
Now maybe the problem expects to consider that the hole might be a rectangular tunnel that removes material, but does it remove the entire small cube if it passes through? Yes, we consider cube "perforated" if it has a hole, i.e., if it lies on any hole. So our approach is correct.
Now finalize.
But wait: The problem says: "counts the number of small cubes with no holes." That means cubes that are not pierced by any hole. So we are correct.
Now produce final code.
Potential edge: The hole might be considered to be of zero thickness? But it's a hole, so it removes the material along its path. So cubes that are intersected are considered perforated. So our interpretation stands.
Now also note: The holes are drilled from the marked surface to the opposite surface. That means the hole passes through the entire cube. But what about the cube at the marked surface? It is perforated because the hole starts on its face? Actually the small cube that contains the marked square on its outer face is definitely pierced, because the drill goes through it. So yes, it is included.
Now confirm with example: For n=4, xy 4 4 pierces the top cube at (4,4,4) and all lower ones. So all good.
Now we can finalize.
But we should double-check the coordinate orientation: For xz mark, we interpreted that the marked face is y=1 (front). But could it be y=n (back)? The set of cubes pierced is still all cubes with that (x,z) and y from 1..n. So same. So no ambiguity.
Thus solution is robust.
Now let's implement in C++.
Implementation steps:
-
Include iostream, vector, string, etc.
-
Use namespace std.
-
const int MAXN = 500; but we can just use 501.
-
In main loop:
int n, h;
while (cin >> n >> h && (n || h)) {
bool xy_ex[501][501] = {}; // zero init
bool xz_ex[501][501] = {};
bool yz_ex[501][501] = {};
vector xy_by_x[501];
vector xz_by_x[501];
vector yz_by_y[501];
int cnt_xy=0, cnt_xz=0, cnt_yz=0;
for (int i=0; i<h; ++i) {
string plane;
int a,b;
cin >> plane >> a >> b;
if (plane == "xy") {
if (!xy_ex[a][b]) {
xy_ex[a][b] = true;
xy_by_x[a].push_back(b);
++cnt_xy;
}
} else if (plane == "xz") {
if (!xz_ex[a][b]) {
xz_ex[a][b] = true;
xz_by_x[a].push_back(b);
++cnt_xz;
}
} else if (plane == "yz") {
if (!yz_ex[a][b]) {
yz_ex[a][b] = true;
yz_by_y[a].push_back(b); // a is y, b is z
++cnt_yz;
}
}
}
// Compute inter_xy_xz
long long inter_xy_xz = 0;
for (int x = 1; x <= n; ++x) {
inter_xy_xz += (long long)xy_by_x[x].size() * xz_by_x[x].size();
}
// Compute cnt_xy_y
int cnt_xy_y[501] = {};
for (int x = 1; x <= n; ++x) {
for (int y : xy_by_x[x]) {
cnt_xy_y[y]++;
}
}
long long inter_xy_yz = 0;
for (int y = 1; y <= n; ++y) {
inter_xy_yz += (long long)cnt_xy_y[y] * yz_by_y[y].size();
}
// Compute cnt_xz_z and cnt_yz_z
int cnt_xz_z[501] = {};
for (int x = 1; x <= n; ++x) {
for (int z : xz_by_x[x]) {
cnt_xz_z[z]++;
}
}
int cnt_yz_z[501] = {};
for (int y = 1; y <= n; ++y) {
for (int z : yz_by_y[y]) {
cnt_yz_z[z]++;
}
}
long long inter_xz_yz = 0;
for (int z = 1; z <= n; ++z) {
inter_xz_yz += (long long)cnt_xz_z[z] * cnt_yz_z[z];
}
// Triple
long long triple = 0;
for (int x = 1; x <= n; ++x) {
if (xy_by_x[x].empty() || xz_by_x[x].empty()) continue;
for (int y : xy_by_x[x]) {
for (int z : xz_by_x[x]) {
if (yz_ex[y][z]) {
++triple;
}
}
}
}
long long removed = (long long)cnt_xy * n + (long long)cnt_xz * n + (long long)cnt_yz * n
- inter_xy_xz - inter_xy_yz - inter_xz_yz
+ triple;
long long total = (long long)n * n * n;
long long answer = total - removed;
cout << answer << endl;
}
Note: We didn't use xy_ex or xz_ex after insertion, but we could skip them if we wanted but they help dedup.
Now we must ensure that yz_by_y is indexed by y, and we used yz_by_y[y].size() correctly. That's fine.
Potential issue: In inter_xy_yz, we used yz_by_y[y].size(). But yz_by_y[y] contains all z for that y. That's count_yz_y[y]. So correct.
Now let's test with sample input manually.
We already did. Should output 52 and 46.
Now consider memory: bool arrays of 501x501 are about 251k each, three ~ 753k, plus vectors. Good.
Now check for any missing includes.
Now we can finalize.
But we should also think about potential error: In triple intersection, we used yz_ex[y][z] which is based on y and z. However, we used yz_ex defined as bool yz_ex[501][501]; But we inserted using yz_ex[a][b] where a is y, b is z. So indexing matches.
Now note: For yz, we also have yz_by_y but we don't need it for triple.
Now consider if there are marks that are duplicates, we skip. That's good.
Now think about scenario: For an xy mark, we store in xy_by_x[a] the y coordinate. But what about the case where there is also an xz mark with same x and maybe y? That's fine.
Now one more nuance: Are we sure that the hole goes through the entire cube, meaning it includes both the cube at the marked surface and the cube at the opposite surface? Yes.
Now check the total number of cubes along a line: n. Indeed, for each integer coordinate along the axis from 1 to n inclusive, there is a cube. So the line contains exactly n cubes. So our sizes are correct.
Now we should consider if the hole is considered to be a line of zero cross-section or unit square cross-section. In either case, the set of cubes it affects is those whose interior intersect the hole. If the hole is a line (infinitely thin), it would only affect cubes that the line passes through, which are the same set of cubes if the line goes through the centers? Actually a line of zero thickness passing through the cube along the axis would go through the centers of the cubes? For a grid of unit cubes aligned with axes, a line along the axis that goes exactly along the edges? Wait: If we mark a unit square on the face, the hole drilled from that square to the opposite face is likely a square prism of the same cross-section as the unit square (i.e., 1x1). So it removes entire cubes. If it were a line of zero thickness, it would only affect cubes that the line passes through; but the line could pass through the interior of multiple cubes if it goes along a grid line? Actually if you drill a hole of zero thickness (a line) from the center of the marked square, it would go through the cube along a straight line. For a grid of unit cubes, a line parallel to an axis that goes through the center of a unit square on the face will go through the centers of all cubes along that column, so it will intersect each cube's interior. So it would still affect those cubes. But if the line is exactly at the boundary between cubes, it might only affect those cubes on the boundary? However, the mark is on a unit square of the face, which corresponds to the face of a specific small cube. The hole likely goes through that small cube entirely. Usually in such problems, the hole is a tunnel that removes the whole column of small cubes.
Thus our interpretation is standard.
Now we should ensure that the answer is printed as integer per line.
Now final code.
But wait: There might be a trick: The holes could intersect each other and create additional cavities? But we only care about cubes that are not pierced. So we just need to know which cubes are intersected by any hole. Intersecting holes don't create new holes; they just mean some cubes are intersected by multiple holes. So union is fine.
Now confirm with a random test by brute force for small n to verify formula.
We'll implement a brute force for n=3 with random marks and compare.
But in analysis, we can trust.
Now think about reading input: There may be trailing spaces, but cin handles.
Now note: The input includes a line with "0 0". We'll break.
Now one more nuance: The coordinate axes: (x,y,z) = (1,1,1) is the lower left cube, and (x,y,z) = (n,n,n) is the upper right cube. So the orientation is as described.
Now we should consider that the marks might be given with coordinates outside the range? But assume valid.
Now finalize code.
Potential performance: For each dataset, we create 3 bool 2D arrays of size 501x501 on stack. That's okay. But we need to zero them. Using = {} initializes all to zero. However, if we declare inside loop, the compiler will generate code to zero them each iteration. That may be a bit of overhead but acceptable.
Alternatively, we could allocate global arrays and use memset to zero only the needed region up to n. But it's fine.
Now we should also consider that the vectors xy_by_x etc are recreated each iteration; they will be destructed and recreated, fine.
Now we also need to ensure that yz_by_y is defined for y up to n. We'll use size 501.
Now final code.
But wait, there is a possible off-by-one: For yz_by_y, we use index a as y. That's correct.
Now what about xz_by_x: we store per x list of z.
Now for cnt_xy_y, we compute correctly.
Now we will test with a custom case:
n=3, marks: xy 1 2; xz 1 3; yz 2 3.
Compute manually:
Cubes: total 27.
xy line: (1,2,1),(1,2,2),(1,2,3) -> 3
xz line: (1,1,3),(1,2,3),(1,3,3) -> 3
yz line: (1,2,3),(2,2,3),(3,2,3) -> 3
Union: distinct cubes: let's list: (1,2,1),(1,2,2),(1,2,3),(1,1,3),(1,3,3),(2,2,3),(3,2,3). That's 7. But note (1,2,3) appears in all three, so total 7. So removed = 7, answer = 20.
Our calculation:
S_xy: {(1,2)} size1
S_xz: {(1,3)} size1
S_yz: {(2,3)} size1
cnt_xy=1, cnt_xz=1, cnt_yz=1. n=3 => |A|sum = 3+3+3=9.
inter_xy_xz: x=1: count_xy[1]=1, count_xz[1]=1 =>1; others 0 => inter=1.
inter_xy_yz: cnt_xy_y: y=2: from xy (1,2) => cnt_xy_y[2]=1; yz_by_y[2].size() = 1 (since (2,3) present) => inter=1.
inter_xz_yz: cnt_xz_z: z=3: from xz (1,3) => cnt_xz_z[3]=1; cnt_yz_z: z=3: from yz (2,3) => cnt_yz_z[3]=1 => inter=1.
triple: For x=1: xy_by_x[1] has y=2; xz_by_x[1] has z=3; check yz_ex[2][3] true => triple=1.
Removed = 9 - (1+1+1) + 1 = 9-3+1=7. Answer =27-7=20. Works.
Now test with overlapping duplicates: n=3, marks: xy 1 2; xy 1 2 (duplicate). Our code will count cnt_xy=1, correct.
Now test with marks: xy 1 2; xz 1 2; yz 2 2? That might cause triple intersection. Should be fine.
Now potential bug: In triple intersection loop, we used yz_ex[y][z] which is boolean from marks. That's correct.
Now check large n and many marks: e.g., n=500, h=200 all distinct, computation is fast.
Now we might want to avoid using vectors of size 501 if n is smaller; but it's fine.
Now we must ensure that when we compute cnt_xy_y, we are using y up to n, but yz_by_y has size 501, and we access yz_by_y[y].size() for y up to n. That's fine.
Now finalize code.
Now produce answer.
But wait: Could there be a scenario where a mark is on a plane that is not the one we think? For instance, "xz" mark could be on the face y=n instead of y=1? But as argued, the set of pierced cubes is the same. However, is it possible that the hole drilled from the marked surface to the opposite surface could be in a different direction if the mark is on the opposite side? Let's examine: Suppose a mark is on the xz-plane but on the back face (y=n). Then the hole would be drilled from that back face to the front face (y=1). That still goes along the y-direction, piercing all cubes with that x and z, but now the order is reversed. So the set of cubes is still all y from 1 to n. So same.
Now what about "yz" mark on the left face (x=1) versus right face (x=n)? Same.
Thus the orientation of which face is marked does not matter; the line is the same.
Thus the solution is independent of which side the mark is on.
Now one more thought: Could a mark be on a face that is not orthogonal to the axis? No, it's on one of those three planes.
Thus all good.
Now we can implement.
But let's also consider if the problem intended that the hole is drilled along the direction perpendicular to the marked face, but only up to the opposite face, and that the hole is of square cross-section matching the unit square. That's what we used.
Now final code in C++.
Potential improvement: Since we only need yz_ex for triple, we could skip xy_ex and xz_ex after insertion if we use vectors to check duplicates? But we need to check duplicates when inserting. We could use a set of pairs, but bool matrices are fine.
Now check for memory: bool arrays of 501x501 are about 251001 bytes each, which is about 0.24 MB. Times 3 is 0.72 MB. Plus vectors: each vector may have small overhead, but total < 2 MB.
Now think about I/O: Use ios_base::sync_with_stdio(false); cin.tie(0); for speed.
Now finalize.
But we also need to consider the case where h=0 but n>0. Then cnt_xy=cnt_xz=cnt_yz=0, all intersections 0, removed=0, answer = n^3. That's correct.
Now we can write the code.
One more nuance: In the problem statement, they say: "Your job is to create a program that reads the positions marked n and counts the number of small cubes with no holes." So they say "positions marked n"? but that's just phrasing.
Now I'll produce final answer.
Now we should also test with a case where all three sets are empty: answer = n^3.
Now produce final code.
After writing, I'll test with given sample.
Let's simulate quickly:
Input:
4 3
xy 4 4
xz 1 2
yz 2 3
...
Our code will output 52 and 46.
Now produce final answer.
But wait, we should also consider if the holes might intersect in such a way that a cube is removed even if not directly intersected because it becomes isolated? No, the problem only cares about cubes that have holes (i.e., are pierced). So no.
Now final.
Now I'll produce the final solution explanation and code.
But before final, check if any optimization needed: The triple loop may be O(|S_xy|*|S_xz|) which is max 40k. That's fine.
Now I think it's correct.
Now produce final message with code.
Note: Some online judges might require using long long for n^3 when n=500, n^3=125,000,000 which fits in 32-bit signed int (max 2.1e9). But to be safe, use long long. The answer could be up to 125 million, which fits in int, but maybe they might have larger n? The problem says n ≤ 500, so okay. But we'll keep long long for consistency.
Now final code.
Potential minor bug: In yz_ex we used [a][b] where a is y, b is z. That's fine.
Now I'll write code.
But consider: When reading "yz 2 1", a=2, b=1. So y=2, z=1. In yz_by_y, we push_back(b) which is z. So yz_by_y[2] gets 1.
Now inter_xy_yz uses yz_by_y[y].size(). That's number of z for that y. So correct.
Now compute cnt_yz_z: from yz_by_y, for each y, for each z, increment cnt_yz_z[z]. That's number of y for that z. Good.
Now triple: yz_ex[y][z] is true if mark present. That's correct.
Now everything is fine.
Now final code.
Edge Cases: If n is 0 and h 0, we break. So no need to process n=0 dataset.
Now I'll output final answer.
But hold on: The problem statement mentions: "reads the positions marked n"? Actually it says "reads the positions marked n" maybe a translation.
Now I'll produce final code with proper includes.
One more thought: Should we consider that the holes may be considered as "penetrating horizontally or vertically". Could "horizontally or vertically" mean that the hole might be drilled in a direction that is not necessarily perpendicular to the face? The phrase "from the marked surface to the opposite surface" suggests it goes straight through to the opposite face, which is perpendicular.
Thus correct.
Now we can produce final answer.
But before final, let's double-check the interpretation with the figure description: They show an example n=4, and marks A, B, C. The right figure shows a hole that penetrates. The figure likely shows three holes: one vertical from top, one horizontal from front, one horizontal from side. That's exactly what we have.
Thus solution is confirmed.
Now code.
Potential pitfalls with reading input: after reading n and h, we read h lines. Each line has a string and two ints. We should use cin.
Now final.
Now I'll craft the final assistant message with explanation and code. ```cpp
#include
#include
#include
#include // for memset if needed, but not necessary
using namespace std;
const int MAXN = 500;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, h;
while (cin >> n >> h && (n || h)) {
// existence tables for deduplication
bool xy_ex[501][501] = {};
bool xz_ex[501][501] = {};
bool yz_ex[501][501] = {};
// lists for fast enumeration
vector<int> xy_by_x[501]; // for each x, list of y with (x,y) marked
vector<int> xz_by_x[501]; // for each x, list of z with (x,z) marked
vector<int> yz_by_y[501]; // for each y, list of z with (y,z) marked
int cnt_xy = 0, cnt_xz = 0, cnt_yz = 0;
for (int i = 0; i < h; ++i) {
string plane;
int a, b;
cin >> plane >> a >> b;
if (plane == "xy") {
if (!xy_ex[a][b]) {
xy_ex[a][b] = true;
xy_by_x[a].push_back(b);
++cnt_xy;
}
} else if (plane == "xz") {
if (!xz_ex[a][b]) {
xz_ex[a][b] = true;
xz_by_x[a].push_back(b);
++cnt_xz;
}
} else if (plane == "yz") {
if (!yz_ex[a][b]) {
yz_ex[a][b] = true;
yz_by_y[a].push_back(b); // a = y, b = z
++cnt_yz;
}
}
}
// ---------- pairwise intersections ----------
// A_xy ∩ A_xz
long long inter_xy_xz = 0;
for (int x = 1; x <= n; ++x)
inter_xy_xz += (long long)xy_by_x[x].size() * xz_by_x[x].size();
// A_xy ∩ A_yz
int cnt_xy_y[501] = {}; // how many x for each y in S_xy
for (int x = 1; x <= n; ++x)
for (int y : xy_by_x[x])
cnt_xy_y[y]++;
long long inter_xy_yz = 0;
for (int y = 1; y <= n; ++y)
inter_xy_yz += (long long)cnt_xy_y[y] * yz_by_y[y].size();
// A_xz ∩ A_yz
int cnt_xz_z[501] = {}; // how many x for each z in S_xz
for (int x = 1; x <= n; ++x)
for (int z : xz_by_x[x])
cnt_xz_z[z]++;
int cnt_yz_z[501] = {}; // how many y for each z in S_yz
for (int y = 1; y <= n; ++y)
for (int z : yz_by_y[y])
cnt_yz_z[z]++;
long long inter_xz_yz = 0;
for (int z = 1; z <= n; ++z)
inter_xz_yz += (long long)cnt_xz_z[z] * cnt_yz_z[z];
// ---------- triple intersection ----------
long long triple = 0;
for (int x = 1; x <= n; ++x) {
if (xy_by_x[x].empty() || xz_by_x[x].empty()) continue;
for (int y : xy_by_x[x]) {
for (int z : xz_by_x[x]) {
if (yz_ex[y][z])
++triple;
}
}
}
// ---------- union size ----------
long long total = (long long)n * n * n;
long long removed = (long long)cnt_xy * n + (long long)cnt_xz * n + (long long)cnt_yz * n
- inter_xy_xz - inter_xy_yz - inter_xz_yz
+ triple;
long long answer = total - removed;
cout << answer << '\n';
}
return 0;
}