Cornfields


Submit solution

Points: 100 (partial)
Time limit: 1.0s
Memory limit: 128M

Author:
Problem type
Allowed languages
Ada, BrainF***, C, C#, C++, Dart, Go, Java, JS, Kotlin, Lua, Pascal, Prolog, Python, Swift, VB

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find.

FJ has, at great expense, surveyed his square farm of N x N hectares (1 \leq N \leq 250). Each hectare has an integer elevation (0 \leq elevation \leq 250) associated with it.

FJ will present your program with the elevations and a set of K (1 \leq K \leq 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 \leq B \leq N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.

INPUT FORMAT

Line 1: Three space-separated integers: N, B, and K.

Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.

Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.

SAMPLE INPUT

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

OUTPUT FORMAT

Lines 1..K: A single integer per line representing the difference between the max and the min in each query.

SAMPLE OUTPUT

5

Comments

There are no comments at the moment.