Sum of submatrix maxima
InterviewTime limit20sMemory limit1024 MB
Add up the maximum value inside every K by K submatrix of an N by N matrix generated from two arrays.
- Level
Medium5 of 10
- Topics
- Sliding window, Matrix, Queue
- Solved
- No attempts yet
Problem
You have an matrix of nonnegative integers. For every submatrix of , take the largest value inside it and write that value down. Submatrices may overlap, so the list holds values. One entry of can be the largest value of several submatrices, and then it appears in the list once for each of them. Compute the sum of the whole list.
Writing the matrix out would make the input huge, so you are given two arrays and of length together with two integers and instead. The value in row and column is
where and both run from 1 to .
Input
The first line holds the number of test cases . The test cases follow.
The first line of each test case holds four integers , , and , separated by spaces. The next two lines hold integers each: the first line is the array and the second line is the array .
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the sum of the maximum values of all submatrices.
Constraints
Hint
In the first test case of the example the matrix is
3
With there is a single submatrix, so the answer is 3.
In the second test case the matrix is
9 3
1 6
With each of the four entries is its own submatrix, so the answer is .
In the third test case the matrix is
11 11 24
13 13 26
14 14 27
With there are four submatrices whose maxima are 13, 26, 14 and 27, so the answer is 80.