## IOI '19 P3 - Rectangles

View as PDF

Points: 30 (partial)
Time limit: 2.5s
Memory limit: 1G

Problem type
Allowed languages
C++

In the early 19th century, the ruler Hoseyngulu Khan Sardar ordered a palace to be built on a plateau overseeing a beautiful river. The plateau is modeled as an grid of square cells. The rows of the grid are numbered through , and the columns are numbered through . We refer to the cell in row and column as cell . Each cell has a specific height, denoted by .

Hoseyngulu Khan Sardar asked his architects to choose a rectangular area to build the palace. The area should not contain any cell from the grid boundaries (row , row , column , and column ). Hence, the architects should choose four integers and and , which define an area consisting of all cells such that and .

In addition, an area is considered valid, if and only if for every cell in the area, the following condition holds:

• Consider the two cells adjacent to the area in row cell and cell and the two cells adjacent to the area in column cell and cell . The height of cell should be strictly smaller than the heights of all these four cells.

Your task is to help the architects find the number of valid areas for the palace (i.e., the number of choices of and that define a valid area).

#### Implementation details

You should implement the following procedure:

long long count_rectangles(std::vector<std::vector<int>> a)

• : a two-dimensional by array of integers representing the heights of the cells.
• This procedure should return the number of valid areas for the palace.

#### Examples

##### Example 1

Consider the following call.

count_rectangles([[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]])


There are valid areas, listed below:

For example is a valid area because both following conditions hold:

• is strictly smaller than and .
• is strictly smaller than and .
##### Constraints
• for all
1. ( points)
2. ( points)
3. ( points)
4. ( points)
5. ( points)
6. ( points) for all
7. ( points) No additional constraints.

The sample grader prints a single line containing the return value of count_rectangles.