#P14985. [USACO26JAN1] Pluses and Minuses P

[USACO26JAN1] Pluses and Minuses P

Problem Description

Farmer John once painted a rectangular grid on the ground of his pasture. In each cell, he painted either a +\texttt{+} or a -\texttt{-} (representing +1+1 and −1−1, respectively).

Over time, the paint faded, and Farmer John now remembers the values of only some cells. However, Farmer John does remember one important fact about the original painting:

In every row and every column, the sum of the values in any contiguous subsegment was always between −1−1 and 22 (inclusive).

As an example, consider the row + - - +\texttt{+ - - +}. It does not satisfy the condition, since the subsegment + [ - - ] +\texttt{+ [ - - ] +} has sum −2-2.

However, the row - + + -\texttt{- + + -} does satisfy the condition.

[ - ] + + -    sum = -1
[ - + ] + -    sum = 0
[ - + + ] -    sum = +1
[ - + + - ]    sum = 0
- [ + ] + -    sum = +1
- [ + + ] -    sum = +2
- [ + + - ]    sum = +1
- + [ + ] -    sum = +1
- + [ + - ]    sum = 0
- + + [ - ]    sum = -1

Count the number of different grids consistent with Farmer John's memory.

Input Format

The first line contains TT (1≤T≤1001\le T\le 100), the number of independent tests. Each test is specified as follows:

The first line contains RR, CC, and XX (1≤R,C≤5⋅1051\le R,C\le 5\cdot 10^5, 0≤X≤min⁡(105,RC)0\le X\le \min(10^5,RC)), meaning that the grid has dimensions R×CR\times C and Farmer John remembers the values of XX different cells in the grid.

Then following XX lines each contain a character v∈{+,-}v\in \{\texttt{+}, \texttt{-}\} followed by two integers rr and cc (1≤r≤R,1≤c≤C1\le r\le R, 1\le c\le C), meaning that the value at the rrth row and ccth column of the grid is vv. It is guaranteed that no ordered pair (r,c)(r,c) appears more than once within a single test.

Additionally, it is guaranteed that neither the sum of RR nor the sum of CC over all tests exceeds 10610^6, and that the sum of XX over all tests does not exceed 2⋅1052\cdot 10^5.

Output Format

For each test, output the number of grids on a separate line.

2
1 3 3
+ 1 3
+ 1 1
- 1 2
1 3 3
+ 1 1
+ 1 3
+ 1 2
1
0
1
2 2 0
7

Hint

Here are the seven grids:

++
++

++
+-

++
-+

+-
++

+-
-+

-+
++

-+
+-

  • Inputs 3-4: min⁡(R,C)=1\min(R,C)=1 for all tests
  • Inputs 5-6: R,C≤10R,C\le 10 for all tests
  • Inputs 7-11: ∑max⁡(R,C)2≤106\sum \max(R,C)^2 \le 10^6
  • Inputs 12-14: ∑RC≤106\sum RC \le 10^6
  • Inputs 15-22: No additional constraints.