#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 n×mn \times m grid made of square cells. The rows of the grid are numbered from 00 to n1n-1, and the columns from 00 to m1m-1. The cell in row ii and column jj (0in10 \le i \le n-1, 0jm10 \le j \le m-1) is denoted as cell (i,j)(i,j). Each cell (i,j)(i,j) has a specific elevation, denoted as ai,ja_{i,j}.

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 00, row n1n-1, column 00, and column m1m-1). To do this, the architect should choose four integers r1r_1, r2r_2, c1c_1, and c2c_2 (1r1r2n21 \le r_1 \le r_2 \le n-2 and 1c1c2m21 \le c_1 \le c_2 \le m-2), corresponding to the rectangular region that includes all cells (i,j)(i,j) satisfying r1ir2r_1 \le i \le r_2 and c1jc2c_1 \le j \le c_2.

In addition, a region is considered valid if and only if, for every cell (i,j)(i,j) inside the region, the following condition holds: among the two cells adjacent to the region in row ii (cells (i,c11)(i,c_1-1) and (i,c2+1)(i,c_2+1)), and the two cells adjacent to the region in column jj (cells (r11,j)(r_1-1,j) and (r2+1,j)(r_2+1,j)), the elevation of cell (i,j)(i,j) 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 r1r_1, r2r_2, c1c_1, and c2c_2 whose corresponding region is valid).

Input Format

The first line contains two integers nn and mm, representing the grid's height and width.

The next nn lines each contain mm integers, which are ai1,0m1a_{i-1,0\dots m-1}.

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 66 valid regions in total:

  • r1=r2=1,c1=c2=1r_1=r_2=1, c_1=c_2=1
  • r1=1,r2=2,c1=c2=1r_1=1, r_2=2, c_1=c_2=1
  • r1=r2=1,c1=c2=3r_1=r_2=1, c_1=c_2=3
  • r1=r2=4,c1=2,c2=3r_1=r_2=4, c_1=2,c_2=3
  • r1=r2=4,c1=c2=3r_1=r_2=4, c_1=c_2=3
  • r1=3,r2=4,c1=c2=3r_1=3,r_2=4,c_1=c_2=3

For example, r1=1,r2=2,c1=c2=1r_1=1, r_2=2, c_1=c_2=1 corresponds to a valid region because both of the following conditions hold:

  • a1,1=4a_{1,1}=4 is strictly less than a0,1=8a_{0,1}=8, a3,1=14a_{3,1}=14, a1,0=7a_{1,0}=7, and a1,2=10a_{1,2}=10.
  • a2,1=7a_{2,1}=7 is strictly less than a0,1=8a_{0,1}=8, a3,1=14a_{3,1}=14, a2,0=9a_{2,0}=9, and a2,2=20a_{2,2}=20.

Constraints

For all testdata:

  • 1n,m25001 \le n, m \le 2500.
  • $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
11 n,m30n, m \le 30 88
22 n,m80n, m \le 80 77
33 n,m200n, m \le 200 1212
44 n,m700n, m \le 700 2222
55 n3n \le 3 1010
66 0ai,j10 \le a_{i,j} \le 1 1313
77 No additional constraints 2828

Translated by ChatGPT 5