luogu#P7970. [KSN2021] Binary Sea
[KSN2021] Binary Sea
Problem Description
There is an grid. Rows and columns are numbered starting from , and cells are indexed from the top-left to the bottom-right.
The cell in row and column is black if and only if .
Two black cells are connected if and only if they are both black and one can reach the other by passing through some black cells, where each step moves between black cells that share a common edge.
You are given rectangles, with top-left corner and bottom-right corner . For each rectangle, you need to find how many connected components are formed by all black cells inside it.
Input Format
The first line contains three integers , representing the grid size and the number of queries.
The next lines each contain four integers , representing a query rectangle.
Output Format
For each query, output one line containing one integer, representing the answer.
6 5 4
0 0 3 2
0 2 1 3
0 1 2 4
5 4 5 4
1
1
2
0
Hint
[Sample Explanation]
The following figures illustrate the four queries in the sample:

[Constraints]
This problem uses bundled testdata.
- Subtask 1 (5 points): There is only one test case, with , , and the queried are, in order, , , and .
- Subtask 2 (11 points): .
- Subtask 3 (10 points): , .
- Subtask 4 (20 points): .
- Subtask 5 (4 points): .
- Subtask 6 (6 points): For each query, it is guaranteed that there exists an integer such that .
- Subtask 7 (29 points): .
- Subtask 8 (15 points): No special restrictions.
For all testdata, , , .
Translated by ChatGPT 5