We need to understand the problem description.
We have a condominium shaped as n cubes lined up in a row. Each cube i (i from 1 to n) has i floors, and each floor has i x horizontal rooms? Actually: "Each cube has one side length increasing by 1 from the west (1,2,3, ..., n), the i-th cube has i floors, and each floor has i x horizontal rooms. There is." The phrasing is ambiguous. Probably they mean: The i-th cube (i from 1 to n) has side length i? It says "cubes" but not necessarily perfect cubes; they are rectangular prisms? It says "the shape of n cubes lined up in a row." Each cube has one side length increasing by 1 from the west (1,2,3,..., n), the i-th cube has i floors, and each floor has i x horizontal rooms. I think they are describing a structure: There are n buildings (cubes) arranged in a row from west to east. The i-th building (from west) has i floors. On each floor of building i, there are i rooms in the horizontal direction? Actually "each floor has i x horizontal rooms" probably means each floor has i rooms in the east-west direction? Or maybe i rows? Let's parse.
Better: "Each cube has one side length increasing by 1 from the west (1,2,3, ..., n), the i-th cube has i floors, and each floor has i x horizontal rooms." It might be describing the dimensions: The i-th cube (building) has a base that is an i x i square? Actually "cube" might be a misnomer; they are rectangular prisms (boxes) with dimensions: width (west-east direction) = i, depth (north-south) maybe? But they said "cubes", so maybe each building is a cube of side length i, so it has i floors, each floor is an i x i grid of rooms? That would be typical: building i is a cube of side length i, so it has i floors, and each floor contains i x i rooms. But the description says "each floor has i x horizontal rooms." Could be missing "i" like "i x i"? The sample: n=3. If each building i has i floors and each floor has i x i rooms, then total rooms = sum_{i=1}^n (i * i * i) = sum_{i=1}^n i^3 = (n(n+1)/2)^2? Actually sum of cubes = (n(n+1)/2)^2. For n=3, sum of cubes = 36. But the output for first sample (3 1, action 0 4) yields 27. 36-? Not 27. So maybe total rooms is something else.
Maybe each building i has i floors, and each floor has i rooms (so total i^2 rooms per building). Then total rooms = sum_{i=1}^n i^2 = n(n+1)(2n+1)/6. For n=3, sum i^2 = 14. But output is 27, so not.
Maybe each building i has i floors and each floor has i rooms in the horizontal direction? But "horizontal rooms" could mean rooms along the east-west direction? Actually they said "each floor has i x horizontal rooms." Possibly there is a typo; it might be "each floor has i x i rooms"? That sum of cubes gives 36. But output after one action is 27; so if all rooms were lit initially, then 9 rooms turned off. The action: A-chan turned off lights in all the kth rooms from the west. For k=4. If each building is a cube with side i, how to define "the kth room from the west"? Probably they mean: Consider all rooms across all buildings arranged in a row? But there are multiple floors. Need coordinate system.
Given the three actions:
- A-chan turns off lights in all the kth rooms from the west.
- Rika-chan turns off lights in all the kth rooms from the south.
- Hart turns off lights in all rooms on the k floor.
Thus we have three dimensions: West-East position (horizontal axis), South-North position (another horizontal axis, perpendicular to the road?), and floor number (vertical). The condominium consists of n cubes (buildings) lined up along the west-east direction. Each building i has i floors, and each floor has i x horizontal rooms? Maybe "i x horizontal rooms" means i rooms in the north-south direction? Or perhaps "i x i rooms" meaning a square grid? The phrase "horizontal rooms" may refer to rooms arranged horizontally (east-west) and there is also vertical (floors). But Rika-chan's action is from the south, so there is a south-north dimension. So likely each floor has rooms arranged in a grid: a certain number of rooms in the east-west direction (west to east) and a certain number in the south-north direction. The description: "the i-th cube has i floors, and each floor has i x horizontal rooms." Maybe it's "i x i rooms" as in i rows (south-north) and i columns (west-east). That would be symmetric: building i is an i x i x i cube (i floors, each floor i by i rooms). That seems plausible.
But the sum of cubes is 36 for n=3, but after one action (turn off all rooms that are the kth from the west, k=4), output is 27, meaning 9 rooms turned off. If building i is i x i x i, the westmost column? "kth rooms from the west" means rooms whose west-east coordinate (maybe column index) equals k? But since buildings are separate with different widths, the global west-east coordinate across the whole complex? Let's define coordinates: West-east direction: buildings are placed side by side. Within building i, there are i columns (west to east). If we number the columns globally from the west end of the entire complex, then the westmost column of building 1 is column 1, building 1 has columns 1...1? Actually building 1 would have i=1, so it has 1 column. Then building 2 (i=2) has 2 columns, but they are adjacent to building 1's east side. So the global columns: building 1 column at position 1; building 2 columns at positions 2 and 3; building 3 columns at positions 4,5,6; etc. So the total number of distinct west-east positions is sum_{i=1}^n i = n(n+1)/2. For n=3, sum = 6. So there are 6 global columns. Each column might have multiple rooms vertically and north-south. But the total rooms then would be: For each building i, it has i floors, and for each floor, it has i rooms in north-south direction? But we need north-south dimension: In building i, each floor has i x ? rooms? The description: "each floor has i x horizontal rooms." Could be "each floor has i horizontal rooms"? That would mean each floor has i rooms in the north-south direction? But then the building would be 1D? No.
Maybe they meant: "each floor has i rows of rooms (north-south) and i columns (west-east)". That gives i^2 rooms per floor. Then total rooms per building = i * i^2 = i^3. Total rooms = sum i^3 = (n(n+1)/2)^2 = 36 for n=3. Then global columns count is sum i = 6, but inside each building columns are only within that building. However if we consider a global coordinate system, the west-east column index runs from 1 to total columns = sum i. But each building's floor also has north-south rows; building i has i rows (south-north). The rows are per building? Are the rows aligned across buildings? Probably the buildings share a common south side facing a road: "the south side of all cubes faces a straight road." So the south side is common; i.e., each building's south face is along the road. So the north-south direction: each building has depth (south-north) maybe equal to its side length? If it's a cube, then the south-north dimension is also i. So building i extends i units northwards from the road. So the north-south coordinate might be measured from the south side (road) as 1 (closest to road) to i (farthest north). However, do these coordinates align across buildings? The road runs along the south side, so the south side of each building is flush with the road. So the south edge is common. But the north-south extent differs: building 1 extends 1 unit north, building 2 extends 2 units, building 3 extends 3 units. So the overall north-south positions across the complex are not uniform: for a given north-south coordinate j, only buildings with i >= j have rooms at that coordinate (since they extend at least j units north). So there is a kind of "staircase" shape.
This is reminiscent of problems about a "triangular" arrangement: There are n buildings (or towers) with increasing heights and widths. Total rooms might be sum i^2? Wait: If each building i has i floors and each floor has i rooms in east-west direction and 1 room in north-south? That would sum i^2. But then Rika-chan's action (kth from south) would affect only rooms with a particular north-south index, which would exist only in buildings with i >= k. That seems plausible. But total rooms would be sum i^2 = 14 for n=3. But sample output after one action gives 27, which is larger than 14. So not sum i^2.
Maybe each building i has i floors, each floor has i rooms in north-south direction? But then where is the east-west? The building may be a single column in east-west but multiple floors and north-south rows. So building i would have i floors and i rows => i^2 rooms per building. Sum i^2 = 14. But again output 27 > 14.
Maybe the rooms are arranged in three dimensions: east-west positions (columns), north-south positions (rows), and floors (vertical). And the buildings are like blocks: building i occupies the region: east-west columns from (some start) to (some end) with width i, north-south rows from 1 to i (south to north), and floors from 1 to i. So building i is an i x i x i cube. Then total rooms = sum i^3 = 36 for n=3. After one action (A-chan turn off all kth rooms from west, k=4), we need to compute how many rooms remain lit. They say output 27. So that would imply 9 rooms turned off. Which rooms are the 4th from the west? The west-east columns: global indices: building 1: columns 1; building 2: columns 2-3; building 3: columns 4-6. So column 4 is the first column of building 3. Building 3 has 3 columns (4,5,6). So column 4 corresponds to westmost column of building 3. In building 3, there are i=3 floors, each floor has i=3 rows (north-south). So building 3 has 3x3 = 9 rooms on each floor? Actually per floor: 3 columns x 3 rows = 9 rooms. So at column 4 (westmost column of building 3), there are: for each floor (3 floors) and each row (3 rows) => 3*3=9 rooms. So turning off all rooms in column 4 would turn off exactly 9 rooms. Then total lit after = 36 - 9 = 27. That matches sample 1! Perfect.
Thus the structure: We have n cubes (buildings) in a row from west to east. The i-th cube (starting from west) has side length i, i.e., it occupies a cubic volume: east-west width = i, north-south depth = i, vertical height = i. It is placed such that its south side is flush with a straight road (so the south coordinate is 0? Actually we'll use indices starting at 1 from south). The cubes are adjacent: the west side of cube i+1 borders the east side of cube i. So overall, the complex is like a set of blocks with increasing sizes.
Thus, the coordinates can be defined as:
- West-east coordinate (x) starts at 1 at the westernmost point. The cubes are placed contiguously. Cube i occupies x from X_start(i) to X_end(i) inclusive, where X_start(i) = 1 + sum_{j=1}^{i-1} j = 1 + i(i-1)/2, and X_end(i) = sum_{j=1}^{i} j = i(i+1)/2. Because each cube j has width j.
- South-north coordinate (y) starts at 1 at the southmost (closest to road). Cube i occupies y from 1 to i.
- Floor coordinate (z) (vertical) starts at 1 at ground? Actually floors: 1 to i.
Thus, each room is identified by a triplet (x, y, z) such that there exists i with X_start(i) ≤ x ≤ X_end(i), 1 ≤ y ≤ i, 1 ≤ z ≤ i. Equivalently, the cube index i must satisfy: i ≥ max( y, z, and also such that x lies within its x-range). But since x-range is determined by i, we need to find i from x? For a given x, there is a unique i such that X_start(i) ≤ x ≤ X_end(i). Because the x-ranges partition the positive integers (since sum of widths). That i is the cube that contains that x coordinate. Then the room exists only if y ≤ i and z ≤ i. So existence condition: let i = f(x) where f(x) is the cube index that covers x; then the room (x,y,z) exists iff y ≤ i and z ≤ i.
Thus total number of rooms = sum_{i=1}^{n} (i * i * i) = sum_{i=1}^{n} i^3 = (n(n+1)/2)^2.
Now we have m actions, each with type q (0,1,2) and a parameter k. Type 0: A-chan (west-east): turn off all rooms that are the k-th from the west. That is, all rooms with x = k. But careful: not all such rooms exist, because if x = k, there is a cube i = f(k). The room exists only if y ≤ i and z ≤ i. So effectively we turn off all existing rooms with x = k.
Type 1: Rika-chan (south-north): turn off all rooms that are the k-th from the south. That is, all rooms with y = k. But again, existence requires that for the cube i containing x, we have k ≤ i and also z ≤ i. So we turn off all existing rooms with y = k.
Type 2: Hart (floors): turn off all rooms on the k floor. That is, all rooms with z = k. Existence: for the cube i containing x, need y ≤ i and k ≤ i.
Also note: "The three may try to turn off the lights in places where there is no room." So we only affect existing rooms; non-existent are ignored. That's fine.
Additionally: "The same action can only be given once." That is, among the m actions, each (type, k) combination appears at most once? Or each of the three persons might act multiple times but with distinct k? The phrase: "The same action can only be given once" means that in the input, there will be no duplicate lines with same q and k. That is, each specific (q,k) appears at most once. This matters because we need not worry about double counting.
Goal: After performing all m actions (turn off lights in those rooms; if a room is turned off multiple times, it's still off, but actions are on distinct sets possibly overlapping), compute the number of rooms that remain lit (i.e., were never turned off). Initially all lit.
Constraints: n, m up to 50000, k_i up to 2e9 (so large). However n is only up to 50000, so the complex's dimensions are bounded by total x positions up to n(n+1)/2 which for n=50000 is about 1.25e9, which is within 2e9. y and z coordinates are up to n (50000). So k may be larger than these maxima; if k exceeds the maximum coordinate in that dimension, then that action affects no rooms (since no room has that x, y, or z). So we need to handle large k by ignoring if out of range.
We need to compute the number of rooms that are not turned off. Since actions are subtractive, we can compute the total rooms minus the number of rooms that are turned off (taking into account overlaps). But careful: a room may be turned off by multiple actions; we must not double subtract. So we need the cardinality of the union of sets of rooms turned off by each action.
Thus we need to compute |Union_{j=1..m} S_j|, where each S_j corresponds to all existing rooms satisfying a coordinate equality (x = k, or y = k, or z = k). Then answer = total_rooms - |Union|.
Given constraints, m up to 50000, we need an efficient way to compute the union size. The sets are large (size up to O(n^3)?) but we can compute sizes and intersections via combinatorial formulas based on the structure.
Let's define:
Total rooms T = sum_{i=1}^n i^3 = (n(n+1)/2)^2.
Now for each action type with parameter k, define the set of existing rooms:
- For type 0 (x = k): S_x(k) = { (x,y,z) : x = k, and if i = cube containing x, then y ≤ i, z ≤ i }.
- For type 1 (y = k): S_y(k) = { (x,y,z) : y = k, and for cube i containing x, need i ≥ k and also z ≤ i }.
- For type 2 (z = k): S_z(k) = { (x,y,z) : z = k, and for cube i containing x, need i ≥ z and y ≤ i }.
Actually symmetry: For given z=k, existence requires i ≥ k and y ≤ i. For given y=k, existence requires i ≥ k and z ≤ i.
We can compute the size of each set individually.
Also intersections between two sets: e.g., S_x(k1) ∩ S_y(k2), etc. And triple intersections: S_x(k1) ∩ S_y(k2) ∩ S_z(k3). Then union size can be computed via inclusion-exclusion. Since m is up to 50000, we cannot do O(m^2) intersections. However, note that each set is defined by a single coordinate constraint; the union could be many, but we can perhaps handle by grouping by type and using combinatorial formulas to compute total union size as total rooms minus number of rooms that remain lit, which are those that avoid all the specified coordinates: i.e., rooms (x,y,z) such that x not in X_set, y not in Y_set, z not in Z_set, where X_set = {k: type 0 actions}, Y_set = {k: type 1 actions}, Z_set = {k: type 2 actions}. However careful: The condition for a room to be lit is that it has NOT been turned off by any action. That is, for a given room (x,y,z) to be lit, it must satisfy: x ∉ X_set, y ∉ Y_set, z ∉ Z_set. But wait, is that sufficient? Because if a room is turned off by an action of type 0 with parameter k, it's exactly those with x = k. Similarly type 1 with y = k; type 2 with z = k. So the condition that none of the actions applies is exactly: x not in any X_set, y not in any Y_set, z not in any Z_set. Indeed, each action turns off all rooms meeting that coordinate equality, regardless of the other coordinates. So the set of rooms that remain lit is exactly those rooms (x,y,z) that satisfy: x ∉ X, y ∉ Y, z ∉ Z, where X = set of x-values turned off (type 0), Y = set of y-values turned off (type 1), Z = set of z-values turned off (type 2). However, note that a room may be turned off even if its x is not in X but its y is in Y or z is in Z. So the lit condition is x ∉ X AND y ∉ Y AND z ∉ Z. That is correct.
Thus, the problem reduces to: Count number of existing rooms (x,y,z) such that x not in X, y not in Y, z not in Z, where X, Y, Z are given sets (with sizes up to m, but each set only contains values within possible coordinate ranges: X values up to total columns ~ n(n+1)/2 ≤ ~1.25e9, Y and Z values up to n ≤ 50000). But careful: For a room (x,y,z) to exist, it must satisfy that the cube index i for that x is at least max(y,z). So existence depends on the relationship between x and y,z.
So we cannot simply count independent product of allowed x, y, z; because existence condition couples them via the cube index.
We need to count, among all existing rooms, those with x not in X, y not in Y, z not in Z.
Let's denote for each possible x coordinate (1 to X_max = total columns = n(n+1)/2) we have a cube index i = f(x) which is the unique i such that x lies in the range of cube i. Let's define i(x) = cube index containing x. The condition for a room (x,y,z) to exist is: y ≤ i(x) and z ≤ i(x). So the existing rooms are exactly triples (x, y, z) where x ∈ [1, X_max], y ∈ [1, i(x)], z ∈ [1, i(x)].
Thus, the total lit rooms = sum over x not in X of (number of y in [1, i(x)] with y not in Y) * (number of z in [1, i(x)] with z not in Z). Because for a fixed x, y and z are independent within [1, i(x)], but we need y ∉ Y, z ∉ Z. So lit count = ∑_{x: x ∉ X} (count_y(x) * count_z(x)), where count_y(x) = number of integers y with 1 ≤ y ≤ i(x) and y ∉ Y, and similarly count_z(x) = number of integers z with 1 ≤ z ≤ i(x) and z ∉ Z.
Therefore, if we can precompute for each possible i (cube index) the values of count_y and count_z as functions of i (since they depend only on i(x) not on x itself), we can sum over x not in X. Note that many x share the same i: each cube i has exactly i many x values (its width). So we can group by i.
Let for each i from 1 to n: let w_i = i (number of x positions in cube i). Let total_i = i^3 rooms originally in cube i (since y and z each up to i). But we are counting only those with x not in X, y not in Y, z not in Z.
Define for a given i:
-
X_i = set of x positions within cube i that are in X (i.e., turned off by type 0 actions). For cube i, the x positions are from X_start(i) to X_end(i) inclusive. Among those, some may be in the set X (global). Since X is a set of arbitrary numbers (maybe up to 2e9), we need to know which x within cube i are in X. This can be done by iterating over the type 0 actions (X set) and mapping each k to its cube i (if k ≤ X_max). For each such k, we add that x to the set of turned-off x positions in that cube.
-
For y and Z, they are independent of x within a cube; the condition y ∉ Y and z ∉ Z depends only on i (since the allowed y and z values are from 1 to i). So for each i, we can compute:
A_i = number of y in [1,i] not in Y.
B_i = number of z in [1,i] not in Z.
But careful: Y and Z are sets from type 1 and type 2 actions. Since y and z coordinates only go up to n, and n ≤ 50000, we can precompute an array for y presence up to n. Similarly for z (also up to n). So we can compute prefix counts to get A_i and B_i quickly.
Now, for cube i, the lit rooms from that cube are those with x positions not in X_i, y not in Y, z not in Z. Since y and z conditions are independent of x (within the cube), we can compute:
Lit_i = (number of x in cube i not in X_i) * (A_i) * (B_i).
Because for each allowed x, the number of allowed y is A_i, allowed z is B_i, and choices are independent.
Thus total lit = ∑_{i=1}^{n} ( (w_i - |X_i|) * A_i * B_i ), where |X_i| is the count of x positions in X that lie in cube i.
We need to compute for each i, the count of x in X that fall into cube i. Since there are up to m type 0 actions (X set), and m ≤ 50000, we can simply iterate over each type 0 action, determine its cube i (if k within total columns), and increment a counter for that cube. But careful: There could be multiple actions with the same k? It's said same action (i.e., same q and k) appears only once, so no duplicates. So each k in X is unique. So we can just process each type 0 action, check if k ≤ X_max, compute i, increment X_i_count. However, computing i from k quickly: we need to find i such that i(i-1)/2 < k ≤ i(i+1)/2. That's essentially solving for i = ceil((sqrt(8k+1)-1)/2). Since n up to 50000 and k up to 2e9, but m only 50000, we can compute i via integer arithmetic, maybe binary search or direct formula with sqrt (floating point careful). Since we need to do for each type 0 action, up to 50000, that's fine.
But there is nuance: The total columns X_max = n(n+1)/2. If k > X_max, then it's outside the complex; ignore. So we only consider k ≤ X_max.
Now, Y and Z sets: For type 1 actions (y = k), we need to add k to Y if k ≤ n (since max y is n). Similarly for type 2 (z = k), add to Z if k ≤ n. Note that Y and Z may contain duplicates? The problem says same action can only be given once, meaning each (q,k) appears at most once. So no duplicate within same type, but it's possible that there is both a type 1 and type 2 with same k? That's fine; they are different types. So sets Y and Z are independent.
Thus we can compute A_i and B_i efficiently:
- Let Y_present[1..n] boolean; similarly Z_present.
- For each type 1 action with k <= n, set Y_present[k]=true.
- For each type 2 action with k <= n, set Z_present[k]=true.
- Then we can compute prefix sums: For each i, A_i = i - count_{y=1..i} Y_present[y]. So we can compute an array Y_prefix where Y_prefix[i] = number of Y elements ≤ i, then A_i = i - Y_prefix[i].
Similarly B_i = i - Z_prefix[i].
Now compute for each i from 1 to n: w_i = i. Let lit_i = (i - X_i_count) * A_i * B_i. Summation.
Edge Cases: Could there be an action with k negative? No, k ≥ 1 per constraints? It says 1 ≤ ki ≤ 2×10^9. So positive.
Now the main challenge: Compute X_i_count: the number of x positions in cube i that are in X (i.e., type 0 actions with that x coordinate). Since each cube i contains i distinct x values. There may be up to m=50000 type 0 actions; but note that X could have many elements. However, we can process them and increment per cube. But we must also ensure that if a type 0 action has k that maps to cube i, we should also check that k is indeed within [X_start(i), X_end(i)]; we need to validate that i computed is ≤ n. Because if k ≤ X_max, the computed i will be between 1 and n. So we just compute i and if i ≤ n then increment X_i_count[i].
But wait: There might be multiple type 0 actions that map to same cube i; they will increment the count accordingly. However, we must also ensure that they correspond to distinct x positions (since duplicate (q,k) not allowed, but could two different k map to same x? No, distinct k, so they are different x coordinates. So each action corresponds to a distinct x position, but it could happen that two different k values are within the same cube, that's fine; we count them as separate turned-off x's.
Now, is there any nuance regarding actions that might turn off a room that doesn't exist? For type 0: if x = k, but for that x, some (y,z) combinations may not exist because y or z > i(x). However, the action definition says "turn off the lights in all the kth rooms from the west." It likely means all rooms that are in the kth position from west, i.e., all rooms with that x coordinate (regardless of y and z) but only those that exist. So our handling is correct: we are turning off all existing rooms with that x. In our counting for lit rooms, for a given x, we are counting only y and z that are within i(x). So if we mark x as turned-off, we effectively remove all rooms with that x from lit count. So we set the factor for x positions that are in X to 0 (since we only sum over x not in X). That's exactly (i - X_i_count) meaning we count only those x positions not in X.
Now, what about type 1 and type 2 actions? They affect all rooms with y=k or z=k, regardless of x and the other coordinate, but only existing rooms. In our method, for each cube i, we count the number of allowed y values (A_i) and allowed z values (B_i) as those not in Y or Z respectively. However, careful: For a given cube i, a room with y=k where k ≤ i would be turned off if k ∈ Y. So indeed, we should exclude those y values entirely from the count for that cube. Similarly for z. That's what we do: A_i = count of y in [1,i] not in Y. So for a given cube i, all rooms with y ∈ Y are removed from lit count, regardless of x and z. That's correct as long as those rooms exist (i.e., y ≤ i). Since we restrict y to ≤ i, if Y contains a value k > i, it does not affect cube i. But A_i = i - (#Y ∩ [1,i]) automatically accounts for that, because Y_prefix[i] counts only Y elements ≤ i. So fine.
Thus the formula seems correct.
But wait: Are there any interactions where a room might be excluded due to both x and y constraints? In our approach, we are counting lit rooms as those with x not in X, y not in Y, z not in Z. Since we multiply counts per x, we are assuming independence: For a fixed x not in X, the number of (y,z) combos with y not in Y and z not in Z is product of counts (A_{i(x)} * B_{i(x)}). This is valid because y and z are independent (choose any y from allowed set and any z from allowed set) and existence condition is y ≤ i and z ≤ i. However, does the existence condition also require that the room actually exists in terms of coordinates? The room exists if y ≤ i and z ≤ i. Our A_i and B_i are defined as counts of y and z in [1,i] that are not in Y/Z. So indeed each combination of an allowed y and allowed z gives a valid room (since both ≤ i). So product is correct.
Thus overall lit count = ∑_{i=1}^n ( (i - cntX_i) * (i - cntY_i) * (i - cntZ_i) ), where cntX_i = number of x in X that lie in cube i, cntY_i = number of y in Y that are ≤ i, cntZ_i = number of z in Z that are ≤ i. But careful: (i - cntY_i) is A_i, but cntY_i is the count of Y elements in [1,i]. Similarly for Z. That matches.
Now we need to compute cntY_i and cntZ_i for all i quickly. Since n ≤ 50000, we can precompute arrays Ycnt[0..n], Zcnt[0..n] by marking present and then prefix sum. Easy.
For cntX_i, we need to compute for each cube i how many x in X fall into that cube. Since the x coordinates are unique and there are up to 50000 type 0 actions, we can just loop over each type 0 action, compute its cube index, and if i ≤ n (i.e., x ≤ X_max) increment cntX_i[i]. This is O(m0 log something) if we binary search for i, but we can compute i via formula or binary search. Since n is only 50000, we can precompute the boundaries of cubes: X_start[i] = i*(i-1)/2 + 1, X_end[i] = i*(i+1)/2. Then for each k, we can find i by binary search over i from 1 to n (since n is small). That would be O(m0 log n) ≤ 50000 * 16 ≈ 800k, fine. Or we could compute i = floor((sqrt(8k+1)-1)/2) but careful with integer rounding. Simpler: binary search.
But we need to be careful: The x coordinate k may be exactly at a boundary? The boundaries are inclusive. For example, X_end[i] = i(i+1)/2, and X_start[i+1] = i(i+1)/2 + 1, so no overlap. So binary search works.
Thus the solution is straightforward.
However, are there any pitfalls? The problem says "The same action can only be given once", but that might also mean that each person (type) can only act once? But from examples, they gave two type 2 actions (both Hart) in second sample, so that's allowed. So it's just that each specific (q,k) pair is unique.
Now verify sample 2 to confirm our reasoning.
Sample 2:
Input:
3 2
2 2
2 3
n=3. Actions: type 2 (Hart) turn off rooms on floor 2, and floor 3.
So Y set empty, X set empty, Z = {2,3}.
We compute:
Total rooms = sum i^3 = 1+8+27=36.
Now we need lit rooms after turning off all rooms on floor 2 and floor 3. That means keep only rooms with z=1? Actually turning off floor 2 and floor 3 removes all rooms with z=2 or z=3. So lit rooms are those with z=1 only. But also existence condition: z must be ≤ i. So for each cube i, we count rooms with z=1, and any x (all x in cube i) and any y (1..i) as long as z=1.
Our formula: For each i, cntX_i = 0. cntY_i = 0 (since Y empty). cntZ_i = number of Z elements ≤ i. Z = {2,3}. So:
i=1: cntZ_1 = 0 (since 2,3 >1). So B_i = i - cntZ_i = 1 - 0 =1? Wait, B_i = number of z in [1,i] not in Z = i - cntZ_i. For i=1, i=1, allowed z values are {1}. Since Z contains 2,3, not 1, so B_1 =1. A_i = i - cntY_i =1-0=1. So lit_i = (i - 0) * 1 * 1 = i * 1 *1 = i. But i=1 gives lit_1 =1. However, cube 1 originally has 1 room per floor? Actually cube 1: width=1, depth=1, height=1 => 1 room total. With z=1 only, that room is lit. So correct.
i=2: cntZ_2 = number of Z ≤2 => only 2, so cntZ_2=1. B_2 = 2 - 1 =1 (allowed z: only z=1 because z=2 is turned off). A_2 = 2 (since Y empty, all y 1,2 allowed). lit_i = (i - 0) * A_2 * B_2 = 2 * 2 * 1 =4. Let's check manually: cube 2: width=2 => two x positions. y from 1 to 2, z from 1 to 2. Total rooms=8. Off: all with z=2. So rooms with z=1 remain: count = (width=2)*(y=2) = 4. So correct.
i=3: cntZ_3 = number of Z ≤3 => both 2 and 3, so cntZ_3=2. B_3 = 3 - 2 =1 (allowed z: only z=1). A_3 = 3 (y all allowed). lit_i = (3) * 3 * 1 =9. Cube 3: total rooms=27, off those with z=2 or 3, so keep z=1: width=3 * y=3 = 9. So total lit = 1+4+9=14. Output is 14. Matches.
Thus our approach works.
Now we need to ensure that for type 0 actions, they affect x positions. For sample 1, we got lit = 27, matches.
Thus the problem reduces to the described computation.
Now we must consider the complexity: n up to 50000, m up to 50000. Precomputing prefixes for Y and Z O(n). For X, we need to process up to 50000 type 0 actions, each binary search O(log n) or compute directly. That's fine.
Potential issues: The x coordinate can be as large as about 1.25e9, which fits in 64-bit. Use 64-bit for sums.
We must compute total_rooms = (n*(n+1)/2)^2. For n=50000, n*(n+1)/2 ≈ 1.25e9, squared is about 1.56e18, which fits in 64-bit? 2^63-1 ≈ 9.22e18, so yes fits in signed 64-bit. But careful: (1.25e9)^2 = 1.5625e18 < 9.22e18, so ok. We'll use unsigned 64-bit or signed 64-bit.
Now compute lit sum similarly: For each i up to 50000, compute term = (i - cntX_i) * (i - cntY_i) * (i - cntZ_i). i up to 50000, each factor up to 50000, product up to 1.25e14, sum over 50000 gives up to about 6.25e18, which might exceed 2^63? Let's estimate maximum: worst-case all cnts zero, lit_i = i^3. Sum i^3 = (n(n+1)/2)^2. For n=50000, that's 1.5625e18 as above. So sum fits within 1.5625e18, still < 9.22e18. So signed 64-bit is safe.
But careful: If we subtract from total, might get negative? No, lit ≤ total.
Thus we can use 64-bit (long long in C++).
Now we must handle the case where an action of type 0 with k maps to a cube index > n (i.e., k > X_max). Then ignore, since no such x exists. For type 1 or 2 with k > n, ignore.
Now what about type 0 with k exactly at a boundary? It will map to some cube i as per ranges; that's fine.
Implementation Steps:
- Read n, m.
- Initialize:
- cntX array of size n+1 (1-indexed) to 0.
- boolean arrays Y_present and Z_present of size n+1 to false.
Actually we only need prefix counts, so we can directly maintain count arrays: For Y, we can have an array Y_cnt[0..n] where Y_cnt[i] = number of type1 actions with k = i (but k may exceed n; ignore). Then compute prefix sum.
But simpler: Create arrays Y_mark[0..n] = 0, Z_mark[0..n] = 0. For each type1 action, if k <= n, set Y_mark[k] = 1. For type2, if k <= n, set Z_mark[k] = 1.
Then compute prefix sums: Y_prefix[i] = sum_{j=1..i} Y_mark[j], similarly Z_prefix.
Then for each i, cntY_i = Y_prefix[i], cntZ_i = Z_prefix[i].
- For type0 actions: For each (0, k):
- if k > total_columns = n*(n+1)/2, skip.
- else find cube index i such that X_start(i) <= k <= X_end(i). Use binary search on precomputed X_end array (or X_start). Precompute for i from 1 to n: end[i] = i*(i+1)/2. Then binary search smallest i such that end[i] >= k. Since ranges partition, that i is the cube.
- increment cntX[i] by 1.
- After processing all actions, we have cntX[i] for i=1..n.
Note: It is possible that cntX[i] > i? Because multiple actions could target same x? No, each action distinct k, and each cube i has exactly i distinct x positions. Since k values are distinct, the count of x's in X that lie in cube i cannot exceed i. So cntX[i] ≤ i. This is fine.
- Compute total_lit = 0.
For i = 1 to n:
allowed_x = i - cntX[i];
allowed_y = i - Y_prefix[i]; // i - cntY_i
allowed_z = i - Z_prefix[i];
lit_i = allowed_x * allowed_y * allowed_z; // use 64-bit
total_lit += lit_i;
- Output total_lit.
But wait: Are we missing something? The condition for a room to be lit is that it hasn't been turned off by any action. But what if a room is turned off by multiple actions? Our counting ensures we only count rooms where all three coordinates avoid the forbidden sets. So that's correct.
Now we should double-check that there is no other subtlety: The action definitions: "A-chan turned off the lights in all the kth rooms from the west." Could that mean something like: For each floor, turn off the kth room from the west? But the phrase "all the kth rooms from the west" likely means all rooms that are the k-th position from the west side, i.e., all rooms with that x coordinate, across all floors and north-south positions. Our interpretation seems consistent with sample.
Similarly, "Rika-chan turned off the lights in all the kth rooms from the south." That would be all rooms with that y coordinate.
"Hart turned off the lights in all rooms on the k floor." That's all rooms with that z coordinate.
Thus our approach stands.
But before finalizing, let's test with some additional scenarios.
Edge Cases:
-
No actions: m=0. Then cntX[i]=0, Y_prefix[i]=0, Z_prefix[i]=0. lit_i = i * i * i = i^3. total_lit = sum i^3 = (n(n+1)/2)^2. Correct.
-
Actions covering all x or all y or all z. For instance, if we turn off all x coordinates (like many actions covering all x positions). But there are many x positions (up to ~1.25e9), but m ≤ 50000, so cannot cover all. However if we have actions covering all y values from 1 to n (all possible y). Then Y_prefix[i] = i, so allowed_y = 0 for all i => total_lit = 0. That's correct: every room has some y coordinate; if all y are turned off, no room lit.
-
Overlap: Suppose we have type0 with x=4 (as sample 1) and type1 with y=2. Then lit count should exclude rooms with x=4 OR y=2. Our counting: For each i, allowed_x = i - cntX_i, allowed_y = i - cntY_i. But careful: This product counts rooms where x not in X and y not in Y. That's exactly the condition "x not in X AND y not in Y AND z not in Z". So that's correct.
Now check that the product method doesn't overcount rooms where both x and y are allowed but the specific (x,y,z) might be turned off due to something else? Only conditions are those three, so it's fine.
Now, is there any condition that for a given x, the set of allowed y and z might be correlated due to the fact that y and z maximum is i? That's accounted.
Thus solution is straightforward.
Now we need to consider performance and implementation details:
- Input size: up to 50000 lines, fine.
- Need to compute X_start and X_end for binary search. Since n ≤ 50000, we can compute an array end[i] = i*(i+1)/2 for i=1..n. Also maybe store start[i] = end[i-1]+1 with end[0]=0.
- For each type0 k, we binary search to find smallest i with end[i] >= k. This is O(log n) per. With n small, could also iterate linearly? 50000*50000=2.5e9 too high. So binary search.
Alternatively, we could compute i directly: i = floor((sqrt(8k+1)-1)/2). But we must be careful with integer precision. Since k up to 2e9, double sqrt might have rounding errors. Better use binary search.
But binary search on end array of length n. Since n is 50000, log2(50000) ~16, so total 800k operations, fine.
Edge: k could be exactly at boundary; e.g., k = end[i-1]? Actually start[i] = end[i-1]+1, so the first k of cube i is end[i-1]+1. For k = end[i-1], that belongs to cube i-1. So condition: find i s.t. end[i] >= k. Then check if k <= end[i] and also k > end[i-1]? But since the ranges partition, the found i will be correct. Example: k=1: end[1]=1 >=1 so i=1; okay. k=2: end[1]=1 <2, end[2]=3 >=2, so i=2; correct.
So binary search works.
Now we must also ensure that for type0 k that is less than or equal to X_max, we get i within 1..n. So we need to compute X_max = n*(n+1)/2. If k > X_max, skip.
Now implement:
-
Use vectors: cntX(n+1, 0) (1-indexed).
-
Y_mark and Z_mark vectors of size n+1, filled 0.
-
For each action line:
read q, k.
if q == 0:
if k <= X_max:
i = bin_search(k); cntX[i]++.
elif q == 1:
if k <= n: Y_mark[k] = 1.
else: // q==2
if k <= n: Z_mark[k] = 1.
After processing all actions, compute Y_prefix and Z_prefix:
- Y_prefix[0] = 0; for i=1..n: Y_prefix[i] = Y_prefix[i-1] + Y_mark[i];
- Similarly Z_prefix.
Then compute total_lit = 0 (unsigned long long or long long).
For i = 1..n:
allowed_x = i - cntX[i];
allowed_y = i - Y_prefix[i];
allowed_z = i - Z_prefix[i];
total_lit += (long long) allowed_x * allowed_y * allowed_z;
Print total_lit.
Now let's test with provided samples.
Sample1: n=3, m=1, type0 k=4.
X_max = 34/2 =6. k=4 ≤6. Binary search: end[1]=1, end[2]=3, end[3]=6. For k=4, end[2]=3<4, end[3]=6>=4 => i=3. cntX[3]=1.
Y_mark all zero, Z_mark zero. Y_prefix[1]=0,Y_prefix[2]=0,Y_prefix[3]=0. Z same.
i=1: allowed_x=1-0=1, allowed_y=1, allowed_z=1 => 1
i=2: allowed_x=2-0=2, allowed_y=2, allowed_z=2 => 222=8
i=3: allowed_x=3-1=2, allowed_y=3, allowed_z=3 => 23*3=18
Sum=1+8+18=27. Output 27.
Matches.
Sample2: n=3, m=2, type2 k=2, type2 k=3.
X_max=6. No type0. cntX all zero.
Y_mark all zero.
Z_mark[2]=1, Z_mark[3]=1.
Z_prefix: [0,0,1,2].
i=1: allowed_x=1, allowed_y=1, allowed_z=1-0=1 =>1
i=2: allowed_x=2, allowed_y=2, allowed_z=2-1=1 =>4
i=3: allowed_x=3, allowed_y=3, allowed_z=3-2=1 =>9
Sum=14.
Works.
Now consider a case where type0 actions overlap with type1 etc: e.g., n=2. Buildings: i=1: x=1, y=1, z=1 =>1 room. i=2: x=2,3; y=1,2; z=1,2 => 8 rooms? Actually i=2: 2x2x2=8, total=9.
Actions: type0 k=2, type1 k=1.
Then:
cntX: k=2 => binary: end[1]=1<2, end[2]=3>=2 => i=2, cntX[2]=1.
Y_mark[1]=1.
Y_prefix: [0,1,1]? Actually i=1:1, i=2:1 (since Y_mark[2]=0).
Z_prefix all 0.
Now compute:
i=1: allowed_x=1-0=1, allowed_y=1 - Y_prefix[1]=1-1=0, allowed_z=1 => 0.
i=2: allowed_x=2-1=1, allowed_y=2 - Y_prefix[2]=2-1=1, allowed_z=2 => 112=2.
Total lit=2.
Check manually: All rooms: list:
Cube1: (x=1,y=1,z=1) - turned off? y=1 is turned off by type1, so off.
Cube2: x=2,3; y=1,2; z=1,2.
Turned off by type0 x=2: all rooms with x=2: (2,1,1),(2,1,2),(2,2,1),(2,2,2) => 4 rooms off.
Turned off by type1 y=1: all rooms with y=1: (1,1,1) from cube1, (2,1,1),(2,1,2),(3,1,1),(3,1,2) from cube2. But note (2,1,1),(2,1,2) are already off due to x=2; overlaps fine.
Now lit rooms: those not off. Let's enumerate cube2 rooms not off:
x=2 are all off. So only x=3.
For x=3, y can be 1 or 2, z 1 or 2.
y=1 is off, so only y=2.
Thus (3,2,1) and (3,2,2) are lit. So 2 lit rooms. matches.
Thus correct.
Now we need to ensure that when we compute allowed_y = i - Y_prefix[i], we are counting y values that are not turned off. However, what about y values that are turned off but maybe for a given x, the cube's i may be less than that y? That's automatically excluded because we only consider y ≤ i. Since Y_prefix[i] counts only Y values ≤ i, that's correct. For y > i, they aren't in the allowed range anyway. So fine.
Now, about possible actions that turn off a room that doesn't exist: e.g., type1 with k=10 when n=5. Y_mark[10] would be ignored because 10>5, so no effect. Good.
Now, what about type0 with k that maps to cube i, but that cube may have y and z max i. The action turns off all rooms with that x, regardless of y,z; some of those may not exist if y or z > i? Actually, all y,z with 1≤y≤i and 1≤z≤i exist; that's all. So for that x, all i*i rooms exist. So turning off that x removes exactly i^2 rooms. Our method subtracts those x positions entirely, which is correct.
Thus solution is correct.
Now we need to handle input format: first line n m, then m lines each with qi ki. Possibly m=0? But constraints say 1 ≤ m? Actually says 1 ≤ n, m ≤ 50000, so m≥1. But could be 0? Not per constraints. But we can handle anyway.
Now about memory: arrays of size n+1, fine.
Now about binary search: we can precompute end array. Use std::upper_bound or lower_bound? Actually we need to find smallest i such that end[i] >= k. That's equivalent to lower_bound on end array for value k. Since end is sorted. So we can compute index = lower_bound(end.begin(), end.end(), k) - end.begin(). But careful: if we use 0-indexed, we might have end[0]? Usually we have end indexed from 1. Simpler: create vector end(n+1) with end[0]=0, for i=1..n: end[i]=i*(i+1)/2. Then index = lower_bound(end.begin()+1, end.end(), k) - end.begin(). This returns i in [1,n]. Because end[0]=0 might be less than k, but we exclude it.
Implementation:
vector end(n+1);
for (int i=1; i<=n; i++) end[i] = (long long)i*(i+1)/2;
For each k:
if (k > end[n]) continue;
int i = lower_bound(end.begin()+1, end.end(), k) - end.begin();
cntX[i]++;
But note: lower_bound returns first position where end[i] >= k. For k exactly equal to end[i], that's fine, returns i. But we must ensure that k is not less than 1? k≥1 so fine.
Alternatively, we can compute i via integer formula: i = ceil((sqrt(8*k+1)-1)/2). But then we need to check boundary conditions. Binary search is simple.
Given n=50000, we can also precompute start array and find i via binary search on start? But either works.
Time complexity: O(n + m log n) ≈ 50000 * 16 = 800k, plus loops O(n), fine.
Now edge case: type0 actions may have k that is exactly 0? No, k≥1.
Now consider if there are both type0, type1, type2 actions. Our method combines them correctly.
Thus solution is ready.
But wait: The problem says "The same action can only be given once". Could that be interpreted as "each of the three actions (A-chan, Rika-chan, Hart) can only be performed once"? But the second sample has two Hart actions, so that interpretation is invalid. So it's about duplicate (q,k). So we are fine.
Now, double-check the phrase: "The three may try to turn off the lights in places where there is no room." So actions that target non-existent coordinates simply do nothing.
Now, is there any scenario where a room could be turned off by an action that seems to target a coordinate but that coordinate might be outside the building's range? For type0: x=k, but if k maps to a cube i, then the room (x=k, y, z) exists only if y≤i and z≤i. But the action says "turn off the lights in all the kth rooms from the west." That could be interpreted as: For each floor, the k-th room from the west (i.e., the room at that horizontal position) is turned off. But if a building has fewer than k rooms in that direction, then there is no such room on that floor. So our interpretation that we turn off all rooms with that x coordinate across all floors and all y is consistent, because if the building has width less than k, then it does not have that x coordinate. So we are turning off only existing rooms. Good.
Now let's consider potential alternative interpretation: Could "kth rooms from the west" mean the k-th room along the west-east direction within each building? That is, for each building, you consider its own columns numbered from west to east 1..i, and turn off all rooms at position k from the west within that building (but only if the building has at least k columns). That would be different: it would turn off rooms with column index = k within each building, but the global x coordinate differs per building. For example, for n=3, building 2 has columns 2 and 3 (global), but its internal column indices: 1 (westmost) and 2 (eastmost). If k=2, then building 2's second column (global x=3) would be turned off, and building 3's second column (global x=5) would be turned off. But the sample 1: k=4. If we interpret as internal column index, building 1 has only 1 column, so no column 4; building 2 has 2 columns, no column 4; building 3 has 3 columns, no column 4. So no rooms turned off, output would be 36, not 27. So that interpretation is not correct. So it's global coordinate.
Thus our interpretation stands.
Now we need to make sure we understand the coordinate system: The cubes are placed adjacent, so the west-east coordinate is global. So we are correct.
Now about the problem constraints: n,m up to 50000, ki up to 2e9. The sum of i up to n is about 1.25e9, so X_max can be up to about 1.25e9, which is within 2e9. So k up to 2e9 may be larger, we skip.
Now we should also consider the possibility of integer overflow during intermediate calculations: When computing allowed_x * allowed_y * allowed_z, each factor up to 50000, product up to 125e12 = 1.25e14, fits in 64-bit. Sum up to 1.56e18, also fits in signed 64-bit (max 9.22e18). But to be safe, we can use unsigned long long (0 to 1.8e19). Or just long long.
Now confirm with n=50000: total_rooms = (50000*50001/2)^2 = (1250025000)^2 ≈ 1.5625625e18. So fine.
Now let's think about alternative potential pitfalls: The problem says "The same action can only be given once". But what about actions that are effectively the same room set but from different types? Not a problem.
Now we need to output the number of lit rooms after all m actions. Our calculation yields exactly that.
Now we should implement.
But wait: The problem description says "the i-th cube has i floors, and each floor has i x horizontal rooms. There is." The phrase "i x horizontal rooms" might be confusing but we interpreted as i by i. Good.
Now potential nuance: Could the "i x horizontal rooms" mean each floor has i rooms in the horizontal direction (east-west) and also the building extends i rooms in the north-south direction? It says "each floor has i x horizontal rooms." That could be interpreted as each floor has i rooms in the east-west direction, but not necessarily i in north-south? However, Rika-chan's action "kth rooms from the south" suggests a north-south dimension. So there must be multiple rows. If each floor had only one row (south-most?), then "kth from the south" would always be the same room? That doesn't make sense. So it must be i rows as well. So i x i is the natural interpretation.
Thus our solution is correct.
Now, let's write pseudo-code:
def solve():
read n, m
X_max = n*(n+1)//2
cntX = [0](n+1)
Y_mark = [0](n+1)
Z_mark = [0](n+1)
end = [0](n+1)
for i in 1..n:
end[i] = i*(i+1)//2
for each action:
q, k = read
if q == 0:
if k <= X_max:
i = lower_bound(end, k, 1, n) # returns index
cntX[i] += 1
elif q == 1:
if k <= n:
Y_mark[k] = 1
else: # q==2
if k <= n:
Z_mark[k] = 1
# compute prefix sums
Y_prefix = [0](n+1)
Z_prefix = [0](n+1)
for i in 1..n:
Y_prefix[i] = Y_prefix[i-1] + Y_mark[i]
Z_prefix[i] = Z_prefix[i-1] + Z_mark[i]
total_lit = 0
for i in 1..n:
allowed_x = i - cntX[i]
allowed_y = i - Y_prefix[i]
allowed_z = i - Z_prefix[i]
total_lit += allowed_x * allowed_y * allowed_z
print total_lit
Now we need to implement lower_bound on array end (1-indexed). In C++, we can use:
int idx = lower_bound(end.begin(), end.end(), k) - end.begin();
But with end[0]=0, and we want index from 1. Since end[0] may be less than k, lower_bound will return first element >= k, which could be at position 0 if k <= 0, but k≥1 and end[0]=0, so it will return index >=1. So it's fine. But careful: end[0]=0, and if k=0 (not possible), it would return 0. So safe.
But we need to ensure that we have end array of size n+1. So we can do:
vector end(n+1);
for (int i=1; i<=n; i++) end[i] = (long long)i*(i+1)/2;
// then
auto it = lower_bound(end.begin()+1, end.end(), k); // search from index 1 to n inclusive.
int idx = it - end.begin();
Since end.begin()+1 gives iterator to element at index1, but lower_bound on that range returns iterator within that range, but end.end() is at index n+1 (one past last). So if k > end[n], it will return end.end(), which is beyond. But we have already checked k <= X_max = end[n], so it will be within range. Then idx is the index.
Alternatively, we could do binary search manually.
Now test with n=3, end = [0,1,3,6]. lower_bound from begin()+1 for k=4 returns iterator to end[3]=6, idx=3. Good.
Now for k=3, lower_bound returns first >=3, which is end[2]=3, idx=2. That corresponds to cube2? But x=3 belongs to cube2? Cube2 x range: 2,3. So yes, cube2 includes x=3. So correct.
Now for k=1, lower_bound returns end[1]=1, idx=1. Cube1 includes x=1. Good.
Thus lower_bound works.
Now the solution is ready.
But before finalizing, consider if there is any subtlety: The actions are performed sequentially, but since they only turn off lights and we are counting final lit state, order doesn't matter. So union approach is fine.
Now we must consider the possibility that a room may be turned off by both a type0 and a type1 action. Our counting method ensures it's not counted because we require x not in X and y not in Y. So correct.
Now double-check the interpretation of "the same action can only be given once". Could this be interpreted as: Each of the three persons only acts once? No, because sample2 has two actions by Hart. So it's definitely about duplicate (q,k). So no duplicate.
Now, consider if there is a type0 action with k that falls within the x-range of a cube i but the cube i has some y or z restrictions? Already accounted.
Now check for large n, like 50000, and many actions. Our loops are fine.
Now let's consider an alternative scenario: If we had both type0 and type1 actions, is there any interaction that might cause a room to be counted as lit even if it was turned off by one of them? No.
Thus solution stands.
Now we should produce final answer.
But let's think about whether we need to consider that after some actions, a room might be turned off then turned on again? No, only turn off actions.
Thus final answer: implement as described.
Now we'll produce a C++ solution.
Implementation details:
- Use #include <bits/stdc++.h> using namespace std;
- typedef long long ll;
- main() {
int n, m; cin >> n >> m;
ll Xmax = (ll)n * (n+1) / 2;
vector cntX(n+1, 0);
vector Ymark(n+1, 0);
vector Zmark(n+1, 0);
vector end(n+1);
for (int i=1; i<=n; i++) end[i] = (ll)i*(i+1)/2;
for (int t=0; t<m; t++) {
int q; ll k; cin >> q >> k;
if (q == 0) {
if (k > Xmax) continue;
// find i using lower_bound
int idx = lower_bound(end.begin()+1, end.end(), k) - end.begin();
cntX[idx]++;
} else if (q == 1) {
if (k <= n) Ymark[k] = 1;
} else { // q==2
if (k <= n) Zmark[k] = 1;
}
}
vector Ypref(n+1,0), Zpref(n+1,0);
for (int i=1; i<=n; i++) {
Ypref[i] = Ypref[i-1] + Ymark[i];
Zpref[i] = Zpref[i-1] + Zmark[i];
}
ll total_lit = 0;
for (int i=1; i<=n; i++) {
ll allowed_x = i - cntX[i];
ll allowed_y = i - Ypref[i];
ll allowed_z = i - Zpref[i];
total_lit += allowed_x * allowed_y * allowed_z;
}
cout << total_lit << endl;
return 0;
}
Check for potential overflow: allowed_x, allowed_y, allowed_z are int up to 50000, product up to 1.25e14, we store in ll. So fine.
Now, what about k possibly being up to 2e9, so we need to read as long long? Yes, k may be up to 2e9, which fits in 32-bit signed (2e9 < 2^31-1 ~2.147e9). So int can hold up to 2.147e9, but 2e9 is okay. However, some k could be 2e9 exactly, which is within int range. But if n=50000, Xmax ≈ 1.25e9, also within int. But sum might exceed 2^31? Not for n=50000. However, to be safe, we can use long long for k since constraints say up to 2e9, but that's within int, but using long long avoids issues. So we'll use long long for k.
Now test with sample.
Now consider possible pitfalls: The lower_bound on end from begin()+1 yields correct index but we must ensure that end is sorted. It is.
Now what if there are no type0 actions? Then cntX remains zero; allowed_x = i. Works.
Now what if there are type0 actions but k equals some value that exactly maps to a cube that doesn't exist? Already checked with k <= Xmax.
Now is there any scenario where type0 action with k might correspond to an x coordinate that is in cube i but the cube i's y or z maximum is less than something? Already accounted.
Now confirm that we haven't missed any nuance: The problem says "the three decided to sneak into a room where they could manage the electricity in all the rooms and play a trick." That might imply they have a control panel that can turn off whole sets. But it's fine.
Thus the answer is correct.
But wait, we need to consider that the actions are performed m times, and each action is by one of the three. They may try to turn off lights in places where there is no room. Our code skips those.
Now I'm thinking about the phrase "The same action can only be given once". Could it also mean that each person can only act once? That would conflict with sample2; maybe sample2 is a special case? Let's re-read problem statement: "Each of the three took the following actions. * A-chan turned off the lights in all the kth rooms from the west. * Licca turned off the lights in all the kth rooms from the south. * Hart turned off the lights in all rooms on the k floor.
Find the number of rooms that are lit after such mischief has been done m times."
Later: "The same action can only be given once"
It might be ambiguous: Could it mean that for each person, the action described (turning off the kth rooms) is done exactly once? But then m would be at most 3. But m can be up to 50000. So obviously multiple actions per person are allowed. So it must mean that duplicate (q,k) lines are not given.
Thus fine.
Now let's double-check our understanding with another custom test.
Test: n=1, m=1, action 0 1. Then cube1: i=1, one room. Turn off x=1 => all rooms off. Lit=0.
Our code: Xmax=1; cntX[1]=1; Ypref[1]=0; Zpref[1]=0; i=1: allowed_x=1-1=0 => total_lit=0. Good.
Test: n=1, m=1, action 1 1. Y_mark[1]=1; Ypref[1]=1; allowed_y=1-1=0 => lit=0. Good.
Now test: n=2, actions: 0 2, 0 3. That turns off x=2 and x=3. So all x are off? Cube1 has x=1, cube2 has x=2,3. So x=1 remains. So lit rooms = those with x=1: cube1 has 1 room, but also check y,z: cube1 i=1, so one room. So lit=1. Our code: cntX[1]=0? k=2 => cube2 idx=2; k=3 => cube2 idx=2? Actually both in cube2. So cntX[2]=2. But i=2 width is 2, so cntX[2] = 2 = i, allowed_x=0. i=1 allowed_x=1. allowed_y for i=1: Ypref[1]=0 ->1, allowed_z=1 ->1 => 1. i=2 allowed_x=0 => 0. total=1. Good.
Now test with overlapping: n=2, actions: 0 2, 1 2. cntX[2]=1, Y_mark[2]=1. For i=1: Ypref[1]=0 => allowed_y=1; allowed_z=1; allowed_x=1 =>1. For i=2: Ypref[2]=1 => allowed_y=1; allowed_z=2; allowed_x=1 (since i=2, cntX[2]=1, so allowed_x=1) => 112=2. So total lit=3. Let's enumerate: cube1: (1,1,1) -> y=2? no, y=1, so lit. cube2: rooms (2,1,1),(2,1,2),(2,2,1),(2,2,2),(3,1,1),(3,1,2),(3,2,1),(3,2,2). Off x=2: remove (2,,). Off y=2: remove all with y=2: (2,2,1),(2,2,2),(3,2,1),(3,2,2). Combined off: (2,1,1),(2,1,2) also off due to x; (2,2,1),(2,2,2) off due to both; (3,2,1),(3,2,2) off due to y. So remaining lit: (3,1,1),(3,1,2). That's 2 rooms. Plus cube1's 1 room gives 3. So matches.
Thus correct.
Now check a scenario where there is both type0 and type1 with the same k and also type2 maybe. No issues.
Now we must consider memory and time: arrays of size 50001, fine.
Now let's consider if we need to handle when an action is given that targets a coordinate that is within the global range but the room may not exist because for that x, the y or z might be beyond i? But the action definition turns off all rooms with that x, and all y,z that satisfy y≤i and z≤i are existing. So it's fine.
Now confirm that we did not need to consider the order of actions, as it's just union.
Thus solution is final.
Now we need to produce a final answer with explanation.
We'll produce a C++ program.
But before finalizing, think about potential integer overflow in computing end[i] for i up to 50000: i*(i+1)/2 ~1.25e9, fits in 32-bit signed? 1.25e9 < 2.147e9, so int works. But we used long long to be safe and avoid overflow when multiplying. Actually if we use int for i, i*(i+1) might overflow 32-bit? For i=50000, i*(i+1)=50000*50001=2.5e9, which exceeds 2^31-1 (2.147e9) if using 32-bit signed int, causing overflow. So we need to use long long for computation of end[i] and for Xmax. So use long long.
Thus we should cast to long long.
Now implement:
long long Xmax = (long long)n * (n+1) / 2;
vector end(n+1);
for (int i=1; i<=n; i++) end[i] = (long long)i * (i+1) / 2;
Now for lower_bound, we need a vector of long long.
Now for cntX array, we can use int (since max count is i <=50000). Ymark and Zmark can be bool or int.
Now I'll write code accordingly.
Now there is a nuance: When using lower_bound on vector with begin()+1, the iterators are fine.
Now test with n=50000, m=50000 random. Should run quickly.
Now we should consider that we might need to handle input where k is larger than 2^31? k up to 2e9 fits in 32-bit, but use long long anyway.
Now write final answer.
But wait, the problem statement: "The same action can only be given once" could also mean that each person can only do the action once, but then m could be up to 50000? That would contradict. So it's about duplicate (q,k). So fine.
Now final solution.
But let's also think about alternative possibility: Could the building be defined such that each floor has i horizontal rooms, meaning only 1 row? And the south-north dimension is not multiple rooms, but perhaps the rooms are arranged in a line along the south-north direction? But then Rika-chan's action "kth rooms from the south" would be ambiguous: if there's only one row, then all rooms are at distance? Actually if the building is just a line of cubes, maybe they have multiple rooms in the north-south direction as well. So i x horizontal rooms could be "i x i"? I'm convinced.
Now finalize.
But before concluding, let's consider if there is any twist: The phrase "each floor has i x horizontal rooms." Might mean each floor has i rooms in the horizontal direction (west-east) and also maybe i rooms in the vertical direction? Actually "horizontal rooms" might refer to rooms that are arranged horizontally (i.e., on the same floor), but the number could be i? Without specifying the other dimension. However, the fact that there is an action "kth rooms from the south" indicates there is a south-north ordering of rooms. So there must be multiple rows. So it's i rows.
Thus, the model i x i per floor is correct.
Thus answer.
Now I'll produce final solution explanation and code. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
// total number of west‑east columns
ll Xmax = (ll)n * (n + 1) / 2;
// cntX[i] – how many x‑coordinates in cube i are turned off
vector<int> cntX(n + 1, 0);
// Ymark[k], Zmark[k] – whether the k‑th y or z is turned off
vector<int> Ymark(n + 1, 0), Zmark(n + 1, 0);
// end[i] = i(i+1)/2 – the last x‑coordinate belonging to cube i
vector<ll> end(n + 1);
for (int i = 1; i <= n; ++i) {
end[i] = (ll)i * (i + 1) / 2;
}
for (int t = 0; t < m; ++t) {
int q;
ll k;
cin >> q >> k;
if (q == 0) { // A‑chan: west‑east
if (k <= Xmax) {
// find the cube containing x = k
int idx = lower_bound(end.begin() + 1, end.end(), k) - end.begin();
cntX[idx]++;
}
} else if (q == 1) { // Rika‑chan: south‑north
if (k <= n) Ymark[k] = 1;
} else { // Hart: floor
if (k <= n) Zmark[k] = 1;
}
}
// prefix sums for Y and Z
vector<int> Ypref(n + 1, 0), Zpref(n + 1, 0);
for (int i = 1; i <= n; ++i) {
Ypref[i] = Ypref[i - 1] + Ymark[i];
Zpref[i] = Zpref[i - 1] + Zmark[i];
}
ll totalLit = 0;
for (int i = 1; i <= n; ++i) {
ll ax = i - cntX[i]; // allowed x in cube i
ll ay = i - Ypref[i]; // allowed y (≤ i)
ll az = i - Zpref[i]; // allowed z (≤ i)
totalLit += ax * ay * az;
}
cout << totalLit << '\n';
return 0;
}