#P7783. 「MCOI-Zero / AC6-M14」Gracemeria Patrol

「MCOI-Zero / AC6-M14」Gracemeria Patrol

Description

You are given an n×mn \times m 01 matrix. In each operation, you can flip the value at one position and the values directly above, left, and right of it. If the original value is 00, it becomes 11 after flipping; otherwise it becomes 00.

For example, the matrix below becomes the following after flipping the position marked by brackets:

$$\begin{bmatrix}0&1&0&1&1\\1&0&[0]&1&0\\0&0&0&0&1\end{bmatrix}\rightarrow \begin{bmatrix}0&1&1&1&1\\1&1&1&0&0\\0&0&0&0&1\end{bmatrix}$$

In particular, if the operation position is on the boundary, only the part that does not go out of bounds will be flipped.

Now you are given qq queries. Each query provides an interval [l,r][l, r] and a constant kk. For the 01 submatrix consisting of rows with indices in [l,r][l, r], consider all ways to make it all 00 by choosing some positions to perform exactly one operation on each chosen position. In those ways, output how many times row kk is operated on.

In particular, if there is no way, or there are multiple ways to choose operation positions, output 1-1.

Queries are independent of each other.

Input Format

The first line contains three integers n,m,qn, m, q.

The next nn lines each contain mm characters, describing the 01 matrix.

The next qq lines each contain three integers l,r,kl, r, k, describing a query.

Output Format

Output qq lines. Each line contains one integer, the answer to the corresponding query.

10 4 2
1010
0110
0101
0000
1011
0111
1110
1011
0001
1100
1 10 6
2 5 3
2
2

Hint

  • Subtask 1 (10 pts): n,m3,q10n, m \leq 3, q \leq 10.
  • Subtask 2 (20 pts): n,m,q10n, m, q \leq 10.
  • Subtask 3 (20 pts): n,q50,m10n, q \leq 50, m \leq 10.
  • Subtask 4 (20 pts): n,q104,m10n, q \leq 10^4, m \leq 10.
  • Subtask 5 (30 pts): No special constraints.

For 100%100\% of the testdata, 1n5×1041 \leq n \leq 5 \times 10^4, 1m501 \leq m \leq 50, 1q5×1051 \leq q \leq 5 \times 10^5, 1lkrn1 \leq l \leq k \leq r \leq n.

idea: Sol1, solution: Sol1 & ethan_zhou, code: Sol1, data: Sol1


"Talisman..."

"An angel won't give up its wings unless
it has finished the last dance, right?"

Translated by ChatGPT 5