Camouflaged Camp
Time limit1sMemory limit128 MB
Using prefix sums over the grid, find the L x W camp position that satisfies the most given adjacent-area altitude comparison rules, breaking ties by smallest row then column.
- Level
Medium6 of 10
- Topics
- Prefix sum, Sliding window, Implementation
- Solved
- No attempts yet
Problem
A squad commander must choose where to build a camouflaged camp of a fixed size. He has a digital topographic map of the area — a rectangular grid in which every cell stores the altitude at that coordinate. A cell is identified by its row and column, both counted from 1.

A 5×6 topographic map with a 3×5 camp placed at position (3, 2).
The camp is an L × W rectangle (L rows and W columns) that must lie entirely inside the map. A camp location is described by the coordinates (row, column) of its top-left cell on the map.
Each candidate location is scored against a list of characteristics. A characteristic compares two equal, adjacent rectangular areas that lie inside the camp. It is described by:
- location — the
(row, column)of the top-left cell of the first area, given relative to the camp (the camp's own top-left cell is(1, 1)); - size — the number of rows and columns of each area (both areas have the same size);
- arrangement flag —
0means the two areas sit side by side horizontally (they share a vertical edge, so the second area is immediately to the right of the first);1means they are stacked vertically (they share a horizontal edge, so the second area is immediately below the first); - altitude flag —
0means the location satisfies the characteristic when the average altitude of the first area is strictly less than that of the second;1means it is satisfied when the average altitude of the first area is greater than or equal to that of the second.
Because the two areas always contain the same number of cells, comparing their average altitudes is equivalent to comparing their altitude sums.

Characteristic A — location (1, 1), size (1, 3), arrangement flag 1, altitude flag 0. The camp shown above satisfies this characteristic.

Characteristic B — location (2, 2), size (2, 2), arrangement flag 0, altitude flag 0. The camp shown above does not satisfy this characteristic.
Given the map, the camp size, and the characteristics, find a camp location that satisfies as many characteristics as possible.
Input
- The first line contains two integers
RandC(2 ≤ R, C ≤ 1000) — the number of rows and columns of the topographic map. - Each of the next
Rlines containsCnon-negative integers; the value in rowi, columnjis the altitude at that coordinate and never exceeds255. - The next line contains two integers
LandW(1 ≤ L ≤ R,1 ≤ W ≤ C) — the number of rows and columns of the camp. - The next line contains one integer
H(1 ≤ H ≤ 1000) — the number of characteristics. - Each of the following
Hlines contains six integers describing one characteristic: the row and column of the top-left cell of its first area (relative to the camp), the number of rows and columns of each area, the arrangement flag, and the altitude flag. Every characteristic fits entirely inside the camp.
Output
Print two integers — the row and column (on the map) of the top-left cell of a best camp location, i.e. one that satisfies the greatest possible number of characteristics.
If several locations satisfy that maximum number, print the lexicographically smallest one: the smallest row, and among locations with that row, the smallest column.