We need to compute for each queen the number of other queens it threatens, considering that along each of the 8 directions (N, NE, E, SE, S, SW, W, NW), a queen threatens only the first queen encountered along that ray (closest queen in that direction). So essentially, for each queen, we need to see if there is a queen in each of the 8 directions that is the nearest in that direction (i.e., minimal positive distance along that direction) and count that as a threat. It is symmetric? Actually, if queen A threatens queen B in a direction, then queen B does not necessarily threaten queen A in the opposite direction if there is a queen between them? Wait, the rule: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." So for a given queen, along each direction, if there are multiple queens, only the closest one is considered threatened. This is per queen. So it's not symmetric. So we must compute threats for each queen independently.
We have up to 1e5 queens on board up to 1e5 size (but coordinates up to n). Straightforward checking all pairs O(m^2) is impossible.
We need to efficiently compute, for each queen, the number of directions in which there is at least one other queen along that direction (ray) and the closest one (so we just need to know if there is any queen in that direction; actually we need to count a threat if there is at least one queen in that direction, but wait: What about multiple queens? The rule says: only the first queen is under attack. So if there is at least one queen in that direction, the given queen threatens exactly one queen in that direction (the closest). So the queen's w (number of other queens threatened) is simply the number of directions among 8 for which there is at least one other queen along that ray (with no blocking queen? Actually, the queen itself is the starting point; the direction ray is from this queen outward. If there is any queen in that direction, the nearest one is attacked. However, if there are multiple queens, still only one is considered threatened. So essentially w = count of directions where there exists at least one other queen on that ray. Because the presence of any queen means there is a closest one. So w ∈ [0,8].
But careful: Could a queen be blocked by another queen that is not the closest? The rule: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." This means that if there are multiple queens along the same direction, the queen threatens only the nearest one. The others are not threatened because they are "behind" the first queen? Actually, yes. The queen can see through? In standard chess, queen can move any number of squares but cannot jump over pieces; if there is a piece, it blocks further squares. So typically, a queen attacks only the first piece encountered. The problem statement: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." So if there are multiple queens along a direction, only the nearest is considered under attack. So from the perspective of the attacking queen, it threatens exactly one queen per direction if there is at least one queen in that direction. So w = number of directions with at least one queen.
Thus the problem reduces to, for each queen, among the 8 axis-aligned and diagonal lines (rows, columns, two diagonals), check whether there is any other queen on that line on each of the two opposite sides? Actually, each direction is separate. There are 8 directions: north (increasing row? Actually rows numbered top to bottom from 1 to n; so row 1 is top. Typically in such problems, "north" would be decreasing row (since row number increases downward). But directions: horizontal: left (decreasing column) and right (increasing column); vertical: up (decreasing row) and down (increasing row); diagonal: up-left (decreasing row, decreasing column), up-right (decreasing row, increasing column), down-left (increasing row, decreasing column), down-right (increasing row, increasing column). So 8 directions.
So for each queen, we need to know if there is any queen with same row to the left (c' < c) - that is west. Similarly same row to the right (east). Same column above (r' < r) - north. Same column below (south). For diagonals: same (r-c) constant for one diagonal (NW-SE) or (r+c) constant for the other (NE-SW). For each direction, we need to know if there is a queen on that ray from the current queen.
Thus we can solve by scanning all queens along each dimension. For each queen, we can find the nearest neighbor in each of four axes: left, right, up, down, and along the two diagonals (both directions). Actually, we need only existence of any queen, not necessarily nearest. But to determine existence efficiently, we can sort the positions along each axis.
Given m up to 1e5, we can process:
- For each row, collect all queens in that row, sorted by column.
- For each column, collect all queens in that column, sorted by row.
- For each diagonal (r-c) constant, collect all queens on that diagonal, sorted by row (or column).
- For each anti-diagonal (r+c) constant, collect all queens on that diagonal, sorted by row.
Then for each queen, we can query in its row: using binary search to check if there is any queen with column less than current (i.e., left direction). If the index in sorted list > 0, then there exists a queen to the left. Similarly, if index < size-1, there exists a queen to the right.
Similarly for column: check up (row less) and down (row greater).
For diagonal (r-c): we need to define the two directions: NW (decreasing row, decreasing col) and SE (increasing row, increasing col). On the diagonal, both coordinates increase together. If we sort by row, then the preceding elements have smaller row and smaller col (NW direction), following elements have larger row and larger col (SE). So we can check: if not the first in the sorted list, then there is a queen in NW direction; if not the last, then there is a queen in SE direction.
For anti-diagonal (r+c): constant sum. For a given queen, increasing row corresponds to decreasing column, and decreasing row corresponds to increasing column. So on this diagonal, if we sort by row (or by column) we can differentiate. Typically, if we sort by row ascending, then a preceding element (smaller row) will have larger column (since sum is constant) => that direction is NE? Actually, let's map directions:
- NW: row-1, col-1 (both decrease)
- NE: row-1, col+1 (row decreases, col increases)
- SE: row+1, col+1 (both increase)
- SW: row+1, col-1 (row increases, col decreases)
Now, on the diagonal where r-c is constant, the coordinates vary like (r, c) = (base + k, base + k). So moving along this diagonal increases both row and col. So if we sort by row ascending, then elements before (smaller row) also have smaller col => that is NW direction (since both smaller). Elements after have larger row and larger col => SE direction. So that's correct.
On anti-diagonal r+c constant, if we sort by row ascending, then for a given queen, a queen with smaller row will have larger column (since sum constant: c = sum - r), so that direction is NE? Because row smaller, col larger => that is NE direction. And a queen with larger row will have smaller column => SW direction. So indeed, using sorted list by row, preceding gives NE, succeeding gives SW.
But careful: "NE" direction is defined as decreasing row and increasing column. So yes, that matches preceding element (row smaller, col larger) if we sort ascending by row. So we can use the same logic: for anti-diagonal, if there exists an element before in the sorted order, then there is a queen in NE direction; if there exists an element after, then there is a queen in SW direction.
However, need to be careful about the direction definitions relative to the board rows increasing downward. In the problem, they use row numbers from top to bottom, so "north" is decreasing row, "south" increasing row. So we've got that.
Thus we can compute for each queen w = (left?1:0)+(right?1:0)+(up?1:0)+(down?1:0)+(NW?1:0)+(NE?1:0)+(SE?1:0)+(SW?1:0). Then accumulate t[w] for w from 0 to 8.
We need to check if there is any queen exactly in that direction, i.e., there exists any queen on that ray. For the row direction, it's clear: if there is any queen on the same row with column less than current, then left exists. Similarly for right. For column similarly.
For diagonals, we need to ensure that the adjacent queen in the sorted list is actually along the ray direction, i.e., no need to consider that there might be a queen on the same diagonal but in the opposite direction? Actually, the sorted list includes all queens on that diagonal. If there is any queen with row less than current on that diagonal, then it is in the NW direction if diagonal is r-c constant, because col = row - (r-c) constant. For r-c constant, row less implies col less, so yes, it's NW. Similarly, for anti-diagonal, row less implies col greater, so it's NE. So preceding elements give the direction that has both row less? Actually for anti-diagonal, row less means col = sum - row larger, so that direction is NE (row less, col more). So that is correct. So we just need to check existence of any element before/after.
But wait: is it possible that there is a queen on the same row but with column less, but there is another queen in between? That doesn't matter; we just need existence. So we can simply check whether the queen is not the leftmost in its row. However, careful: if there is a queen left of it, that queen might be blocked by another queen that is closer? No, because the queen's threat direction left is just looking left; the nearest queen is the first encountered. But for counting w, we only care if there is any queen at all; we don't need to know if it's blocked by something else? In a row, there is no obstacle except other queens. But the queen can see through empty squares; the only thing that could block is if there is a queen in between? Actually, if there are multiple queens to the left, the nearest one is the first attacked, but the existence of any queen means there is a threat (the closest one). So w counts that direction as threatened if there is at least one queen, regardless of whether there is a queen even further. So simply existence is enough. However, we need to consider that a queen may have a queen exactly adjacent in that direction? That's still existence.
Thus algorithm: For each queen, we need to know if there exists another queen on same row with column < c; and column > c; same column row < r; row > r; on same diagonal r-c with row < r; row > r; on same anti-diagonal r+c with row < r; row > r. That yields 8 boolean values.
Implementation: Use maps/dictionaries to group queens by row, column, diag1 = r-c, diag2 = r+c. For each group, sort the positions appropriately (by column for rows, by row for columns, by row for diag1 and diag2). Then for each queen, we can binary search in the corresponding sorted list to find its index. We'll need to do this efficiently.
We have up to 1e5 queens. So we can pre-process: For each queen, store its coordinates and also compute diag keys. Then we need to create these groups: For each row, we will have a list of (col, queen_index) maybe. Similarly for columns: list of (row, queen_index). For diag1: list of (row, col, queen_index) but we can store just row (since col = row - d? Actually for diag1, given row we can compute col = row - d, but it's okay to store both row and col for simplicity, but sorting by row works). For diag2: store (row, queen_index) and col = sum - row.
To speed up, we can after grouping, sort each list. Then for each queen, we look up its row list and find its position using binary search (maybe store indices after sorting, or we can precompute for each queen its rank in each group). Since each queen belongs to exactly one row, one column, one diag1, one diag2. So total groups: number of distinct rows, columns, diag1, diag2. Sum of sizes across groups is m each for rows, columns, diag1, diag2, i.e., 4m. So we can sort each group: total sorting cost O(m log m) per grouping? Actually, we sort each group individually; total sorting complexity is sum over groups (size_i log size_i) which is <= m log m (since sum of size_i log size_i <= (log max_size) sum size_i, but max size can be up to m. Actually, worst-case if all queens share the same row, then row group size m, sorting that takes O(m log m). Similarly columns: all queens could share same column (but they can't share both row and column because positions are unique; but they could all be in same row, same column no, each queen unique. They could all be in same diag1? Possibly. So worst-case sorting all four groups might each be O(m log m) in the case where all queens are on a single row (row group large) but column group: each queen has different column, so column group sizes maybe each queen unique column? Actually, if all queens are on same row, then they are on different columns. So column groups: each column appears exactly once (since columns all distinct because positions are unique) so each column group size 1. So sorting columns groups trivial. Similarly diag1 groups: each queen on different diag1? For same row, diag1 = r-c varies because c differs, so they are distinct. So diag1 groups size 1. Same for diag2: each queen might be on distinct sum? r+c = row + col; with same row and varying col, sums vary, so distinct. So worst-case for one group could be m, but the sum of sorting across all groups could be up to 4 * m log m? Actually, row group sorting O(m log m), column groups collectively O(m log 1?) but sum of (size_i log size_i) across all column groups: each size_i is the number of queens in that column. In worst-case, all queens could be in same column (m in one column) => column group size m, sorting that is O(m log m). Similarly, they could also all be in same diagonal? But can they all be in same row and same column? No because positions unique. However, worst-case input could be all queens on a single diagonal (both r-c constant) and also on same row? Actually, on a single diagonal, they have distinct rows and columns. So they are not in same row or same column, so row groups each small. So it's possible that one of the four grouping categories has a large group of size m, but it's unlikely that all four have large groups simultaneously because that would require queens to be all aligned in multiple ways, which is impossible unless m=1. For m>1, you cannot have all queens on same row and same column and same diagonal and same anti-diagonal simultaneously. But the worst-case total sorting complexity could be, say, row group size m, column group size 1 each, diag1 group size m (if all on same diagonal), diag2 group size 1 each? Actually, can a set of queens be both on same row and same diagonal? If they are on same row, then all have same r. For them to be on same diagonal r-c constant, we would need c = r - const, so all have same r and c? Actually, r-c constant = r - c, if r is constant, then c must be constant to keep r-c constant. So all would have same column too, i.e., same position, impossible. So row group and diag1 group cannot both be large simultaneously. Similarly, row group and anti-diag group: r+c constant, if r constant, then c constant, same position. So they are mutually exclusive. Similarly, column group and diag1: if c constant, for r-c constant, r must be constant, so all same row -> impossible for distinct positions. So at most two of these groups could be large? Actually, consider queens all on a line with slope 2? But diagonals defined by slopes ±1 only. So we only consider those four alignments. For a set of points, being collinear on a row is a specific line. Being collinear on a column is another. Being collinear on a 45° diagonal is another. Being collinear on a -45° anti-diagonal is another. A set of distinct points can be on at most two of these families? Actually, a point belongs to one row, one column, one diag1, one diag2. A set of points can be all on the same row and also on the same diag1 only if they are all the same point (contradiction). However, they could be all on the same row and also all on the same anti-diagonal? For a fixed row, anti-diagonal sum = r+c varies with c, so cannot be constant unless all c same, again same point. So indeed, for distinct points, they cannot all lie on the same row and same diagonal (45°) unless m=1. Similarly for same row and same column. So at most one of the four groupings can have size m. But consider all points on a line with slope 2; they are not on any of the considered directions? Actually, they would not be grouped together in any of these groups except each point belongs to its own row, column, diag, etc. So no group size large. So the worst-case sorting complexity across all groups is O(m log m) because total sum of size_i log size_i is bounded by m log m times a constant? Let's bound: For each group, size_i log size_i ≤ size_i log m (since size_i ≤ m). So sum over groups of size_i log size_i ≤ (∑ size_i) log m = (4m) log m = O(m log m). Because there are 4 categories, each point appears exactly once per category. So total sum of sizes across all groups is 4m. Then sum of size_i log size_i ≤ ∑ (size_i log m) = (∑ size_i) log m = 4m log m = O(m log m). So total sorting is O(m log m).
After sorting, we need to, for each queen, find its index in its row list, column list, diag1 list, diag2 list. We could for each group, after sorting, iterate through the list and assign each queen its index (position) and also perhaps precompute left/right existence. For example, for each row group (list of queens sorted by col), we can go through the list and for each queen, we know if there is a left neighbor (if index>0) and right neighbor (if index<len-1). We can store these two booleans for each queen from row perspective. Similarly for column, diag1, diag2. Then combine.
Thus we can do:
-
Create arrays of length m to store for each queen: has_left, has_right, has_up, has_down, has_NW, has_NE, has_SE, has_SW. Initially false.
-
Group queens by row: map<int, vector<pair<int,int>>> where key=row, value = (col, queen_index). For each row, sort vector by col. Then iterate through sorted vector; for each element at position i, if i>0 then queen_index gets has_left=true; if i<size-1 then queen_index gets has_right=true.
-
Group by column: map<int, vector<pair<int,int>>> key=col, value = (row, queen_index). Sort by row. For each, if i>0 -> has_up (since smaller row) true; if i<size-1 -> has_down true.
-
Group by diag1: key = r - c. For each queen, compute d1 = r - c. Group as map<int, vector<pair<int,int>>> storing (row, queen_index) or (col, queen_index). Since on this diagonal, sorting by row is same as sorting by col. So we store (row, queen_index). Sort by row. Then iterate: if i>0 -> has_NW true (since smaller row implies NW). If i<size-1 -> has_SE true.
-
Group by diag2: key = r + c. Group as map<int, vector<pair<int,int>>> storing (row, queen_index). Sort by row. Then iterate: if i>0 -> has_NE true (since smaller row -> NE). If i<size-1 -> has_SW true.
Then for each queen, compute w = sum of the 8 booleans. Increment t[w].
Complexity: O(m log m) due to sorting, but we can also use O(m) if we use counting sort? But n up to 1e5, but coordinates up to n, but m up to 1e5. Sorting is fine.
Edge Cases: For directions, we must ensure that we count threats only if there is at least one queen in that direction. The method using sorted list and checking neighbors works because if there is any queen to left, then the queen is not the leftmost, so there is at least one queen with col less. However, what about the possibility that there is a queen to left but also a queen further left? That's fine; we just need existence. So correct.
But we need to ensure we are not counting a queen that is exactly on the same ray but maybe not the first? No, we don't care about first; existence is enough. So fine.
However, is there any scenario where a queen could threaten a queen but there is another queen in between? In that case, the queen would threaten the nearest queen, which is the one in between. So there is still a threat (the in-between queen). So the existence of any queen in that direction, regardless of distance, implies there is a threat. Because even if there is a closer queen, that closer queen is threatened. So existence of any queen is sufficient.
Thus the algorithm is correct.
Double-check examples:
First example: 8 4 queens: (4,3), (4,8), (6,5), (1,6). Let's compute manually.
Queen (4,3): Row 4: there is also (4,8) to the right, so has_right, no left. Column 3: no other queen. Diag1: r-c=1: (4-3=1). Are there other queens with same r-c? (6,5): 6-5=1 also. So same diagonal. Sorted by row: (4,3) row4, (6,5) row6. So has_SE? For (4,3), not first? Actually, there are two; (4,3) is before (6,5) since row4<row6, so i=0, size=2 => has_SE true (since there is a queen after, i<size-1), and no NW (i>0? false). So has_SE. Diag2: r+c=7: (4+3=7). Others? (1,6): 1+6=7 also. So same anti-diagonal. Sorted by row: (1,6) row1, (4,3) row4. So for (4,3), i=1 (since row1, row4), so i>0 true => has_NE? Actually, preceding element (row1, col6) has smaller row, larger col, that is NE direction? For (4,3), decreasing row to 1 and increasing col to 6 is NE? From (4,3), moving up and right to (1,6): that's indeed NE. So yes, has_NE true. Also i<size-1? size=2, i=1 not <1, so no SW. So far threats: right, SE, NE = 3 directions. Also check other diagonals? No other. So w=3.
Queen (4,8): Row 4: left has (4,3), right? no. So left. Column 8: any? (1,6) col6; (6,5) col5; (4,3) col3; none col8 besides itself? Actually, only (4,8) has col8, so no up/down. Diag1: r-c= -4? 4-8=-4. Any other? (1,6): 1-6=-5; (6,5):1; no. So none. Diag2: r+c=12. (6,5):11; (1,6):7; (4,3):7; none. So threats: left only, w=1.
Queen (6,5): Row 6: only queen in row 6? Actually, only (6,5). So no left/right. Column 5: only queen col5? Others have col3,8,6? (1,6) col6, (4,3) col3, (4,8) col8. So none. Diag1: r-c=1, with (4,3). Sorted: (4,3) row4, (6,5) row6 => i=1, so has_NW (since preceding row4 gives NW), no SE. Diag2: r+c=11. Any other? None. So threats: NW, w=1.
Queen (1,6): Row1: only queen row1? Actually (1,6) only, so no left/right. Column6: any other? (4,3) col3; (4,8) col8; (6,5) col5; none col6? Only (1,6). So no up/down. Diag1: r-c=-5? 1-6=-5; none else. Diag2: r+c=7, with (4,3). Sorted: (1,6) row1, (4,3) row4. For (1,6), i=0, so has_SW (since after element gives SW? Actually, after has larger row, smaller col = SW). So has_SW true. So w=1.
Thus w values: 3,1,1,1. So t0=0, t1=3, t2=0, t3=1, others 0. Output: 0 3 0 1 0 0 0 0 0. matches example.
Second example: n=10, m=3 (1,1),(1,2),(1,3). All in same row. For each:
Queen(1,1): has right? yes (since there are queens with col>1), no left. Column1: only? Actually col1 only (1,1) so no up/down. Diag1: r-c=0, any other? (1,2):1-2=-1; (1,3):1-3=-2; none. Diag2: r+c=2, (1,1) sum2; (1,3) sum4; (1,2) sum3; none same? Actually (1,1) sum2 only itself. So no diagonals. So threats: right only -> w=1.
Queen(1,2): row: left (col1) yes; right (col3) yes -> two directions. Column2: only itself, so no up/down. Diag1: r-c=-1, any? (1,1):0, (1,3):-2, no. Diag2: sum=3, any? (1,1):2, (1,3):4, no. So w=2.
Queen(1,3): row: left yes, right none. Column3: only itself. Diag1: -2 none. Diag2:4 none. So w=1.
Thus t1=2, t2=1, others 0. Output: 0 2 1 0 ... matches.
Thus algorithm works.
Implementation details:
-
Input n,m (n may not be used except for bounds, but we don't need board representation).
-
Read m lines, store r[i], c[i]. Compute d1 = r-c, d2 = r+c.
-
Create arrays: left(m,0), right(m,0), up(m,0), down(m,0), nw(m,0), ne(m,0), sw(m,0), se(m,0) as bool or int.
-
For rows: use unordered_map<int, vector<pair<int,int>>> rows; for each queen i, rows[r[i]].push_back({c[i], i}); After reading, for each row, sort its vector by first (col). Then iterate: for (size_t j=0; j<vec.size(); j++) { int idx = vec[j].second; if (j>0) left[idx]=1; if (j<vec.size()-1) right[idx]=1; }
-
For columns: unordered_map<int, vector<pair<int,int>>> cols; key= c[i]; push {r[i], i}; sort by r; iterate: if j>0 up[idx]=1; if j<size-1 down[idx]=1.
-
For diag1: unordered_map<int, vector<pair<int,int>>> d1map; key = r[i]-c[i]; push {r[i], i}; sort by r; iterate: if j>0 nw[idx]=1; if j<size-1 se[idx]=1. (Because preceding: smaller row => NW; succeeding: larger row => SE). Check: Is it always that a smaller row on same diagonal corresponds to NW? For d1, (r-c) constant, if row is smaller, col = row - const is also smaller, so yes. So NW.
-
For diag2: unordered_map<int, vector<pair<int,int>>> d2map; key = r[i]+c[i]; push {r[i], i}; sort by r; iterate: if j>0 ne[idx]=1; if j<size-1 sw[idx]=1. (Preceding: smaller row => larger col => NE; succeeding: larger row => smaller col => SW). Confirm: For d2, row smaller => col = sum - row larger, so direction is up-right = NE. Yes. And larger row => col smaller => down-left = SW.
Alternatively, we could store both row and col and check direction explicitly but this is simpler.
Potential pitfalls: For diag2, if we sort by row, the order is consistent: increasing row. So preceding element has smaller row, larger col (NE). But what about the case where two queens have same row? That would imply same sum? If same row and same sum, then col must be same (since sum = r + c), so they would be same position, which is impossible because positions unique. So no duplicates within same diagonal group? Actually, two queens could have same sum and same row? That would require same c, same r, same position, not allowed. So within a diag group, rows are distinct because if two queens share same diagonal (r-c constant) but have same row, then c would be same (since c = r - const) => duplicate position. Similarly for anti-diagonal, if same row then same col. So rows are distinct in each diag group. So sorting by row yields unique ordering.
Similarly, for columns: within a column group, rows are distinct because positions unique. For rows: within a row group, columns are distinct.
Thus no ties in sorting key. So stable sorting not needed.
Edge Cases: m=1, then all groups have size 1, all booleans false => w=0 => t0=1, others 0.
Now, complexity: We'll use unordered_map for grouping. However, we could also use map (ordered) but unordered_map is fine. However, we need to iterate over keys; unordered_map's iteration is fine. Sorting each vector; total O(m log m).
Memory: Storing 4 maps, each with vectors that hold pairs. Could be up to O(m). Acceptable.
Implementation steps:
-
Read n, m.
-
Create vectors r(m), c(m), maybe also vectors for bools. But we can use arrays of size m.
-
Loop i from 0 to m-1: read r[i], c[i].
- rows[r[i]].push_back({c[i], i})
- cols[c[i]].push_back({r[i], i})
- d1 = r[i]-c[i]; d1map[d1].push_back({r[i], i})
- d2 = r[i]+c[i]; d2map[d2].push_back({r[i], i})
-
Process rows: for each auto& [row, vec] in rows: sort(vec.begin(), vec.end()) (since default sort by first then second, but we only need by col; first is col, second is index). Then iterate j: left/right as described.
-
Process cols: for each auto& [col, vec] in cols: sort(vec.begin(), vec.end()) (by row). Then assign up/down.
-
Process d1map: for each auto& [key, vec]: sort(vec.begin(), vec.end()) (by row). Then assign nw, se.
-
Process d2map: for each auto& [key, vec]: sort(vec.begin(), vec.end()) (by row). Then assign ne, sw.
-
Initialize array t[9] = {0}. For i=0..m-1, compute w = left[i]+right[i]+up[i]+down[i]+nw[i]+ne[i]+sw[i]+se[i]; t[w]++.
-
Output t[0] to t[8] separated by spaces.
Check: Did we assign left/right correctly? For rows, left is column smaller; in sorted order ascending by col, indices increase with column. So if j>0, there is a queen with smaller col => left. That is correct. For j<size-1, there is larger col => right. Good.
For columns, sorted by row ascending. If j>0, there is a queen with smaller row => up (north). Yes. If j<size-1 => down.
For d1: sorted by row ascending. If j>0 => smaller row => NW. If j<size-1 => larger row => SE.
For d2: sorted by row ascending. If j>0 => smaller row => NE. If j<size-1 => larger row => SW.
Make sure naming: NW, NE, SE, SW correspond to correct directions.
Double-check: For d2, SW is row+1, col-1. That's larger row, smaller col. In sorted list, larger row comes after, so yes j<size-1 => has_SW. Good.
Now, one more test: Consider queen on anti-diagonal with multiple queens. For example, (2,2) and (3,1) share sum=4? (2+2=4, 3+1=4). Sorted by row: (2,2) row2, (3,1) row3. For (2,2): i=0; has_SW (since after exists) => direction to (3,1) is row+1, col-1 = SE? Wait, (3,1) relative to (2,2): row+1, col-1 => that is actually SE? Actually, SE is row+1, col+1. SW is row+1, col-1. So (3,1) is down-left => SW. Yes. So for (2,2), the after gives SW. And for (3,1): i=1; has_NE from preceding (2,2) which is row-1, col+1 => NE. Good.
For d1, (2,2) and (3,3) share d1=0? Actually 2-2=0, 3-3=0. Sorted: (2,2) row2, (3,3) row3. For (2,2): after -> (3,3) row+1, col+1 => SE; preceding none. For (3,3): preceding -> (2,2) row-1, col-1 => NW. Good.
Thus correct.
Now, does the algorithm double count any direction? For each queen, we count each of 8 directions once. So w is correct.
Now, is there any nuance: The problem says: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." So if there is at least one queen in that direction, the queen threatens that first queen. So w counts that direction. If there are no queens, it doesn't threaten. So yes.
Now, some may think about blocking: If there is a queen in a direction but there is also a queen even closer? That doesn't change existence. So counting existence is sufficient.
Thus solution is straightforward.
But is there any scenario where a queen might not threaten any queen in a direction even if there is a queen? Suppose there is a queen directly adjacent in that direction; obviously threatens. Suppose there is a queen further away but there is a queen even closer; then the queen threatens the closer one, but still threatens someone. So still counts.
Thus existence of any queen in that direction means w gets +1. So our method is fine.
Potential pitfalls: Could there be a queen in the same row but with column less, but the queen's line of sight is blocked by another queen that is even closer? Actually, along a row, if there are multiple queens to left, the closest one is the immediate left neighbor (if sorted by col). That one is the first encountered. So there is definitely a queen (the closest) that is threatened. So existence is true if there is any queen to left, regardless of distance. So leftmost condition is sufficient.
Thus algorithm stands.
Now, let's think about performance: using unordered_map for keys up to 2e5 range (r, c, differences can be negative as low as -n+1 to n-1, sums up to 2n). So we can use map or unordered_map. unordered_map should be fine. But to avoid hashing overhead, we could also use a sorted vector approach: store all queens, sort by row, then process rows? But we need to quickly know for each queen its position in its row. Doing grouping with map is simpler.
Alternative: We can sort all queens by (row, col) and then iterate to process rows: as we traverse sorted by row then col, we can detect per row consecutive groups. But also need column groups, etc. That would be more complex.
Given constraints, maps are fine.
Potential memory: we store 4 maps each containing vectors of pairs. Each vector for a key. So total memory O(m). Acceptable.
Implementation details in C++:
- Use vector< pair<int,int> > for each grouping.
- Use unordered_map<int, vector<pair<int,int>>>.
- For d1 and d2, key could be negative, so unordered_map supports int keys.
But unordered_map may be slower than map for some cases, but m=1e5 it's fine.
Alternatively, we could use map to avoid rehashing but it's okay.
We'll need to be careful to reserve maybe.
We'll also need to allocate arrays for booleans: maybe using vector of size m, initialized to 0. Then set to 1 when needed.
Pseudo:
int main() {
int n, m;
cin >> n >> m;
vector r(m), c(m);
unordered_map<int, vector<pair<int,int>>> rows, cols, d1, d2;
for (int i=0; i<m; i++) {
cin >> r[i] >> c[i];
rows[r[i]].push_back({c[i], i});
cols[c[i]].push_back({r[i], i});
int diag1 = r[i] - c[i];
d1[diag1].push_back({r[i], i});
int diag2 = r[i] + c[i];
d2[diag2].push_back({r[i], i});
}
vector left(m,0), right(m,0), up(m,0), down(m,0), nw(m,0), ne(m,0), sw(m,0), se(m,0);
// rows
for (auto& [, vec] : rows) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j=0; j<sz; j++) {
int idx = vec[j].second;
if (j>0) left[idx] = 1;
if (j<sz-1) right[idx] = 1;
}
}
// cols
for (auto& [, vec] : cols) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j=0; j<sz; j++) {
int idx = vec[j].second;
if (j>0) up[idx] = 1;
if (j<sz-1) down[idx] = 1;
}
}
// d1
for (auto& [, vec] : d1) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j=0; j<sz; j++) {
int idx = vec[j].second;
if (j>0) nw[idx] = 1;
if (j<sz-1) se[idx] = 1;
}
}
// d2
for (auto& [, vec] : d2) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j=0; j<sz; j++) {
int idx = vec[j].second;
if (j>0) ne[idx] = 1;
if (j<sz-1) sw[idx] = 1;
}
}
vector t(9,0);
for (int i=0; i<m; i++) {
int w = left[i]+right[i]+up[i]+down[i]+nw[i]+ne[i]+sw[i]+se[i];
t[w]++;
}
for (int i=0; i<=8; i++) {
cout << t[i] << " ";
}
cout << endl;
return 0;
}
Test with examples.
Now, confirm that the directions are correctly labeled: In d2, preceding gives NE (since up-right). In d2, succeeding gives SW (down-left). That matches the earlier naming: we used ne for NE, sw for SW. In d1: preceding NW, succeeding SE. Yes.
But we also have se and nw, ne, sw. Need to ensure we haven't swapped SE and SW accidentally. For d1: SE is both row and col increase; that is after (larger row). Good. For d2: SW is row increase, col decrease; that is after (larger row). Good.
Now, one more test: Consider queen (5,5) and another (7,3) on same anti-diagonal? sum=10? 5+5=10, 7+3=10. Sorted by row: (5,5) row5, (7,3) row7. For (5,5): after gives (7,3) which is down-left = SW. So sw=1. For (7,3): preceding gives (5,5) which is up-right = NE. So ne=1.
What about queen (5,5) and (3,7) sum=10? 3+7=10, row3 smaller. Sorted: (3,7), (5,5). For (5,5): preceding gives (3,7) which is up-right = NE. So ne=1. For (3,7): after gives (5,5) which is down-left = SW. So consistent.
Now, about up/down: For column, we set up for j>0 (smaller row) and down for j<sz-1 (larger row). Good.
Thus final solution.
Now, edge case: Large n but irrelevant. m up to 1e5, so loops fine.
One more nuance: The problem statement says: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." So if there is at least one queen, the queen threatens exactly one queen in that direction. So w counts each direction with at least one queen. This is correct.
Now, we should consider if there is any scenario where a queen might not threaten any queen in a direction because the first queen is actually itself? No.
Thus final.
Potential improvement: Instead of storing 8 separate arrays, we could combine into a single array of bits, but not needed.
Now, confirm that we correctly treat the directions as separate: there are 8 directions; each queen can threaten at most 8 others.
Now, what about time limit? Sorting within each group may be O(m log m) which is acceptable for 1e5.
Now, we might need to use fast I/O (ios_base::sync_with_stdio(false); cin.tie(0);) to handle large input.
Now, write final solution in C++.
We should also consider that using unordered_map may cause TLE if we have many collisions? But m=1e5, number of distinct keys at most m, but hashing int is trivial. It's fine.
Alternatively, we could use map to avoid possible issues, but map adds O(log number_of_keys) per insertion, which is also okay. However, overall insertion into maps is O(m log distinct_keys) ~ O(m log m). Sorting already O(m log m), so fine. But unordered_map average O(1) insertion might be faster. I'll use unordered_map.
But for unordered_map with int key, we need to include <unordered_map>. Also we may need to reserve or not necessary.
But one caution: In C++, when we iterate over unordered_map, the order doesn't matter. So fine.
Now, implement.
We'll also need to include for sort, , , etc.
Now, we might also store pairs as pair<int,int>; default sort works by first then second. Since first is the coordinate for sorting, and second is index. That's fine. In rows, we sort by col (first). In columns, by row (first). In diagonals, by row (first). All good.
Now, test on given examples.
Now, potential bug: For d2, we used sorting by row. For a given anti-diagonal, row uniquely determines col, but we need to ensure that the direction NE (up-right) corresponds to smaller row and larger col. Indeed, preceding element (smaller row) has larger col. So ne is correct. And SW (down-left) is larger row and smaller col, so after gives sw. Good.
Thus final answer.
But wait, consider direction naming: Actually, we have 8 directions: left (west), right (east), up (north), down (south), NW, NE, SE, SW. Our boolean arrays: left, right, up, down, nw, ne, se, sw. That's consistent.
Now, check whether we have any missing direction: The queen might threaten in a direction if there is any queen, regardless of whether there is a queen in the opposite direction as well. That's fine.
Now, we produce output: numbers separated by spaces, and maybe trailing space allowed as per examples.
Now, confirm with a custom case:
Case: n=3, m=2: (2,2) and (2,1). Queen (2,2): left exists? Yes (col1). Right none. Up? Column2: only (2,2). Down? none. Diag1: (2,2) d1=0, (2,1) d1=1? 2-1=1, not same. Diag2: (2,2) sum=4, (2,1) sum=3, not same. So threats: left only -> w=1. Queen (2,1): right exists? yes. So w=1. So t0=0, t1=2, others 0. Our algorithm: rows: row2 list: (1, idx1) and (2, idx0)? Actually, col1 and col2, sorted: (1, idx of (2,1)), (2, idx of (2,2)). For idx1 (col1): j=0 => left false, right true. For idx0 (col2): j=1 => left true, right false. So left/right correct. Columns: col1 list: (row2, idx1) only size1 => no up/down. col2 list: (row2, idx0) size1. So no up/down. d1: for (2,2) d1=0 list: (2, idx0) size1; for (2,1) d1=1 list: (2, idx1) size1; so no nw/se. d2: (2,2) sum4 list: size1; (2,1) sum3 list: size1. So w=1 each. Good.
Now, test with queens on same diagonal but not covering both directions: (2,2) and (4,4). For (2,2): row2 only? row2 only one; col2 only one; d1=0 list: (2,2) and (4,4) sorted rows: (2,2) j=0 => se true. d2: sums 4 and 8, different. So threats: se -> w=1. For (4,4): row4 only; col4 only; d1: j=1 => nw true. w=1. Good.
Now, test with queen having threats in multiple directions: (5,5) and queens at (5,3) (left), (5,7) (right), (3,5) (up), (7,5) (down), (3,3) (NW), (3,7) (NE), (7,3) (SW), (7,7) (SE). That would be 8 threats. Our algorithm should give w=8. Let's verify groups: Row5: columns [3,5,7] -> for queen (5,5) index=1 => left true, right true. Column5: rows [3,5,7] -> index=1 => up true, down true. Diag1 (r-c=0): queens (3,3), (5,5), (7,7) -> index=1 => nw true, se true. Diag2 (r+c=10): queens (3,7), (5,5), (7,3) -> index=1 => ne true, sw true. So w=8. Good.
Now, confirm that if there are multiple queens on the same ray, our algorithm still counts only one threat, which is correct. For example, row: queen at col1,2,3. For queen at col2, left exists (col1) and right exists (col3). That's two threats. But does queen at col2 also threaten col1? Yes, because it's the first to the left. It also threatens col3? Actually, to the right, first is col3, so yes. So w=2. Good.
If queen at col1, left none, right exists (col2). But note there is also col3 further right, but that's behind col2. Still queen at col1 threatens col2 (first to right). So w=1. So existence of any queen to the right suffices.
Thus algorithm works.
Now, what about queen's threat being blocked by another queen in a different direction? Not relevant.
Thus final answer is good.
Now, we can implement.
One more thing: The board size n is up to 1e5, but we don't need it.
Now, talk about memory: using 8 vectors of size m ints, each 4 bytes -> 32 bytes per queen -> 3.2 MB for m=1e5, fine. The maps each store vectors; total memory maybe 4 * (m * pair) ~ 4 * 1e5 * 8 bytes = 3.2 MB plus overhead. Fine.
Now, let's double-check potential off-by-one: In rows, we sort by col. For each queen, we set left if j>0; but if j>0 means there is a queen with col less, correct. Right if j<size-1. Good.
Now, verify that we didn't accidentally invert left/right. For row, left is west (decreasing column). In sorted ascending by col, lower col comes first. So if there is a lower col, that is left. So j>0 => left. Good.
Now, for column, up is north (decreasing row). Sorted ascending by row, lower row first. So j>0 => there is a smaller row => up. Good.
Now, for d1, nw is decreasing both row and col. Sorted ascending by row, lower row first (and lower col as well). So j>0 => smaller row => NW. Good.
Now, for d2, ne is decreasing row, increasing col. Sorted ascending by row, lower row first (and larger col). So j>0 => smaller row => NE. Good.
Thus all correct.
Now, confirm that we are not missing any direction. There are exactly 8 directions; we covered all: left, right, up, down, nw, ne, sw, se.
Now, edge case: m=0? But constraints: m ≥ 1? Actually, m can be 0? The problem says 1 ≤ n, m ≤ 10^5, so m≥1. So we don't handle m=0.
Now, check for large input: we need to use fast IO. Use ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
Now, let's write final code.
Potential micro-optimizations: Instead of using separate arrays for each direction, we could compute w on the fly while processing groups by incrementing w array directly. For instance, while processing rows, for each queen we can increment w_temp[i] by left? Actually, we can have an array w_count initialized to 0, and then add 1 for each direction as we discover it. That would reduce memory: we only need one array w of size m (or even just compute final w after processing all groups). Let's consider: we have 8 directions; we can in each grouping add to a cumulative w array. For rows: if j>0, w[idx] += 1 (left); if j<size-1, w[idx] += 1 (right). For columns: up/down. For d1: nw/se. For d2: ne/sw. Then after all, we have w[i] = total threats. Then t[w[i]]++. That uses only one array of ints size m. This is more efficient memory and simpler. So we can do that.
Thus implement:
vector w(m,0);
Then in rows processing:
if (j>0) w[idx]++; // left
if (j<sz-1) w[idx]++; // right
Similarly columns: up/down.
d1: nw/se.
d2: ne/sw.
Yes, that's simpler and avoids 8 arrays. So we'll do that.
Now, careful: For d1, we need to add for NW and SE. So if j>0 -> w[idx]++ (NW). if j<sz-1 -> w[idx]++ (SE). For d2: if j>0 -> w[idx]++ (NE). if j<sz-1 -> w[idx]++ (SW). That's it.
So final w sum includes all.
Now, check if we need to differentiate directions for counting? No, we just count each direction once if queen has any queen in that direction.
Thus we can implement as described.
Now, confirm with earlier examples:
Queen (4,3): row: j? In row 4 list: cols [3,8] sorted -> indices: (3, idx0) j=0? Actually, we need to compute index. For (4,3), col=3, in sorted row list: first element, j=0 -> left false, right true -> w+=1 (right). Column: col3 list: only (4,3) -> no up/down. d1: list for d1=1: (4,3) row4, (6,5) row6 -> j=0 -> no NW, SE true -> w+=1 (SE). d2: list for sum7: (1,6) row1, (4,3) row4 -> j=1? Actually sorted by row: (1,6) j=0, (4,3) j=1 -> j>0 => NE true -> w+=1 (NE). So total w=3. Good.
Thus correct.
Now, let's test with the 8 threat scenario: For queen (5,5) with 8 neighbors, row: index=1 -> left and right => w+=2. column: index=1 -> up and down => w+=2 => total 4. d1: index=1 -> NW and SE => w+=2 => total 6. d2: index=1 -> NE and SW => w+=2 => total 8. Good.
Thus algorithm works.
Now, we need to ensure that we don't double-add a direction incorrectly when a queen is on both a row and a diagonal? No, they are distinct directions.
Now, consider the scenario where a queen might have multiple queens in the same direction but we only add 1, which is correct.
Now, final implementation:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> r(m), c(m);
unordered_map<int, vector<pair<int, int>>> rows, cols, d1, d2;
for (int i = 0; i < m; ++i) {
cin >> r[i] >> c[i];
rows[r[i]].emplace_back(c[i], i);
cols[c[i]].emplace_back(r[i], i);
d1[r[i] - c[i]].emplace_back(r[i], i);
d2[r[i] + c[i]].emplace_back(r[i], i);
}
vector<int> w(m, 0);
// process rows
for (auto& [_, vec] : rows) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) w[idx]++; // left
if (j < sz - 1) w[idx]++; // right
}
}
// process columns
for (auto& [_, vec] : cols) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) w[idx]++; // up
if (j < sz - 1) w[idx]++; // down
}
}
// process main diagonal (r - c)
for (auto& [_, vec] : d1) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) w[idx]++; // NW
if (j < sz - 1) w[idx]++; // SE
}
}
// process anti-diagonal (r + c)
for (auto& [_, vec] : d2) {
sort(vec.begin(), vec.end());
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) w[idx]++; // NE
if (j < sz - 1) w[idx]++; // SW
}
}
vector<int> t(9, 0);
for (int i = 0; i < m; ++i) {
t[w[i]]++;
}
for (int i = 0; i <= 8; ++i) {
cout << t[i] << " ";
}
cout << "\n";
return 0;
}
Check: In d2, we assign NE when j>0, and SW when j<sz-1. This is consistent.
Now, verify using second example: queens (1,1),(1,2),(1,3). rows: row1 list sorted: (1, idx0), (2, idx1), (3, idx2). For idx0: j=0 => left false, right true -> w+=1; idx1: j=1 => left true, right true -> w+=2; idx2: j=2 => left true, right false -> w+=1. columns: col1: only idx0 => no up/down; col2: only idx1; col3: only idx2 => no adds. d1: for each queen: (1,1) d1=0; (1,2) d1=-1; (1,3) d1=-2 => each alone -> no adds. d2: sums: 2,3,4 => each alone. So w: idx0=1, idx1=2, idx2=1. t[1]=2, t[2]=1. Output matches.
Thus correct.
Now, test with m=1: rows: list size1 -> no adds. columns: size1. d1: size1. d2: size1. w[0]=0 => t[0]=1. Output: 1 0 0 0 0 0 0 0 0? But problem says t0...t8. Output 1 and zeros. That's fine.
Now, test with potential overlapping: For a queen on a line where there are queens on both sides, we count both sides separately, correct.
Now, consider correctness when there is a queen exactly in the same row but with lower column, but also a queen on same diagonal in that direction? That's separate.
All good.
Now, consider potential pitfalls: The keys for d1 and d2 may be negative. unordered_map works with negative ints. Good.
Now, consider that we used "auto& [_, vec]" in C++17. It's fine.
Now, some compilers may not support structured bindings with unordered_map? They do with C++17. But we could also use older style: for (auto& it : rows) { auto& vec = it.second; ... }.
But I'll keep C++17.
Now, ensure we include necessary headers: iostream, vector, algorithm, unordered_map.
Now, final answer.
But wait: The problem statement says: "For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack." That is exactly our interpretation. However, is there any nuance: The queen can only attack if the path is clear? But if there is a queen in between, the queen cannot attack beyond it. But we count the first queen as attacked. So yes.
Now, does the queen threaten the first queen even if the first queen is adjacent? Yes.
Now, is there any direction where the queen might not threaten any queen because the first piece is of another type? But only queens are on the board. So fine.
Thus solution is correct.
Now, we can produce final answer.
One more thought: Could there be a scenario where a queen has a queen directly in line but that queen is not the closest because there is a queen even closer? That's exactly our existence method. Existence of any queen ensures there is a closest queen, so we count that direction. So fine.
Now, consider if the queen has a queen in the same row to the left but also a queen in the same row to the right? Two threats. Fine.
Now, consider if the queen has a queen exactly in the same diagonal in the NW direction, but also another queen on the same diagonal further NW? That's still one threat for that direction.
Thus all good.
Now, final code.
But we should consider performance: Using unordered_map, the keys for rows and columns are from 1..n, which is up to 1e5, not huge. But we might have many distinct keys. Insertions into unordered_map may cause rehash. But m=1e5, it's fine.
Alternative: Use vectors of vectors indexed by row, but row numbers up to n=1e5, we can allocate arrays of size n+1 if n is up to 1e5. Actually, n can be as large as 1e5, so we could allocate vectors of size n+1 for rows and columns. That might be simpler and faster: vector<vector<pair<int,int>>> rows(n+1); But each row's list only for rows that actually have queens. That would allocate n+1 empty vectors, which is fine because n <= 1e5, that's only 100k vectors. Similarly for columns. For diagonals, the number of distinct d1 values can be up to 2n-1 (from -n+1 to n-1) which is about 2e5, still okay to allocate array? But we could use map for diagonals because range is larger? Actually, d1 range from -(n-1) to (n-1), total 2n-1 ≈ 2e5, which is manageable for array if we shift index. Similarly d2 range from 2 to 2n, total 2n-1 ≈ 2e5. So we could use vector of vectors for all four groups with index shifting. That would be even faster and avoid map overhead. But it's not necessary. However, given m=1e5, maps are fine. But for safety, we can use vectors with resizing. I'll keep maps for simplicity.
But note: n up to 1e5, but we could also allocate rows as unordered_map since number of rows with queens ≤ m, but we can also use map.
I'll keep unordered_map.
Now, note: Use of emplace_back is fine.
Now, finalize.
Potential bug: In the for loops, we used "auto& [_, vec]" but we didn't use the key. Using _ is okay, but some compilers might warn about unused variable. We can use "auto& kv" and then use kv.second.
But it's fine.
Now, final code.
Now, before final answer, we could also consider a more optimized approach using sort of all queens globally and scanning, but this is fine.
Now, I'll write final answer in C++ as described.
Now, test with random small case with brute force to ensure correctness. But not needed now.
We can trust.
Now, produce final output. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> r(m), c(m);
unordered_map<int, vector<pair<int, int>>> rows, cols, diag1, diag2;
for (int i = 0; i < m; ++i) {
cin >> r[i] >> c[i];
rows[r[i]].emplace_back(c[i], i);
cols[c[i]].emplace_back(r[i], i);
diag1[r[i] - c[i]].emplace_back(r[i], i);
diag2[r[i] + c[i]].emplace_back(r[i], i);
}
vector<int> threats(m, 0); // number of directions with at least one other queen
// process rows: left and right
for (auto& [_, vec] : rows) {
sort(vec.begin(), vec.end()); // sort by column
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) threats[idx]++; // left
if (j < sz - 1) threats[idx]++; // right
}
}
// process columns: up and down
for (auto& [_, vec] : cols) {
sort(vec.begin(), vec.end()); // sort by row
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) threats[idx]++; // up
if (j < sz - 1) threats[idx]++; // down
}
}
// process main diagonal (r - c): NW and SE
for (auto& [_, vec] : diag1) {
sort(vec.begin(), vec.end()); // sort by row (col follows)
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) threats[idx]++; // NW
if (j < sz - 1) threats[idx]++; // SE
}
}
// process anti-diagonal (r + c): NE and SW
for (auto& [_, vec] : diag2) {
sort(vec.begin(), vec.end()); // sort by row
int sz = vec.size();
for (int j = 0; j < sz; ++j) {
int idx = vec[j].second;
if (j > 0) threats[idx]++; // NE
if (j < sz - 1) threats[idx]++; // SW
}
}
vector<int> ans(9, 0);
for (int i = 0; i < m; ++i)
ans[threats[i]]++;
for (int i = 0; i <= 8; ++i)
cout << ans[i] << " ";
cout << "\n";
return 0;
}