#K69822. Most Frequent Flower in a Garden

    ID: 33172 Type: Default 1000ms 256MiB

Most Frequent Flower in a Garden

Most Frequent Flower in a Garden

You are given a garden represented as an \( R \times C \) grid. Each cell in the grid contains an integer which represents a type of flower. You will be given \( Q \) queries, and each query defines a submatrix of the garden by providing its upper left and lower right coordinates \( (r_1, c_1) \) and \( (r_2, c_2) \) respectively (1-indexed).

For each submatrix query, determine the flower that appears most frequently. In case of a tie, report the smallest flower type among those that occur most frequently.

The input is to be read from stdin and the output should be written to stdout.

inputFormat

The first line contains three integers \( R \), \( C \), and \( Q \) separated by spaces, representing the number of rows, columns, and queries respectively.

The next \( R \) lines each contain \( C \) integers, representing the garden grid. Each integer denotes the type of flower in that cell.

Then, \( Q \) lines follow. Each of these lines contains four integers \( r_1 \), \( c_1 \), \( r_2 \), \( c_2 \) which specify the coordinates of the submatrix (using 1-indexed positions) for which you need to determine the most frequent flower.

outputFormat

For each query, output a single integer on a new line. This integer is the type of the most frequent flower within the specified submatrix. In case multiple flower types are tied for the maximum frequency, output the smallest flower type among them.

## sample
3 3 2
1 2 1
3 1 2
2 2 3
1 1 2 2
2 1 3 3
1

2

</p>