Statisticians
Time limit1sMemory limit128 MB
Given a grid of counts, take the median of the mean densities over all axis-aligned subrectangles whose area lies in [a,b].
- Level
Hard8 of 10
- Topics
- Prefix sum, Binary search, Sorting
- Solved
- No attempts yet
Problem
Statisticians like to compute many statistics. One simple measure is the mean: the sum of all values divided by the number of values. Another is the median: the middle value once all values have been sorted. If there is an even number of values, the median is the mean of the two middle values.
Such measures can describe, for example, the population of a country or part of it. Anne Jensen, Maria Virtanen, Jan Hansen, Erik Johansson and Jón Þórsson want to measure statistically how many statisticians there are in the Nordic countries — more precisely, how many there are per unit area. Because the population is spread out, they use a new measurement called MAD (Median of All Densities):
- Overlay a square grid on the map.
- For every axis-aligned rectangle of grid cells whose area lies within the allowed range, compute its density: the mean number of statisticians per unit area (the total inside the rectangle divided by its area).
- Repeat for every possible rectangle; the MAD is the median of all these densities.
Given the grid and the allowed range of rectangle areas, compute the MAD.
Input
The first line contains two space-separated integers and , the height and width of the grid (, ).
The second line contains two space-separated integers and , the lower and upper bounds on the allowed rectangle area; every considered rectangle has area in , with .
Each of the next lines contains space-separated integers , the number of statisticians in each cell of the grid ().
It is guaranteed that at least one rectangle has area within .
Output
Print a single line containing the MAD — the number of statisticians per unit area — rounded to three decimal places.