atcoder#AGC015C. [AGC015C] Nuske vs Phantom Thnook
[AGC015C] Nuske vs Phantom Thnook
Score : points
Problem Statement
Nuske has a grid with rows and columns of squares. The rows are numbered through from top to bottom, and the columns are numbered through from left to right. Each square in the grid is painted in either blue or white. If is , the square at the -th row and -th column is blue; if is , the square is white. For every pair of two blue square and , there is at most one path that starts from , repeatedly proceeds to an adjacent (side by side) blue square and finally reaches , without traversing the same square more than once.
Phantom Thnook, Nuske's eternal rival, gives queries to Nuske. The -th query consists of four integers , , and and asks him the following: when the rectangular region of the grid bounded by (and including) the -th row, -th row, -th column and -th column is cut out, how many connected components consisting of blue squares there are in the region?
Process all the queries.
Constraints
- is either or .
- satisfies the condition explained in the statement.
- $1 \leq x_{i,1} \leq x_{i,2} \leq N(1 \leq i \leq Q)$
- $1 \leq y_{i,1} \leq y_{i,2} \leq M(1 \leq i \leq Q)$
Input
The input is given from Standard Input in the following format:
..
:
..
:
Output
For each query, print the number of the connected components consisting of blue squares in the region.
3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4
3
2
2
2
In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus should be printed.
In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus should be printed. Note that squares that belong to the same component in the original grid may belong to different components.
5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4
3
2
1
1
3
2