#P8164. [JOI 2022 Final] 沙堡 2 / Sandcastle 2
[JOI 2022 Final] 沙堡 2 / Sandcastle 2
Problem Description
JOI is playing on the beach. He built a sandcastle. The sandcastle built by JOI is located in a rectangular area on the beach. This rectangular area has rows and columns. The height of the cell in the -th row from top to bottom and the -th column from left to right is . Note that all values of are pairwise distinct.
On the sandcastle, JOI performs the following actions.
- First, JOI chooses a cell and starts moving from that cell.
- Then, from the current cell, he moves to an adjacent cell in one of the four directions: up, down, left, or right. He must move to a cell whose height is lower than the current cell. He may repeat this action zero or more times.
In the end, if we look from above, the cells he has visited will form a rectangle.
Given the height information of each cell, write a program to compute the number of all possible rectangles that can be formed by the cells visited by JOI.
Input Format
The first line contains two positive integers .
The next lines follow. The -th line contains positive integers .
Output Format
Output one line with a single integer, representing the number of all possible rectangles formed by the cells visited by JOI.
1 5
2 4 7 1 5
10
3 2
18 10
19 12
17 13
15
3 5
83 47 36 38 40
13 10 26 68 67
15 19 20 70 90
65
Hint
[Sample Explanation #1]
Since there are possible rectangles formed by the cells visited by JOI, output .

This sample satisfies the constraints of all subtasks.
[Sample Explanation #2]
Since there are possible rectangles formed by the cells visited by JOI, output .

This sample satisfies the constraints of subtasks .
[Sample Explanation #3]
For example, the following rectangle can be formed by the cells visited by JOI. Since there are possible rectangles in total, output .

This sample satisfies the constraints of subtasks .
[Constraints]
This problem uses bundled testdata.
For of the testdata, , , ().
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): no additional constraints.
Translated from JOI 2022 Final T5「砂の城 2 / Sandcastle 2」
Translated by ChatGPT 5