We need to simulate turning sea cells into land, but only if after turning, there still exists a sea path from top row to bottom row, where adjacency includes left-right wraparound (cylindrical side). So we have a grid of r rows and c columns, with cylindrical adjacency in columns: (i,1) adjacent to (i,c). Also adjacent vertically (i,j) adjacent to (i+1,j). The top and bottom rows: top row is row 1, bottom row is r. We start with all sea (we consider sea as empty? Actually initially all cells are sea). We need to process n operations, each attempts to change a given cell from sea to land. We only perform if after change, there still exists a path of sea cells from any cell in top row to any cell in bottom row (or more precisely, there exists a sequence of sea cells from a top row cell to a bottom row cell). So we need to check connectivity of sea cells from top to bottom after removal (i.e., after turning that cell to land). If that connectivity is broken, we skip.
We need to output number of successful changes.
Constraints: r,c up to 3000 => grid up to 9 million cells. n up to 300k. So grid is large but not huge; 9 million cells maybe memory borderline: 9 million booleans ~ 9 MB fine. But we need to process connectivity queries dynamic deletions (sea cells become land). That's like we are deleting nodes from a graph, and need to know if after deletion, there still exists a top-bottom sea path. Deletion only, no re-insertion. Classic approach: reverse time: treat all operations as deletions from initial all-sea grid. At the end (after all deletions), some cells are land (deleted from sea). Then we have final sea grid. Then we add cells back in reverse order, and check when adding a sea cell would connect top and bottom? Wait, careful: In forward process, we start with all sea, then we try to turn sea to land, but only if after turning, there remains a top-bottom sea path. That is like we are allowed to delete a node only if the remaining graph remains connected from top to bottom. This is like maintaining a percolation? Another viewpoint: We want to keep a set of sea cells that always contains a top-bottom path. At each step, we try to remove a cell; if its removal would break all top-bottom paths, we reject (skip) and the cell remains sea. So effectively, we end up with a final set of sea cells that is a superset of the successful deletions? Actually the successful deletions are those we removed, while failed ones remain sea. So final sea set consists of all cells that were never successfully turned to land, i.e., cells that either were never attempted or attempted but rejected. So the final sea set is the initial all-sea minus the set of successful land conversions. So final sea set is exactly the set of cells that are sea after all operations. So we can compute final sea set by simulating forward with condition? But we need to decide which deletions are successful.
Alternative: Reverse time: Suppose we know which deletions were successful? Not known a priori. However, we can think of the process as we only delete a cell if it is not a "cut point" for top-bottom connectivity. In other words, we maintain a minimal "spanning" structure? Might be similar to the classic problem "blocked cells cannot block water flow" where we have to ensure water can go from top to bottom through empty cells. This is like computing the set of cells that are "articulation points" for the connectivity between top and bottom. But dynamic deletion.
Given constraints, we might use union-find with offline approach: We process operations in reverse: Starting from the final state after all operations (but we don't know final state because some operations might be rejected). However, we can treat all attempted cells as "to be potentially land" and we need to decide which of them actually become land. This looks like we need to simulate the process exactly.
Maybe we can use a technique: Maintain a union-find of sea cells, but deletions are hard. However, we can treat the sea cells as a graph, and we are only allowed to delete a cell if it is not on all top-bottom paths. Another way: The condition essentially is: After removal, the top and bottom rows must still be connected via sea. That is equivalent to saying that the cell is not a "bridge" or "cut" that separates top from bottom? Actually it's a node deletion, so we ask whether removal disconnects top from bottom. For a node to be safe to delete, it must not be an articulation point whose removal disconnects top from bottom. But note that top and bottom are sets of nodes (multiple top row cells, multiple bottom row cells). In node connectivity terms, we need that top and bottom are in the same connected component after removal. So safe deletion if the node is not a separator between top and bottom.
We could maintain a graph of sea cells and compute articulation points relative to top and bottom, but dynamic deletions would be tough.
Given r,c <= 3000 and n <= 300k, we might consider per operation BFS/DFS to check connectivity, but that would be O(n * rc) which is too large.
We need something like union-find but with deletions; we can use a technique where we pre-mark all cells that will ever become land (the successful ones) as blocked and then check connectivity? That still requires determining which ones are successful.
Maybe we can use the idea that the process ensures that after all operations, there is still a top-bottom path. In fact, after each successful deletion, the sea still has a path. So final sea set must have at least one path. For any attempted cell, if it lies on all possible paths, it cannot be deleted. So the successful deletions are those that are not necessary to maintain connectivity. This is reminiscent of computing the set of cells that belong to every top-bottom path, i.e., the "minimum cut" or "bottleneck" cells. In grid with top-bottom connectivity, the cells that are in every possible path form a kind of "minimum vertex cut" between top and bottom. However, the process might delete cells that are not in all paths, but after many deletions, the set of paths shrinks, and some cells may become critical later. So the condition depends on the current state.
We need to simulate incremental deletion with condition that we never delete a critical node. This is similar to "decremental connectivity with forbidden deletions if they disconnect"? But we can simulate by maintaining the set of cells that are "safe" to delete using a two-pass BFS from top and bottom? Perhaps we can maintain two distances: from top to each cell, and from bottom to each cell, but as deletions happen, distances increase; but we need to update efficiently. Possibly using union-find to maintain connected components of sea, but we need to check if removal of a node disconnects top from bottom. That's like checking if the node is a bridge in the current sea graph when considering the top and bottom as terminals. For planar grid-like graphs, there might be simpler methods.
Another idea: Since the grid is cylindrical and top and bottom rows are the boundaries, the condition is essentially that there must be a path from top to bottom through sea. This is like percolation. The process tries to block cells, but avoids blocking all percolation paths. So eventually, the remaining sea cells will form a set that includes a "spanning tree" connecting top to bottom. In fact, the set of successful deletions is exactly those cells that can be removed without breaking connectivity. This is similar to building a "maximum set of cells you can block while keeping top-bottom connectivity", which is akin to finding a "minimum cut" and removing all others. But we need to follow a given order; if a cell would become critical at the time of attempt, we skip it. So the result depends on order.
We need an efficient data structure to support queries: given a cell (r,c) that is currently sea, if we remove it (make land), will top and bottom remain connected? Equivalent to: In the current graph of sea cells, does there exist an alternative path that bypasses this cell? i.e., is the cell a cut-vertex separating top from bottom? For general graphs, we can maintain biconnected components, but dynamic deletions make it hard. However, note that deletions only, and we only delete if not a cut-vertex. That means we are deleting only non-articulation points (with respect to top-bottom connectivity). There is known data structure for decremental connectivity in planar graphs? This grid is planar? Actually cylindrical adjacency means it's planar? With wrap-around, it's a cylinder, which is planar (can be embedded on a cylinder; but if we cut it, it's a planar grid with left-right wrap). It's essentially a graph on a cylinder. Might have properties: The top-bottom connectivity can be considered as existence of a path that goes from top to bottom. If we consider dual graph? There's classic problem: "Ice skating" or "flood it" etc. Not sure.
Maybe we can use a technique of union-find with "blocked" cells, but process in reverse: Suppose we start with final state after all operations, but we don't know which are land. However, we could determine which deletions are successful by analyzing the order. Consider that if a cell is ever attempted and it's not necessary for connectivity at that moment, we delete it; else we keep it. So the final sea set is the set of cells that are never deleted. That's basically a set that includes all cells that were attempted but rejected, plus cells never attempted. So final sea set is the initial all-sea minus the successful attempts. So we could attempt to compute final sea set by iterating attempts in order and deciding which to delete. That's the forward simulation we need.
Maybe we can use a union-find like algorithm that maintains for each cell whether it is currently land or sea, and also maintain the connectivity from top to bottom. When we delete a sea cell, we need to check if removal disconnects top from bottom. That is like checking if all paths go through that cell. Equivalent to: After temporarily removing the cell, is there still a path from top to bottom? That's a connectivity test. Could we quickly test that by using something like: If there is any neighbor that can reach top and any neighbor that can reach bottom, but they are only connected via this cell? Not exactly.
Alternative: Use "max flow" or "min cut" view: Top-bottom connectivity in a grid can be decided by checking whether there is a left-right barrier of land cells that separates top from bottom. Since adjacency includes wrap, the concept of a "blocking curve" might be simpler: A set of land cells that form a closed loop around the cylinder? For a cylinder, a path from top to bottom is blocked if there is a cycle of land cells that goes around the cylinder and separates top from bottom. More formally, on a grid, the existence of a top-bottom path is equivalent to the non-existence of a "blocking set" of land cells that form a cut from left to right (wrapping) that separates top from bottom? Actually think of dual graph: On a planar grid, a path from top to bottom through empty cells exists iff there is no "cut" of blocked cells from left to right (if we consider the dual graph on blocked cells). But on a cylinder, maybe it's about cycles.
Classic problem: "Minesweeper" or "Hex" but not exactly.
Maybe we can treat the problem as maintaining a set of sea cells, and we need to know if top and bottom are connected. This is dynamic connectivity with deletions only, but we only delete when connectivity remains. There exist data structures for decremental connectivity, but they are complex and may not be necessary given the grid structure.
Observe that r and c are up to 3000, so total cells up to 9e6. n is only up to 300k, so at most 300k cells become land (successful or not? Actually only successful ones become land, at most 300k). So the number of land cells in the final state is at most 300k (since all attempted cells that succeed become land). Others remain sea. So the sea set remains large (most cells sea). So the graph of sea cells is huge, but only a few deletions. So maybe we can afford to do BFS/DFS for each operation if we only explore the sea region, but that could be O(n * (rc)) in worst case (if early operations cause many BFS on large grid). But we can optimize using the fact that sea cells dominate, but we need to check connectivity after deletion. However, if we delete a sea cell, we need to see if top and bottom become disconnected. That could be done by checking if there still exists a path. But we could maybe check quickly if the cell is a "bridge" by checking local connectivity: If after removal, the top and bottom are still connected via some other path. But to be sure, we might need to do BFS.
Given that n is only 300k, and rc up to 9e6, a BFS of the whole grid is 9e6 nodes, which is maybe okay if done a few times, but 300k times would be too much (2.7e12). So we need better.
Alternative: Since land cells are few, we can consider the complement: treat land as obstacles. The sea is the free space. We need to check if top and bottom are connected through free space. This is like a percolation problem. Adding obstacles (land) incrementally. The condition is we only add an obstacle if it does not disconnect top from bottom. So we are incrementally adding obstacles while maintaining connectivity. This is similar to building a maze: we have free cells, and we block some cells, but only if they are not critical. This is essentially computing a spanning forest? There is a known approach: Use union-find to maintain connectivity of sea cells. Initially, all sea cells are present, so we can union adjacent sea cells. Then, when we want to add a land cell, we would remove that node from the graph, which in union-find is hard. But we can think of it as: we start with all cells as sea. We will gradually remove nodes. Instead of removing, we can think of the final set of sea cells (after all successful deletions) and then add back nodes in reverse order. But we don't know which deletions are successful forward.
However, note that the decision to delete a node depends on whether it would disconnect top and bottom. That decision is equivalent to checking if the node is a cut-vertex in the current sea graph relative to top and bottom sets. This is reminiscent of the algorithm for "dynamic connectivity in incremental deletions using offline reverse" but only if we know which deletions actually happen. But we don't know, because the decision depends on the state at that time. But maybe we can treat the process as: we only delete nodes that are not articulation points. In a graph where we delete nodes only if they are not articulation points, the order does not affect the final set of articulation points? Actually, it might. But maybe we can characterize the set of nodes that will eventually become land as exactly those that are not in any "minimum cut" or something? Not sure.
Perhaps we can use a technique similar to "flood fill" from top and bottom, and maintain two labels: reachable from top, reachable from bottom. When we block a cell, we need to see if it separates the two reachable sets. More precisely, if the cell is currently sea, and it is the only connection between the top-reachable region and bottom-reachable region, then blocking it disconnects them. Otherwise, it's safe.
So we could maintain for each sea cell whether it is on all paths. That is similar to the concept of "min cut" or "bottleneck". In a flow network, the set of edges (or vertices) that are part of every path correspond to the minimum vertex cut. But here vertices.
Idea: Because the grid is large but deletions few, we might use BFS but only on a limited region near the blocked cells. Perhaps after many deletions, the sea region might become narrow; but worst-case, we might block many cells scattered around, but still sea region is huge. However, the condition that we only block if not critical might mean that we rarely block cells that are in the "bridge" region. Actually, the bridge region could be large. For example, if the grid is a straight corridor of width 1 (c=1), then every cell is critical because removing any would disconnect top and bottom. So all attempted deletions would be rejected. Then we never block any cell. So we would have to process 300k operations, each checking connectivity. In that scenario, we could precompute that all deletions will be rejected, so we can answer 0. But in general, the grid may have many alternative paths, so many deletions may be accepted. Still worst-case might be that many are accepted, but then we need to update data structures.
Maybe we can use a union-find that supports "deleting" a node by splitting? That is difficult.
Another perspective: Instead of maintaining sea connectivity directly, we can maintain the set of land cells and check if they form a barrier separating top from bottom. Since land cells are few, it might be easier to detect if adding a land cell creates a barrier. The condition "there exists a sea path from top to bottom" is equivalent to "there is no land barrier that separates top from bottom". A land barrier would be a connected set of land cells that forms a closed loop around the cylinder (or connects left to right with wrap) such that it blocks all paths? Actually on a grid, the complement of sea cells (land) can form obstacles. A top-bottom path exists if there is no cut consisting of land cells that goes from left to right (including wrap) and separates top from bottom? Think of the grid as a graph; the dual graph might help: In a planar grid, the absence of a path from top to bottom through empty cells is equivalent to existence of a path from left to right through blocked cells (or vice versa) if we consider the dual graph with appropriate boundaries. For a cylinder, the boundaries are top and bottom (open) and the sides wrap. There is known percolation duality: On a rectangular grid with open top and bottom, existence of a vertical open path is equivalent to non-existence of a horizontal closed path that connects left and right. But that's for planar with no wrap? Actually standard percolation: On a rectangular lattice, if you consider open cells, the existence of a path from top to bottom through open cells is equivalent to the non-existence of a path from left to right through closed cells (if we consider the dual lattice). That holds for planar graphs with appropriate boundaries. For a cylinder where left and right are identified, the dual might be different. But maybe we can transform: The cylinder's side can be cut to a rectangle with periodic boundary on left/right. The top and bottom are boundaries. The existence of a sea path from top to bottom is equivalent to the non-existence of a land path that goes from some column to the same column (wrapping around) and separates top from bottom? Actually think of the grid as a graph on the cylinder. A set of land cells that forms a cycle around the cylinder that separates top from bottom would block all sea paths. More precisely, if there is a connected set of land cells that touches both the left and right edges (but since edges are identified, that's not well-defined). Alternatively, consider the "annulus". There is a known result: On a cylinder, a top-bottom open path exists iff there is no closed loop of closed cells that winds around the cylinder. This loop would be a connected set of closed cells that forms a barrier.
Thus, perhaps we can maintain the land cells and check if they create a cycle that separates top from bottom. Adding a land cell could potentially create such a cycle. Since land cells are few, we could maintain a graph of land cells and check for cycles that are top-bottom separating. But we need to define separating cycles.
Let's formalize: The side area is a cylindrical grid: rows 1..r, columns 1..c with wrap at columns. So it's essentially a graph on an r x c grid on a cylinder. We want a sea path from any cell in row 1 to any cell in row r. The absence of such a path means that all possible paths are blocked by land. In terms of land cells, this is like there is a "cut" of land cells that separates the top from the bottom. On a cylinder, a cut can be a set of land cells that, when removed (i.e., considered as obstacles), separates the two boundaries. In graph theory, Menger's theorem says the minimum number of vertices needed to separate top from bottom equals the maximum number of vertex-disjoint paths. But we care about whether a set of land cells already separates.
Given that land cells are obstacles, we can think of the sea cells as free. A top-bottom sea path exists if and only if there is no "blocking set" of land cells that forms a "separating cycle". In a planar embedding of the cylinder (cut along a vertical line), the top and bottom are boundaries, and the sides are identified. A separating set would be a path of land cells from one side of the cut to the other (wrapping around) that connects the two identified sides? Might be messy.
Another angle: Use union-find on "super nodes" representing top and bottom. We start with all sea cells. We want to support deletion of a sea cell, checking if deletion disconnects top from bottom. In union-find, we can only add edges, not delete. So if we reverse time, we would add cells back (convert land to sea). That is easier: we can start from final state (all attempted cells that become land are land, others sea) but we don't know which ones become land. However, maybe we can determine which ones become land by analyzing the order offline using a concept of "criticality". This resembles the process of building a spanning tree incrementally but in reverse: In forward, we delete nodes only if they are not articulation points. That's like we are maintaining a 2-connected component? There's a known algorithm for "offline dynamic connectivity for deletions with answers" using divide-and-conquer and union-find (often called "offline dynamic connectivity" by adding and removing edges). But here we remove vertices, not edges. Could transform vertices into edges? Not directly.
Maybe we can treat each cell as a node and edges to neighbors. The condition is connectivity between two sets (top row and bottom row). We need to process vertex deletions, but we can simulate vertex deletion by deleting all incident edges. In offline dynamic connectivity for edge deletions, we can use divide-and-conquer on time and union-find (segment tree of time intervals). However, here deletions are conditional: we only delete if it doesn't disconnect. That means we don't know the deletion times a priori. So it's not a fixed schedule.
Maybe we can approach by BFS/DFS but using heuristic: Since land cells are few, the sea region is almost fully connected. Removing a single cell rarely disconnects top from bottom unless it's in a narrow passage. So we could try to check connectivity quickly by checking if there exists an alternative path around the cell. For a given cell, we could run a local search to see if its removal creates a separation. Perhaps we can precompute the "level" of each cell in terms of distance from top and bottom? Could use concept of "articulation points" in grid graphs; but dynamic updates would require recomputation.
Maybe we can use union-find on the sea cells but maintain connectivity information in a way that supports "temporary removal". Since land cells are few, we could treat the land cells as blocked and the sea cells as a union-find that is built incrementally as we add land? Wait, we start with all sea cells. When we turn a sea cell into land, we are effectively removing it. To check if removal disconnects, we could see if there is an alternative path between its neighbors that goes around. That is similar to checking if the cell is a cut-vertex in the current sea graph. In a large graph with few removals, most cells are not cut-vertices. To test if a cell is a cut-vertex, we can use Tarjan's algorithm on the whole graph, but it's static.
We could maintain the sea graph as a dynamic graph with deletions. There are data structures for dynamic connectivity under vertex deletions, e.g., using spanning forest and tracking back edges, but for planar graphs maybe simpler.
Given constraints, maybe a simpler approach exists: Because r and c are up to 3000, we can store the grid and use BFS/DFS but limit it to the area around the cell being removed. For a cell to be a cut-vertex separating top from bottom, its removal would create two disconnected components: one containing top and one containing bottom. In the sea graph, if we remove that cell, the top and bottom become disconnected. This means that in the current graph, all paths from top to bottom go through that cell. In other words, the cell is on every top-bottom path. Such cells are exactly the ones that lie on all possible top-bottom paths. In a grid, such cells are relatively rare; they form a kind of "bottleneck". Possibly we can maintain a "minimum cut" set using max flow/min cut? But we need to update dynamically.
Another thought: Since the grid is a product of rows and columns, we can think of it as a 2D array. The condition for top-bottom connectivity can be expressed using union-find per row? Not exactly.
Maybe we can solve by converting to a problem of "blocking cells" and using a technique of "flood fill from top and bottom" and marking cells that are reachable from both? Actually, consider the following: In a graph, a node is critical (lies on all paths between two sets) if and only if it is in every possible path. That is equivalent to saying that if we remove it, the two sets become disconnected. So we need to detect if a node is such a cut-vertex for sets A (top row) and B (bottom row). In an undirected graph, a vertex v is an (A,B)-cut vertex if all paths from A to B go through v. This is equivalent to: In the graph, there is no path from A to B that avoids v. So to test quickly, we could precompute two sets: reachable from A (call R_A) and reachable from B (call R_B) in the graph without v. If v is removed, A and B are connected if there exists some neighbor u of A that can reach B? But we could use the concept of "separator". For a given v, if there is a path from A to B that does not use v, then v is not critical. So we need to check existence of such a path. This can be done by performing a BFS from A that avoids v, and seeing if we reach B. But that's O(rc) per query.
However, we can maybe accelerate by using the fact that the graph is grid and v is a single cell. The BFS that avoids v might be localized if v is not critical. If v is critical, the BFS might need to explore large area. But maybe we can limit BFS by using distances? Not sure.
Given n up to 300k, we need near O(n log n) or O(n * small constant). 300k * 3000 = 900 million, borderline but maybe not acceptable in Python. But r*c up to 9e6, BFS of that is heavy. However, maybe we can use union-find to simulate connectivity but handle deletions by "undo" operations using persistent union-find? There is offline dynamic connectivity using divide-and-conquer and union-find that can handle edge deletions if we know the time intervals when each edge is present. Here, edges exist initially, and they disappear when either endpoint becomes land. So each edge's removal time is the minimum of the times its two endpoints become land (if they become land at all). But we don't know which endpoints become land because deletion of a vertex is conditional. So it's circular.
Let's step back: The process is deterministic: we iterate attempts; if turning a cell to land would not disconnect top and bottom, we do it; else skip. So we need to simulate this exactly. Could we simulate by maintaining a BFS tree or something? Possibly use a technique similar to "disjoint set union with rollback" to test each deletion by temporarily removing the cell and checking connectivity via union-find on the remaining graph? That would require re-adding it after test, but we need to decide to permanently remove if safe. If we could test quickly by checking if there is an alternative path around the cell using local search limited to nearby blocked cells, maybe we can design a data structure that maintains distances from top and bottom and updates only when critical cells are removed.
Another direction: Since the grid is cylindrical, we can think of the sea region as a set of cells. The condition of connectivity from top to bottom is equivalent to there being no closed loop of land cells that encircles the cylinder. So we can track land cells and detect when adding a land cell creates such a loop. This is similar to adding obstacles in a maze and checking if it blocks the passage. Since land cells are few, we could maintain a graph of land cells and check for cycles that separate top and bottom. How to detect that?
Consider the dual graph: Represent each cell as a face? Actually the primal is the grid of sea/land. The dual graph's vertices correspond to the "gaps" between cells? Another classic technique: For grid connectivity, we can use union-find on the cells, but also on the "walls". In problems like "percolation", we can maintain two union-find structures: one for the cells, and one for the complement or for the dual. The condition for percolation is that top and bottom are connected in the cell graph if and only if left and right are not connected in the dual graph (where dual vertices are between cells). For a rectangular grid with no wrap, the dual graph's vertices are the "edges" between cells. For a cylindrical grid, the dual might also be cylindrical? We need a robust method.
Let's try to derive: On a cylinder, the sides are identified, so the left and right boundaries are the same. The top and bottom are boundaries. The dual graph would have vertices corresponding to "faces" between cells, and edges crossing primal edges. The dual graph would be an annulus with boundaries corresponding to top and bottom. Actually, consider the grid as a graph on a cylinder. The dual graph (on the same surface) would also be a graph on a cylinder, with its own boundaries. A path of closed cells from left to right (wrapping) in the primal might correspond to a dual path from top to bottom. So the condition that there is no open path from top to bottom is equivalent to existence of a closed path (of blocked cells) that wraps around the cylinder. In other words, the land cells form a cycle that separates top from bottom. So we can monitor whether land cells have a cycle that goes around the cylinder. This is similar to the concept of "top-bottom percolation" on a cylinder: The open cells percolate iff there is no closed circuit around the cylinder.
Thus, when we add a land cell, we need to check if it creates a closed cycle of land cells that goes around the cylinder, i.e., a cycle that is top-bottom separating. But what exactly constitutes a separating cycle? On a cylinder, a cycle that is homotopically non-trivial (i.e., loops around the cylinder) will separate the two boundaries if it is "essential". Actually, on a cylinder, a simple closed curve that goes around the cylinder (i.e., winds around once) will separate the cylinder into two components: one containing the top, the other containing the bottom. So if land cells form such a loop (a connected set of land cells that forms a continuous barrier from one side to the other, but since sides are identified, it's a loop that goes around the cylinder), then top and bottom are separated. However, land cells are discrete and may form a "wall" that is not necessarily a simple closed curve but a set of cells that blocks all paths. In grid terms, a set of blocked cells that separates top from bottom is a "cut". Such a cut can be represented as a connected set of blocked cells that touches both the left and right boundaries? Wait, on a cylinder, left and right are identified, so a cut that separates top from bottom would be a set of blocked cells that forms a closed loop around the cylinder. This loop must be connected (in the adjacency sense) and must encircle the cylinder. In a grid, this would correspond to a set of cells that, in each column, there is at least one blocked cell? Not exactly; the barrier could be meandering.
Let's think: For a path from top to bottom to exist, you need to be able to go from row 1 to row r moving only through sea cells. If land cells block, they must form a horizontal barrier that connects from the left edge to the right edge (wrapping) such that there's no vertical gap. Because if there is a continuous wall of land cells that goes from the left side to the right side (wrapping around), then any vertical path would have to cross it, but since it's blocked, you can't get from top to bottom without crossing land, which is not allowed. But is that sufficient? On a discrete grid, a set of land cells that forms a "blocking curve" means that in each column, there might be a land cell somewhere, but they must be connected in such a way that they form a continuous barrier. More formally, a cut separating top from bottom corresponds to a set of vertices whose removal disconnects top and bottom. By Menger, the minimum size of such a cut is something. But our condition is: after adding a land cell, does such a cut now exist? Since initially there is no cut (all sea), and we add land cells incrementally, the moment a cut emerges, connectivity is lost. So we need to detect when a cut (set of land cells) that separates top from bottom first appears. That's similar to monitoring when the set of land cells percolates horizontally (forms a left-right blocking path). But careful: On a cylinder, a left-right path (with wrap) of land cells would separate top from bottom? Actually, think of a simple example: r=2, c=2. Top row (row1) and bottom row (row2). If we block cells (1,1) and (1,2) (top row entirely land), is there a sea path from top to bottom? Top row cells are land, but we need a sea path that starts at any top row cell (sea) to bottom. Since all top row cells are land, there is no starting sea cell in top row? The condition says: "The first cell in the sequence is in the top row." It must be a sea cell in top row. So if all top row cells become land, then there is no sea cell in top row, so there is no sequence (since first cell must be sea in top row). That would also disconnect. So land cells in top row can block connectivity even if they don't form a wall across. Actually, the condition requires existence of at least one sea cell in top row and one in bottom row, and a path between them. So it's not just about a wall; if top row becomes all land, then connectivity fails because no starting sea cell. But can that happen without a wall? Yes. So we need to consider both top and bottom rows having at least one sea cell and being connected.
In the process, we turn sea into land; we must maintain that there is at least one sea cell in top row and bottom row, and they are connected. So if we try to turn a top row cell into land, it might remove the last sea cell in top row, which would break connectivity. So that's another way.
Thus, we need to maintain connectivity between the set of sea cells in top row and bottom row.
Maybe we can use a union-find where we initially union all sea cells. Then, as we delete cells (turn to land), we need to remove nodes from the union-find, which is difficult. But we can use the concept of "cutting" nodes and see if the graph remains connected. Since we only delete nodes if they are not articulation points relative to top-bottom, maybe we can maintain articulation points using a data structure like "link-cut tree" or "dynamic connectivity for forests"? However, the graph is not a tree; it's a grid with cycles.
Given constraints, maybe we can adopt a simpler approach: Because r,c <= 3000, but n <= 300k, we could store the grid and for each query, perform a BFS/DFS but only on the sea cells. However, BFS over 9 million cells 300k times is impossible. But maybe we can early stop if we find a path. Since the grid is mostly sea, a BFS from top row to bottom row might quickly find a path (maybe O(rc) but actually in a large open area, BFS will explore many cells before hitting bottom? In worst case, to determine that top and bottom are disconnected, BFS would need to explore the entire reachable component from top, which could be O(rc) ~ 9e6. 300k * 9e6 is astronomical.
We need smarter.
Maybe we can use union-find but building it incrementally from the end. Let's try to think offline: Suppose we process operations backwards. At the end of all operations, we have some set of land cells (the successful ones). But we don't know which ones are successful. However, we can determine success by considering the order: A cell becomes land if at the moment of its attempt, it is not critical. That decision depends on previous successful deletions. So we could simulate forward but use a data structure that can test criticality quickly using some precomputed values like "level" in a BFS tree. For a grid, perhaps we can assign each sea cell a "height" or distance from top and bottom, and then a cell is critical if it lies on all top-bottom paths, which would mean that it is a "bottleneck" in the sense that its removal would disconnect. In a grid, such bottlenecks often correspond to "minimum cut" which can be found via a concept of "left-right" distances.
Consider the idea: For each cell, we can compute the set of cells that are reachable from top without passing through that cell. But that's per cell.
Perhaps we can use a maximum flow algorithm: The top-bottom connectivity in a grid can be reduced to a flow problem where each vertex has capacity 1. The minimum vertex cut size is the maximum number of vertex-disjoint paths. But we need to check if a particular vertex is part of every minimum cut? Actually, if the minimum cut size is > 1, then no single vertex is critical. If minimum cut size = 1, then the only critical vertices are those that form a minimum cut of size 1. But after deletions, the minimum cut size can change. So we need to keep track of the min cut size. If min cut size >= 2, then any single deletion is safe (since removing one vertex cannot disconnect if you need at least 2 to separate). But is that true? In vertex connectivity, if the vertex connectivity between top and bottom is k, then removal of any set of less than k vertices does not disconnect. So if the current vertex connectivity between the set of top row sea cells and bottom row sea cells is at least 2, then removing any single sea cell will not disconnect them. However, if the connectivity is 1, then there exist some vertices whose removal disconnects (the articulation points relative to the two sets). But not all vertices: only those that lie on all paths (i.e., form a cut of size 1). So we could maintain the vertex connectivity between top and bottom. Initially, with all sea cells, what is the vertex connectivity? In a grid with r rows and c columns, the vertex connectivity between top and bottom is c (since there are c disjoint vertical paths? Actually, due to wrap-around, there might be many disjoint paths. But if c is large, connectivity is high. As we delete cells, connectivity decreases. If we can maintain the current vertex connectivity, we can quickly decide: If connectivity >= 2, any deletion is safe (since removing one vertex cannot disconnect). Only when connectivity becomes 1, we need to be careful: only deletions of cut-vertices (which are the ones on the unique narrow path) will disconnect. But after deletions, connectivity may become 0 (disconnected) but we never allow that to happen.
Thus, maybe we can maintain the minimum number of land cells needed to separate top from bottom, i.e., the size of a minimum vertex cut. In a grid, this is related to the number of disjoint paths. There is a known result: In a planar graph, the minimum vertex cut between two sets on the boundary equals the maximum number of vertex-disjoint paths (Menger). For a grid, the min cut can be found via a shortest path in the dual? Actually, vertex cuts in grid can be transformed to edge cuts by splitting vertices. Might be heavy.
But maybe we can maintain the connectivity by using a spanning structure like a "grid graph" and track articulation points via a DFS tree. Since deletions only, we could use a data structure for dynamic connectivity in planar graphs that supports vertex deletions? Possibly use "SPQR tree" but too complex.
Given constraints, perhaps we can simulate using BFS but with optimization: Since land cells are few, the sea graph is mostly connected. The only way a cell is critical is if it lies on a narrow passage. That narrow passage likely consists of a set of cells that form a "bridge" in the grid after removing other land cells. As we delete cells, we might create narrow passages. Maybe we can maintain a set of "critical cells" that are currently articulation points relative to top-bottom. When we attempt to delete a cell, if it is not in that set, we can delete it safely. If it is, we need to check if its removal would disconnect (maybe it's still critical). But we can update the set after each deletion. The number of critical cells might be small (like O(r+c) perhaps) because they form "bottlenecks". For a grid, articulation points between top and bottom often correspond to "minimum cut" which is like a "curve" across the cylinder; its size is at most c (if it's a vertical cut) but could be more if it's meandering. But the set of articulation points (cut-vertices) could be large: consider a grid where we have removed many cells to create a maze with a single winding path; then every cell on that path is a cut-vertex for top-bottom connectivity. That path could have length up to O(rc). So worst-case, the number of critical cells could be large (e.g., if we have carved a single path from top to bottom, all cells on that path are articulation points). Then many deletions would be rejected if they are on that path. But in that scenario, the number of land cells (successful deletions) would be the complement of that path, which could be large. However, the process order might cause many cells to become land, leaving a narrow path. So we could end up with many critical cells.
We need a method that works even if many cells become critical.
Maybe we can use a union-find with "time" offline technique: we know all attempted cells. Could we determine which attempts succeed by analyzing the final connectivity? The process is monotonic: once a cell becomes land, it stays land. The set of sea cells shrinks (or stays same) over time. The decision to accept a deletion is based on whether the cell is "non-essential" at that moment. This is similar to the process of building a "minimum" set of cells that must remain sea to keep connectivity. In other words, we start with all sea; we try to delete cells in a given order; we delete if possible. This is exactly the process of computing the "maximal" set of cells we can delete while maintaining connectivity, given a deletion order. This is reminiscent of computing the "lexicographically smallest" or "greedy" deletion that maintains connectivity. There's a known concept: Given a graph and an order on vertices, you can compute the set of vertices that are not in the "minimum Steiner tree" or something.
Alternatively, think of it as: We want to keep a spanning subgraph (actually any subgraph) that connects top and bottom. We delete vertices in a given order if they are not articulation points relative to top-bottom in the current graph. This is exactly the process of "core decomposition" or "minimum connected spanning subgraph"? Not exactly.
Observation: The process ensures that after all operations, the remaining sea cells contain at least one top-bottom path. Moreover, the set of sea cells is maximal with respect to the given order: we could not delete any further attempted cell because its deletion would break connectivity (or it was never attempted). This is similar to the concept of "last surviving connected set" under a deletion order where you only delete if not separating. This is analogous to computing the "connected component containing the terminals" after removing vertices in order, but with the rule that you only remove if removal doesn't disconnect terminals. That's exactly the process of "contracting" the graph by removing non-essential vertices in a given order. Actually, if you think of the graph with two terminals (a super source connected to all top row sea cells, and super sink to bottom row sea cells), then a vertex can be removed if it is not in every source-sink path. This is known as the "2-terminal reliability" or "essential vertices".
One way to compute which vertices are essential is to compute the set of vertices that lie on some source-sink min-cut? Not exactly.
Maybe we can model this as a flow network and compute for each vertex whether it is part of some minimum cut? But min cut size can be more than 1.
But the order matters: A vertex might be non-essential initially (if there are many paths), but after other deletions, it might become essential. So at the time of its attempted deletion, if it's essential, we skip. So we need to simulate the order.
Given that n <= 300k, we could potentially afford O(n * something like (r+c) or O(n log n) with some data structure that updates connectivity after deletions.
Consider using a union-find structure that represents the sea cells, but we cannot delete. However, we can simulate deletions by "adding" cells in reverse. In reverse, we would start from the final state (which we don't know) and add cells (convert land to sea) while checking connectivity? But in reverse, the condition would be something like: adding a cell (making it sea) could potentially restore connectivity? Not directly.
Maybe we can determine the final set of sea cells via a "pruning" process: Starting from all cells, we process attempts in order; for each cell, if it is currently sea and its removal would not disconnect top-bottom, we remove it. This is like a "decremental connectivity with forbidden deletions of cut vertices". There is known approach: Maintain a "blocked" array, and also maintain two BFS/DFS trees from top and bottom, and update them lazily. Could we maintain for each cell a "level" or "distance" from top and bottom and use that to test if it's a cut? For a cell to be a cut, all paths from top to bottom must pass through it. In flow terms, that means that if we remove it, there is no path. Equivalently, in the current graph, there is no path from top to bottom that avoids that cell. That can be tested by checking if there exists a neighbor of the cell that can reach top, and a neighbor that can reach bottom, and those two sets are disconnected if we remove the cell. But we can precompute reachability from top and bottom in the current graph. If we maintain for each cell whether it is reachable from top and from bottom, and also maintain connectivity of the graph, we might detect cut quickly.
Suppose we maintain two boolean arrays: reachable_from_top and reachable_from_bottom, computed via BFS from top and bottom on the current sea cells. Then a cell is critical if:
- It is reachable from top and reachable from bottom? Actually, if a cell is not reachable from top or not from bottom, then removing it cannot affect connectivity because any top-bottom path wouldn't go through it (since it's not on any top-bottom path). But wait: Could a cell that is not reachable from top but is adjacent to a reachable cell be critical? If it's not on any top-bottom path, its removal cannot disconnect top-bottom. So only cells that are on some top-bottom path could be critical. So cells that are in the intersection of top-reachable and bottom-reachable (i.e., in the "flow" region) are potential cut vertices. Among those, a cell is a cut if after its removal, there is no path from top to bottom. That is equivalent to: In the graph, all paths from top to bottom go through that cell. In terms of reachability, if we remove the cell, the top-reachable set (excluding the cell) and bottom-reachable set (excluding) become disconnected. This is similar to the concept of a "separator".
We could test a candidate cell by checking if all its neighbors that are reachable from top (excluding itself) are not also connected to bottom via alternative routes. More concretely, we could perform a BFS from the top that avoids the cell, and see if we can reach bottom. That's O(area) per query.
But maybe we can maintain a "flow" or "distance labels" that allow quick query: For a cell v, if there exist two edge-disjoint paths from top to bottom that avoid v, then removal is safe. If v lies on all paths, then it's critical. In planar graphs, there is a relationship between distances in dual. Could we compute for each cell its "left-right" distance? For a grid with top and bottom boundaries, a cell is critical if it lies on every top-bottom path. In planar graphs, the set of such vertices is the "minimum cut" vertices. For two-terminal graphs, the set of vertices that lie on all paths are exactly the vertices that are in the intersection of all "minimal" paths? Not easy.
Given the constraints, perhaps we can use a union-find that supports "undo" operations to test deletions: For each query, we could temporarily mark the cell as land, then check connectivity via BFS/DFS but restricted to a region around the cell? Not reliable.
Another idea: Since n is 300k and grid is 9e6, perhaps we can use a BFS that uses bitsets to represent rows, and propagate connectivity quickly? For example, we could maintain for each row a bitset of columns that are sea and reachable from top. Then when we delete a cell, we need to update reachable sets. This is like dynamic connectivity in a grid with deletions, we could use a segment tree of rows with bitset unions? Might be complex.
Maybe we can reduce the problem to a simpler one using the concept of "union-find with portals". Because the grid is cylindrical, we can treat columns as circular.
Wait, maybe we can use a technique: For each column, we can consider the sea cells as open. The connectivity from top to bottom can be thought of as percolation. There is known result: In a 2D grid, percolation can be detected by maintaining a union-find as you add obstacles, but here we remove obstacles. However, we can reverse: instead of removing sea cells (making land), we can think of adding land cells as adding obstacles. Starting from all sea, we add obstacles one by one, but only if adding the obstacle does not block all top-bottom paths. That is, we add an obstacle only if it does not create a full barrier. This is similar to the classic problem of "adding walls in a maze without sealing off the exit". There is a known approach using "disjoint set union" with a virtual top and bottom node, and when we add an obstacle, we remove connections. But again deletion.
But maybe we can treat land cells as blocked, and we want to maintain connectivity between top and bottom. Adding a blocked cell is like removing a node from the graph. However, we can simulate this by considering the dual graph: when we block a cell, we may connect components in the dual, potentially creating a cycle that separates top and bottom. Could we maintain a DSU on the dual to detect when a barrier is formed? For instance, in grid percolation problems, to check if adding a blocked cell creates a left-right barrier, we can check the connectivity of the blocked cells in the dual graph (or the "wall" graph). For a rectangular grid, adding a blocked cell can connect to other blocked cells to form a continuous wall from left to right, which would block top-bottom connectivity. But on a cylinder, we need to detect a cycle that goes around the cylinder. That might be doable.
Let's explore this dual approach more concretely. Consider the grid of cells. The top and bottom are boundaries. We want to know if there exists a path of sea cells from top to bottom. Equivalent to: There is no path of land cells that connects the left side to the right side? For a cylinder, left and right are identified, so a land path that goes from a cell on the left boundary to a cell on the right boundary (with wrap) would actually be a loop. But careful: In a cylinder, the left and right edges are the same line. So a land path that touches both sides (i.e., includes a cell in column 1 and a cell in column c) and is connected (via adjacency that wraps) could form a barrier. However, a land path that connects a cell in column 1 to a cell in column c (through wrap) and also is connected such that it separates top from bottom? Actually, consider a simple barrier: a complete row of land cells (all columns) would certainly block any sea path from top to bottom because it creates a horizontal wall. This row is connected (since adjacent horizontally, and wrap around) and touches both sides? It touches column 1 and column c, but because of wrap, it's actually a cycle around the cylinder. So it's a closed loop. In general, a set of land cells that forms a connected component that touches both the top and bottom? No, to block top-bottom sea paths, the land cells must form a "cut" that separates the top from the bottom in the graph sense. In the planar dual of the grid embedded on a cylinder, a cut of vertices in the primal corresponds to a cycle in the dual. The dual vertices correspond to the "edges" between cells? Perhaps easier: We can construct a graph where each cell is a node, but also we consider the "boundaries" as nodes. However, there is known technique: For percolation in a rectangular grid, we can use two DSUs: one for the cells (open) and one for the "walls" (closed) to detect when opening a cell connects top to bottom or when closing a cell connects left to right. Specifically, for a rectangular grid with four sides, if we are adding open cells (making them passable), we can check if they connect top to bottom by union-find. For adding blocked cells, we could check if they connect left to right in the dual. But here we are adding blocked cells (land) to an initially all open (sea) grid. So we could maintain a DSU for the land cells and detect when they connect from one side to the other (wrapping) to form a barrier. However, we need to ensure that the barrier indeed separates top from bottom. On a cylinder, what constitutes a barrier? A set of land cells that forms a connected component that touches both the "left" and "right" boundaries? But left and right are identified, so touching both is automatic if the component is large enough? Not exactly.
Alternative viewpoint: Consider the grid's cells as squares. The sea region is the open area. The land region is obstacles. For there to be no sea path from top to bottom, the land obstacles must contain a cycle that goes around the cylinder, i.e., a closed curve that separates top from bottom. This cycle can be thought of as a connected set of land cells that, when projected, forms a loop. In the dual graph (where vertices correspond to the corners of cells, say), a cycle of land cells corresponds to a path in the dual that connects the top boundary to the bottom boundary? I'm mixing.
Better: Use the concept of "cell graph" and its "dual graph" where each cell corresponds to a vertex in the primal, and each edge between cells corresponds to a vertex in the dual. For planar graphs, a set of vertices that separates two faces corresponds to a cut. Here, the top and bottom are faces? Actually, the cylinder has two boundaries: top and bottom. If we cap the top and bottom with caps (like the top and bottom areas are cities, not part of the side), but in our problem, the top and bottom rows are part of the side? Wait: The picture: The planet is a cylinder. The top and bottom areas consist of big cities. The side area consists of sea. We are only dealing with the side area (grid). The trade route requires a sea path from a cell in the top row to a cell in the bottom row, through side area cells. So top and bottom rows are part of the side area, but they are also adjacent to the cities? Actually, the condition says: "All cells in the sequence are occupied by the sea. The first cell in the sequence is in the top row. The last cell in the sequence is in the bottom row." So the path must start and end in the top/bottom rows of the side area. So those rows are part of the grid. So the top row cells are sea initially. So the boundaries are the top and bottom rows themselves, not beyond. So there is no "outside" face. The path must go from top row to bottom row within the grid.
Now, consider the dual graph: The primal graph vertices are the cells (sea/land). The edges are between adjacent cells (including wrap). We want to know if there is a path from any top row cell to any bottom row cell. This is a standard connectivity problem in the primal.
The dual approach: If we consider the graph whose vertices are the "edges" between cells? Not straightforward.
Maybe we can use a "union-find with height" trick: Because the grid is cylindrical, we can treat columns modulo c. Then connectivity from top to bottom can be thought of as existence of a path that goes from row 1 to row r. As we add land cells, we block some cells. The moment the top and bottom become disconnected, there must be a "cut" of land cells that separates them. That cut would be a set of land cells that, if we consider them as walls, form a continuous barrier from the left to the right (wrapping). Because on a cylinder, a closed curve that separates the two boundaries must intersect the grid in a cycle that goes around. So we could maintain connectivity among land cells to detect when they form a left-right wrapping barrier. But is that equivalent? Let's test with examples.
Consider a 2x3 grid (r=2, c=3). Top row row1, bottom row row2. Initially all sea, top-bottom connected (via vertical moves).