Omkar and Determination
Contest: Technocup 2022 - Elimination Round 1
Difficulty rating: 1700
Tags: data structures, dp

The problem statement looms below, filling you with determination.

Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up.

Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't.

You are given a grid $$$a$$$ of dimensions $$$n \times m$$$ , i. e. a grid with $$$n$$$ rows and $$$m$$$ columns. You need to answer $$$q$$$ queries ($$$1 \leq q \leq 2 \cdot 10^5$$$). Each query gives two integers $$$x_1, x_2$$$ ($$$1 \leq x_1 \leq x_2 \leq m$$$) and asks whether the subgrid of $$$a$$$ consisting of the columns $$$x_1, x_1 + 1, \ldots, x_2 - 1, x_2$$$ is determinable.

Input format:
The first line contains two integers $$$n, m$$$ ($$$1 \leq n, m \leq 10^6$$$, $$$nm \leq 10^6$$$)  — the dimensions of the grid $$$a$$$.

$$$n$$$ lines follow. The $$$y$$$-th line contains $$$m$$$ characters, the $$$x$$$-th of which is 'X' if the cell on the intersection of the the $$$y$$$-th row and $$$x$$$-th column is filled and "." if it is empty.

The next line contains a single integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$)  — the number of queries.

$$$q$$$ lines follow. Each line contains two integers $$$x_1$$$ and $$$x_2$$$ ($$$1 \leq x_1 \leq x_2 \leq m$$$), representing a query asking whether the subgrid of $$$a$$$ containing the columns $$$x_1, x_1 + 1, \ldots, x_2 - 1, x_2$$$ is determinable.

Output format:
For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise.

For the first query:

For the second query:

Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid.

For the third query:

This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well:

For the fourth query:

For the fifth query:

This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well:

Example input:
4 5
..XXX
...X.
...X.
...X.
5
1 3
3 3
4 5
5 5
1 5

Example output:
YES
YES
NO
YES
NO

Editorial:
First notice that in a determinable grid, for any cell, it can't be that both the cell above it and the cell to its left are filled. If that were the case, then the cell wouldn't be exitable regardless of whether it was filled or not, and so we couldn't determine whether it was filled.
Now notice that in any grid with the above property, namely that from each cell you can move either up or to the left into an empty cell (or both), every empty cell must be exitable  — just keep moving either up or to the left, whichever is possible, until you exit the grid.
It follows that for any grid satisfying that property, given only which cells are exitable, starting from the outermost cells you will be able to determine that the nonexitable cells are filled, which implies that the next cells satisfy the property, which further implies that the nonexitable ones there are filled, and so on. This allows you to determine the entire grid (since the exitable cells are obviously empty).
Therefore, a grid being determinable is equivalent to all of its cells having an empty cell immediately above and/or to the left of it. You can check this for arbitrary subgrids by precomputing two dimensional prefix sums of the cells that violate this property, then checking whether the sum for a given subgrid is $$$0$$$. This solution is $$$O(nm + q)$$$.
The actual problem only asked for subgrids that contained every row, which allows for a somewhat simpler implementation.