#P5781. [IOI 2019] 矩形区域
[IOI 2019] 矩形区域
Description
In the early 19th century, a ruler ordered a palace to be built on a plateau overlooking a beautiful river view. The plateau can be seen as an grid made of square cells. The rows of the grid are numbered from to , and the columns from to . The cell in row and column (, ) is denoted as cell . Each cell has a specific elevation, denoted as .
The ruler instructed his architect to choose a rectangular region to build the palace. This region cannot include any cells on the boundary of the grid (row , row , column , and column ). To do this, the architect should choose four integers , , , and ( and ), corresponding to the rectangular region that includes all cells satisfying and .
In addition, a region is considered valid if and only if, for every cell inside the region, the following condition holds: among the two cells adjacent to the region in row (cells and ), and the two cells adjacent to the region in column (cells and ), the elevation of cell must be strictly less than the elevations of all these four cells.
Your task is to help the architect count the number of valid regions where the palace can be built (that is, the number of choices of , , , and whose corresponding region is valid).
Input Format
The first line contains two integers and , representing the grid's height and width.
The next lines each contain integers, which are .
Output Format
One line with one integer, representing the number of valid regions.
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6
6
Hint
Sample Explanation

There are valid regions in total:
For example, corresponds to a valid region because both of the following conditions hold:
- is strictly less than , , , and .
- is strictly less than , , , and .
Constraints
For all testdata:
- .
- $0 \le a_{i,j} \le 7 \times 10^6 (0 \le i \le n - 1, 0 \le j \le m - 1)$.
The detailed additional constraints and scores for subtasks are shown in the table below:
| Subtask ID | Additional Constraints | Score |
|---|---|---|
| No additional constraints |
Translated by ChatGPT 5
京公网安备 11011102002149号