Recognize that a queen attacks a point if they share the same row, column, or diagonal. Precompute hash maps (or sorted lists) for each row, column, and diagonal (both main and anti) that store the count of queens on that line. For each query, sum the counts from the four lines that pass through the query point, but subtract 3 times the number of queens exactly at the query point to avoid overcounting (since a queen at the query point is counted in all four lines).
Pro tip: Clarify whether queens can occupy the same position as a query point and whether multiple queens can share the same position; this affects the counting logic. Also, mention that using hash maps gives O(1) per query after O(N) preprocessing, which is optimal for large inputs.
A queen attacks a point if they share the same row, column, or either diagonal. So for a query (x, y), we need to count queens on row y, column x, diagonal y - x, and anti-diagonal y + x.
Create four hash maps: one for rows, one for columns, one for main diagonals (key = y - x), and one for anti-diagonals (key = y + x). Each map stores the count of queens on that line.
For each query (x, y), look up the counts in the four maps using the appropriate keys and sum them. This gives the total number of attacks, but queens at the query point are counted four times.
If there are queens exactly at (x, y), they are included in all four line counts. Subtract 3 times the number of queens at (x, y) to count them only once. If no queen is at the query point, no adjustment is needed.
Preprocessing takes O(N) time and space, where N is the number of queens. Each query takes O(1) time. This is optimal for large numbers of queries.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Extend the previous solution by incorporating rocks as obstacles that block attacks. For each queen, scan in all 8 directions and stop when encountering a rock or the query point; only count the query if it is the first non-rock cell in that direction. Preprocess rock positions into a hash set for O(1) lookup, and handle each query in O(1) by checking the 8 directions from the query's perspective.
Pro tip: Clarify whether rocks are static or can be added/removed; if static, preprocess a nearest-obstacle map for each queen to answer queries in O(1) instead of scanning. Also, consider edge cases like multiple rocks in a line and the query point itself being a rock.
Ask about grid size, number of queens, rocks, and queries, and whether rocks are static or dynamic. This determines whether preprocessing is worthwhile.
Represent rocks in a hash set for O(1) membership checks. For each queen, the attack in a direction stops at the first rock or the query point.
For each query, check the 8 directions from the query point. In each direction, find the nearest queen and ensure no rock lies strictly between them. Count if valid.
If many queries, precompute for each queen the nearest rock in each direction, or for each cell the nearest queen in each direction. Use this to answer queries in O(1).
Compare brute-force scanning (O(Q * (N+M))) vs. preprocessing (O(N*M + Q) with O(N*M) memory). Discuss time-space trade-offs and choose based on constraints.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.