Sandcastle 2
Time limit4sMemory limit1024 MB
Count the rectangles whose cells can be visited by a path that steps only to adjacent cells with strictly lower heights.
- Level
Hard9 of 10
- Topics
- Sorting, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
JOI-kun is playing on a sand beach and makes a sandcastle. The sandcastle lies in a rectangular region of sand made of rows and columns of cells. The cell in the -th row from the north and the -th column from the west has height . All heights are different from each other.
JOI-kun performed the following actions. First, he chose a cell and started moving from it. Then he moved from the current cell to an adjacent cell in one of the four directions. The cell he moved to had to be lower than the current cell. He repeated this zero or more times.
Finally, if you view the cells he visited from above, they form a rectangle. Count the number of possible rectangles formed by the cells JOI-kun visited.
Input
The first line contains two integers and . Each of the next lines contains integers, where the -th integer on the -th line is .
Output
Print one line containing the number of possible rectangles formed by the cells JOI-kun visited.
Constraints
, , , and for all and . Any two different cells have different heights.