We need to solve a problem: given n rows, m columns (grid initially white). Pasha will perform k moves sequentially, coloring a specific cell (i, j) black (if already black, remains black). He loses when a 2x2 square (submatrix of 2 rows and 2 columns) becomes all black. We need to output the move number (1-indexed) when that first occurs, or 0 if never.
Constraints: n,m up to 1000, k up to 1e5. So we need O(k) or O(k log something) solution.
We can simulate the process and after each move, check if this move creates a new 2x2 all-black square. However, note that a move could potentially affect up to four 2x2 squares (since a cell is a corner of up to four such squares, depending on position). So we can check these squares after each move to see if they've become all black. But we must ensure we only detect the first occurrence. Since k up to 1e5 and checking up to 4 squares per move is fine, as long as checking a square's status is O(1). That is, we need to know for each cell whether it's black. We can maintain a boolean array n x m (size up to 1e6, which is okay). Then for each move, mark cell as black (if already black, do nothing? But note: the move might be on an already black cell. In that case, we still consider it a move but the state doesn't change. Could it cause a 2x2 square? The square might already have been all black earlier; but the loss condition is when a 2x2 square consisting of black pixels is formed. If it's already formed earlier, we would have lost on that earlier move. So if a move paints a cell that is already black, it cannot create a new all-black 2x2 because no change. So we can simply skip checking if it's already black. However, we must be careful: Could there be a scenario where a 2x2 square was already formed earlier, but we didn't detect because it was formed earlier? No, because we check each move. So we detect the first time a square becomes all black. So if a move paints a cell already black, it does not change state, so cannot create a new all-black square that wasn't already all black. So we can ignore such moves for checking.
So algorithm:
- Initialize grid (n+1 x m+1) all false (white).
- For move index t from 1 to k:
- read i, j
- if grid[i][j] is already true: continue (no change)
- else set grid[i][j] = true.
- Then consider up to four 2x2 squares that have (i,j) as one of their four cells:
- Top-left corner: if i >=2 and j >=2 -> square with cells (i-1,j-1), (i-1,j), (i,j-1), (i,j). Check if all four are black.
- Top-right corner: if i >=2 and j <= m-1 -> (i-1,j), (i-1,j+1), (i,j), (i,j+1)
- Bottom-left: if i <= n-1 and j >=2 -> (i,j-1), (i,j), (i+1,j-1), (i+1,j)
- Bottom-right: if i <= n-1 and j <= m-1 -> (i,j), (i,j+1), (i+1,j), (i+1,j+1)
- If any of these squares is all black (all four cells true), then output t and exit.
- After loop, output 0.
That's it! Complexity O(k). Memory O(n*m) which is up to 1e6, fine.
But is there any subtlety? The grid is 1-indexed, we can allocate size [n+2][m+2] to simplify bounds checks.
Edge Cases:
- n=1 or m=1: then no 2x2 square exists at all, so we never lose. Our checks: for any cell, none of the four squares will be valid because either i>=2? false etc. So we'll never output a move. That's correct.
- If a square becomes all black due to multiple moves, we detect on the move that completes it.
But wait: There is a nuance: The move may paint a cell that is already black. In that case, if we skip checking, we might miss the scenario where a 2x2 square becomes all black exactly because the same cell is being colored again? But it's already black; the square was already all black? No, because if the cell is already black, the square cannot become all black if it wasn't already all black: the only way a square becomes all black is that all four cells are black. If one of them is already black, the others might be black after some moves, but the last missing cell would be the one we are painting now. So if we are painting a cell that is already black, it means that cell was black before, so it was already part of the square; if the square becomes all black, it would require that the other three were black, but then the square already had that cell black; but the square could have been missing one of the other three; painting this cell again doesn't change those. So no new complete square. So skip is fine.
Thus, simple solution works. But wait: The problem statement says: "In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black." So moves can be redundant. And we need to determine when a 2x2 square consisting of black pixels is formed. That means, when it first becomes all black. So detection only on moves that change a cell from white to black.
Thus, O(k) is fine.
But, we need to be careful: n,m up to 1000, grid 1e6 boolean is fine. k up to 1e5, checking up to 4 squares per move is 4e5 checks, each check involves 4 lookups, that's 1.6e6 operations, trivial.
Let's test with examples:
Example1: 2x2, 4 moves: (1,1), (1,2), (2,1), (2,2). After (2,2) we check squares: bottom-right? Actually for (2,2), valid squares: top-left (since i>=2,j>=2): check (1,1),(1,2),(2,1),(2,2) all true -> output 4. Good.
Example2: 2x3. Moves:
1: (2,3) -> set; check squares: top-left? i=2>=2 j=3>=2? yes if j>=2 -> top-left square includes (1,2),(1,3),(2,2),(2,3). (2,2) not black, others? only (2,3) black. Not all black. Top-right? j<=m-1? m=3 so j<=2? j=3 >2 -> no. Bottom-left? i<=n-1? n=2 so i<=1? i=2 >1 -> no. Bottom-right? i<=1? no. So no.
2: (2,2) -> set; check squares: (2,2) valid squares: top-left (i>=2,j>=2): check (1,1),(1,2),(2,1),(2,2). Currently (1,1) white, (1,2) white, (2,1) white. Not all. top-right? j<=2? j=2 <=2 so top-right exists: check (1,2),(1,3),(2,2),(2,3). (1,2) white, (1,3) white, (2,2) black, (2,3) black. Not all. bottom-left? i<=1? i=2 >1 no. bottom-right? i<=1? no. So no.
3: (1,3) -> set; check squares for (1,3): top-left? i>=2? no. top-right? i>=2? no. bottom-left? i<=1? yes i=1 <=1? n=2, i<=1, and j>=2? j=3 >=2, so bottom-left: check (1,2),(1,3),(2,2),(2,3). (1,2) white, (1,3) black, (2,2) black, (2,3) black -> not all. bottom-right? i<=1 and j<=2? j=3>2 -> no. So no.
4: (2,2) -> already black, skip.
5: (1,2) -> set; check squares: (1,2) squares: top-left? i>=2? no. top-right? i>=2? no. bottom-left? i<=1? yes, j>=2? yes, bottom-left: check (1,1),(1,2),(2,1),(2,2). (1,1) white, others black -> not all. bottom-right? i<=1 and j<=2? j=2 <=2, bottom-right: check (1,2),(1,3),(2,2),(2,3). Now (1,2) black, (1,3) black, (2,2) black, (2,3) black -> all black! So output 5. That matches example.
6: (1,1) not executed because output already.
So correct.
Third example should output 0.
Thus, trivial solution passes.
But wait: The constraints: n,m up to 1000, k up to 1e5. However, some online discussions (like Codeforces problem "Pasha and Phone"?) Actually this is a known problem: CF 169A? Not sure. But I recall a problem "Pasha and Phone" maybe? There is a known problem: "A. Pasha and Hamsters" or something. The problem described seems similar to "A. Little Pony and Sort by Shift"? No. I think it's Codeforces Round # 179 problem? Actually it's Codeforces problem 139A? Not sure. It might be "374A - Inna and Pink Pony"? No.
But anyway, simple.
One potential memory optimization: we could use bitset, but boolean array fine.
Potential pitfall: Input coordinates are 1-indexed; we just use as is.
Implementation details: We'll define a 2D boolean array, perhaps vector<vector> grid(n+2, vector(m+2, false)). Then for each move, if !grid[i][j], then grid[i][j]=true, then check:
if (i>=2 && j>=2 && grid[i-1][j-1] && grid[i-1][j] && grid[i][j-1] && grid[i][j]) then found.
if (i>=2 && j<=m-1 && grid[i-1][j] && grid[i-1][j+1] && grid[i][j] && grid[i][j+1]) then found.
if (i<=n-1 && j>=2 && grid[i][j-1] && grid[i][j] && grid[i+1][j-1] && grid[i+1][j]) then found.
if (i<=n-1 && j<=m-1 && grid[i][j] && grid[i][j+1] && grid[i+1][j] && grid[i+1][j+1]) then found.
Note: For the last two, we need to ensure i+1 <= n, j+1 <= m, but condition i<=n-1 ensures i+1<=n; similarly j<=m-1 ensures j+1<=m.
Since we have already set grid[i][j] true, we can include it in condition, but it's redundant. Could also check all four cells explicitly.
Time is fine.
Thus, answer is straightforward.
But wait, there's a catch: In the problem statement, "In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black." So moves are not necessarily all distinct; but our detection only on changes.
However, what if a square becomes all black not on a move that paints the last missing cell, but because of a move that paints a cell that is already black? Impossible.
Thus, solution works.
But we need to produce code accordingly.
Let's think about edge cases: If k = 0? Not allowed (k>=1). n,m >=1.
Potential overflow? None.
We'll implement in C++.
Pseudo:
#include
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;
cin >> n >> m >> k;
vector<vector> black(n+2, vector(m+2, false));
for (int move = 1; move <= k; ++move) {
int i,j;
cin >> i >> j;
if (black[i][j]) continue;
black[i][j] = true;
// check squares
if (i >= 2 && j >= 2) {
if (black[i-1][j-1] && black[i-1][j] && black[i][j-1]) {
cout << move << endl;
return 0;
}
}
if (i >= 2 && j <= m-1) {
if (black[i-1][j] && black[i-1][j+1] && black[i][j+1]) {
cout << move << endl;
return 0;
}
}
if (i <= n-1 && j >= 2) {
if (black[i][j-1] && black[i+1][j-1] && black[i+1][j]) {
cout << move << endl;
return 0;
}
}
if (i <= n-1 && j <= m-1) {
if (black[i][j+1] && black[i+1][j] && black[i+1][j+1]) {
cout << move << endl;
return 0;
}
}
}
cout << 0 << endl;
return 0;
}
Note: In the checks, we omitted checking black[i][j] since it's true. We need to check all four cells: For top-left: (i-1,j-1),(i-1,j),(i,j-1),(i,j). Since (i,j) is true, check the other three. So as above.
Similarly for others.
Be careful with condition boundaries: For top-right: condition i>=2 and j<=m-1 (i.e., j+1 <= m). Then check (i-1,j),(i-1,j+1),(i,j+1) and implicitly (i,j). Actually need to check (i-1,j) and (i-1,j+1) and (i,j+1) and (i,j) true, so check three others.
But note: There's a square using (i,j) as bottom-left? Actually we have four squares: (i,j) as top-left, top-right, bottom-left, bottom-right. In the code above:
-
For (i,j) as bottom-right? Actually our first check is top-left: the square with (i,j) as bottom-right? Wait, we considered "top-left corner: if i>=2 and j>=2 -> square with cells (i-1,j-1),(i-1,j),(i,j-1),(i,j)". Here (i,j) is bottom-right of that square. But that's one of the squares containing (i,j). It's fine.
-
Second: "top-right": if i>=2 and j<=m-1 -> square with (i-1,j),(i-1,j+1),(i,j),(i,j+1). Here (i,j) is bottom-left? Actually it's bottom-left? Let's see: the square has corners (i-1,j) top-left, (i-1,j+1) top-right, (i,j) bottom-left, (i,j+1) bottom-right. So (i,j) is bottom-left.
-
Third: "bottom-left": if i<=n-1 and j>=2 -> square with (i,j-1),(i,j),(i+1,j-1),(i+1,j). (i,j) is top-right? Actually corners: (i,j-1) top-left, (i,j) top-right, (i+1,j-1) bottom-left, (i+1,j) bottom-right. So (i,j) is top-right.
-
Fourth: "bottom-right": if i<=n-1 and j<=m-1 -> square with (i,j),(i,j+1),(i+1,j),(i+1,j+1). (i,j) is top-left.
Thus we cover all squares containing the cell (i,j). In the checks we only need to test if all four cells are black. Since we just turned (i,j) black, we need to check the other three. So for each valid square, we check the other three.
We must be careful in conditions: For bottom-left square, the condition i<=n-1 (i+1 <= n) and j>=2 (j-1 >=1). Then check (i,j-1) left neighbor, (i+1,j-1) bottom-left, (i+1,j) bottom. For top-right square, check (i-1,j) up, (i-1,j+1) up-right, (i,j+1) right. etc.
But there is subtlety: The square might have been already all black before this move? That would have been detected earlier, so not possible. But if we mistakenly check only three neighbors, we might get a false positive if the three neighbors are black but the square's fourth cell is not (i,j) but some other cell that is not black? But in each square, the missing cell to check is exactly (i,j) and three others. So checking those three ensures all four are black. However, consider scenario: For bottom-left square, the three cells we check are (i,j-1), (i+1,j-1), (i+1,j). But what about (i,j)? It's black now. So if all three are black, square is complete. Good.
But we must also consider the possibility that some of the cells we check might be out of bounds? We already have conditions ensuring the square exists, and indices are within grid. So safe.
Thus code above works.
We must note that using vector<vector> can be slow due to bit packing and access? But it's fine for up to 1e5 moves and 1e6 cells.
Alternatively, we could use vector<vector> or bitset, but bool is okay.
Potential alternative: Use a 2D integer array of char: vector<vector> grid(n+2, vector(m+2, 0)). Might be faster due to no bit compression issues. I'll use char.
We'll implement:
vector<vector> grid(n+2, vector(m+2, 0));
Then set grid[i][j] = 1; etc.
All good.
Now, let's test with sample inputs manually using code.
We'll also test with n=1,m=5,k=3 moves (1,1),(1,2),(1,3) -> no square, output 0.
If a square becomes all black on a move that also completes more than one square simultaneously? That's possible if painting a cell that is corner of multiple squares that become all black at the same time. But we just need to output the move number; we can stop at first detection.
Potential tricky: The move could create two squares simultaneously (e.g., painting a cell that completes two squares at once). Our checks will detect one of them, and we output the move. That's fine.
Now, is there any scenario where painting a cell that is already black could lead to a square becoming all black? Suppose that due to previous moves, three cells are black, and the fourth cell is white. If we paint the fourth cell, it becomes black and we detect. So it's a change. If we paint an already black cell, the fourth remains white. So no.
Thus solution is correct.
But wait, reading problem statement: "Pasha loses the game when a 2 × 2 square consisting of black pixels is formed." Could it be that a 2x2 square is considered formed at the moment when all four pixels become black, regardless of order? Yes. So we detect when the last of the four is colored.
Thus answer.
But there might be a twist: The field is rectangular, but n and m up to 1000, but k up to 1e5. That's fine.
Now, let's think about complexity: O(k). Memory O(n*m) ~1e6.
All good.
However, I recall a known Codeforces problem: "466A - Cheap Travel"? No. Actually the problem "Losing Game" might be from Codeforces Round # 224 (Div. 2) - A? Not sure. But there is also a problem called "A. Little Pony and Sort by Shift"? No.
One nuance: The problem statement says: "Pasha loves his phone and also putting his hair up... But the hair is now irrelevant." That's just a story.
We'll proceed.
But I'm ChatGPT, I need to produce a final answer with explanation and code.
Thus, final solution:
Algorithm:
- Maintain a 2D array marking black cells.
- For each move in order:
- If the cell is already black, do nothing.
- Otherwise, mark it black and check all 2x2 squares that contain this cell (up to 4). If any of these squares now have all four black cells, output the move number and exit.
- If loop finishes, output 0.
Complexities: O(k) time, O(n*m) memory.
Now let's consider if we can reduce memory: Since k <= 1e5, we could store only black cells in a hash set, but then checking squares would require looking up up to 3 other cells in the set, which is O(1) average but hash set overhead. But n*m up to 1e6, which is acceptable. So we can stick with 2D array.
Thus final code.
We'll provide C++ code.
Potential improvements: Use bitset for rows, but not needed.
Now, let's double-check boundaries: For m=1000, n=1000. We need indices up to n+1 and m+1 for safety. Condition i <= n-1 means i+1 <= n, i.e., i <= n-1. Using i+1 <= n is fine.
In code:
if (i > 1 && j > 1 && grid[i-1][j-1] && grid[i-1][j] && grid[i][j-1]) ...
Similarly, if (i > 1 && j < m && grid[i-1][j] && grid[i-1][j+1] && grid[i][j+1]) ...
if (i < n && j > 1 && grid[i][j-1] && grid[i+1][j-1] && grid[i+1][j]) ...
if (i < n && j < m && grid[i][j+1] && grid[i+1][j] && grid[i+1][j+1]) ...
Note: Use 1-indexed; grid dimensions [n+2][m+2] to avoid out-of-bounds when accessing i+1 or j+1 even when condition fails; but we condition anyway.
Better to allocate grid as vector<vector> (n+2, vector(m+2,0)) so that indices 0 and n+1, m+1 are accessible but will be zero.
Then conditions: if (i>1 && j>1 && grid[i-1][j-1] && grid[i-1][j] && grid[i][j-1]) ...
But we also need to ensure i-1, j-1 etc. are within bounds; they are since i>1, j>1.
Similarly for others: i>1 && j<m ensures j+1 <= m. But careful: j<m because j is <= m. If j==m then j<m false. Good.
i<n ensures i+1 <= n. etc.
Now test with n=2,m=2: For move (2,2): i=2, j=2. Conditions: i>1 && j>1 true -> check (1,1),(1,2),(2,1). If all black, detect. Others: i>1 && j<m? j<m? 2<2 false; i<n && j>1? i<n? 2<2 false; i<n && j<m? false. So only first. Good.
For move (1,1): i=1,j=1: Only condition that might be true is i<n && j<m? 1<2 && 1<2 true -> check (1,2),(2,1),(2,2). That's square with (1,1) as top-left? Actually bottom-right? Wait: Our fourth condition is i<n && j<m and checks grid[i][j+1], grid[i+1][j], grid[i+1][j+1]. That corresponds to square where (i,j) is top-left. So for (1,1), it checks (1,2),(2,1),(2,2). So if those three are black, square (1,1)-(2,2) becomes all black. That's correct.
Thus we covered all.
Now, consider the scenario where the cell is painted and creates a square where the cell is not the last missing? Actually it becomes the last missing because before painting, it was white, so the square had at most three black cells. After painting, it becomes the fourth. However, it's possible that before painting, the square already had all four black cells? That would mean the cell was already black, but we are painting it now? That would be impossible because we only do this for white cell. So can't be.
Thus detection correct.
But what about the case where painting a white cell might cause two squares to become complete simultaneously, as noted. We detect at the first check; but the move number is same for both, so it's fine.
Now confirm with a test: n=3,m=3. Suppose moves: (1,1),(1,2),(2,1),(2,2) -> after (2,2) we get square (1,1)-(2,2) complete. But also (1,2) is part of square (1,2)-(2,3) etc. Not relevant. Our detection: For (2,2), we check top-left (1,1),(1,2),(2,1) all black -> detect. Good.
Now if we had (2,2) already black from earlier, then we later paint (1,1) that completes square? Actually painting (1,1) would check bottom-right condition (i<n && j<m) for (1,1) and detect if (1,2),(2,1),(2,2) are black. So detection works.
So code is correct.
Now, concerning performance: 1e5 moves, each doing up to 4 checks of 3 boolean accesses, so about 1.2e6 boolean checks, trivial.
Thus final answer.
But before finalizing, let's think if there is any subtlety about the input size: n,m up to 1000 => grid size up to 1e6; vector<vector> of that size (1e6 bytes) is okay. If we use vector<vector>, it may be packed as bits, but access might be slower due to bitwise operations; but still fine for 1e6. However, some compilers have performance issues with vector due to proxy objects. Safer to use vector<vector> or vector<vector>.
We'll use vector<vector> or char.
We'll also use fast I/O.
Now produce final code.
But let's also consider an alternative approach: using a count of black cells per 2x2 square. For each 2x2 square, maintain count of black cells; when count becomes 4, output move. That would be O(1) per move, but we need to update counts for all squares that include the painted cell. That's up to 4 squares. So essentially the same. Our approach of checking the squares directly is similar. But using counts might be simpler: maintain an array of counts for each top-left corner of possible 2x2 squares. Initially all zeros. When we paint a white cell (i,j), for each of up to 4 squares (with top-left corners: (i-1,j-1), (i-1,j), (i,j-1), (i,j)), we increment the count. If after increment the count becomes 4, we output move. This method also works and avoids checking three other cells each time. However, we need to be careful: If the square count is 4, it means all four cells are black. But what about duplicate painting? If we increment count for a square multiple times for the same cell (if we paint a cell that was already black but we erroneously increment again), we would get count >4 and possibly false detection after a later move? But we only increment when the cell changes from white to black. So we need to ensure we don't increment for moves that paint already black cells. So we can use same condition: only when cell becomes black. Then for each affected square, increment its count. When count reaches 4, that square is completed on that move.
But note: The count array size would be (n-1)(m-1) up to 999999 ~ 1e6, still fine. This alternative is also O(k) and may be cleaner because we don't need to check three neighbors each time; we just update counts. However, either works.
But we must consider: When a cell is painted black, it might already be black, but we skip. For count method, we must not increment counts for already black cells. So we need to check if it's already black; if not, for each square, increment count. Then if any count becomes 4, output move. This method might be slightly more efficient, but both are okay.
I'll stick with direct neighbor check because simpler to understand, and we don't need to maintain extra count array.
But, note: In direct neighbor check, we are checking the neighbor cells. However, if the grid is large but moves are few, most cells are white; the checks will quickly find a white neighbor and fail. That's okay.
One more nuance: The square could be formed by cells that are all black, but the last move might not be the one that paints the last cell? Obviously it is. So detection on that move.
Thus we are good.
Now implement.
Let's write final answer in C++.
Potential pitfalls: Input reading: use scanf or cin with sync. We'll use fast cin/cout.
We'll also include cstdlib? Not needed.
Now final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<char>> black(n+2, vector<char>(m+2, 0));
for (int move = 1; move <= k; ++move) {
int i, j;
cin >> i >> j;
if (black[i][j] == 1) {
continue;
}
black[i][j] = 1;
// Check the 2x2 square with (i,j) as bottom-right
if (i > 1 && j > 1 && black[i-1][j-1] && black[i-1][j] && black[i][j-1]) {
cout << move << "\n";
return 0;
}
// Check the 2x2 square with (i,j) as bottom-left
if (i > 1 && j < m && black[i-1][j] && black[i-1][j+1] && black[i][j+1]) {
cout << move << "\n";
return 0;
}
// Check the 2x2 square with (i,j) as top-right
if (i < n && j > 1 && black[i][j-1] && black[i+1][j-1] && black[i+1][j]) {
cout << move << "\n";
return 0;
}
// Check the 2x2 square with (i,j) as top-left
if (i < n && j < m && black[i][j+1] && black[i+1][j] && black[i+1][j+1]) {
cout << move << "\n";
return 0;
}
}
cout << "0\n";
return 0;
}
We can also compress by using loops but it's fine.
Test with sample.
Now I'll produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
// grid with an extra border to simplify bound checks
vector<vector<char>> black(n + 2, vector<char>(m + 2, 0));
for (int move = 1; move <= k; ++move) {
int i, j;
cin >> i >> j;
// already black → nothing changes
if (black[i][j]) continue;
black[i][j] = 1;
// check the four possible 2x2 squares containing (i,j)
// (i,j) is bottom‑right
if (i > 1 && j > 1 && black[i-1][j-1] && black[i-1][j] && black[i][j-1]) {
cout << move << "\n";
return 0;
}
// (i,j) is bottom‑left
if (i > 1 && j < m && black[i-1][j] && black[i-1][j+1] && black[i][j+1]) {
cout << move << "\n";
return 0;
}
// (i,j) is top‑right
if (i < n && j > 1 && black[i][j-1] && black[i+1][j-1] && black[i+1][j]) {
cout << move << "\n";
return 0;
}
// (i,j) is top‑left
if (i < n && j < m && black[i][j+1] && black[i+1][j] && black[i+1][j+1]) {
cout << move << "\n";
return 0;
}
}
cout << "0\n";
return 0;
}