Parking
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
In front of Ilona Maksymivna’s house, there is a large rectangular parking lot with \(n\) rows of \(m\) spaces each. In each parking space, she has placed either a rocket or a car.
Ilona wants to fence off a rectangular area such that the number of rockets within this area strictly exceeds the number of cars. In how many ways can she do this?
Note that she can only draw the boundaries of the area along the edges of the parking spaces.
Input
The first line contains two integers \(n\) and \(m\) — the dimensions of the parking lot.
The next \(n\) lines each contain
\(m\) numbers. The number
0 denotes a car, and the number 1 denotes a
rocket.
Output
In a single line, print an integer — the answer to the problem.
Constraints
\(1 \le n, m \le 200\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 1 0 0 0 1 1 1 0 0 | 7 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Стоянка - Розбір
Замінимо ракету на \(+1\), а автомобіль на \(-1\). Тоді ракет у прямокутнику більше, ніж автомобілів, тоді й лише тоді, коли сума чисел у ньому додатна. Потрібно порахувати кількість підпрямокутників з додатною сумою.
Зафіксуємо верхній рядок \(i\) і будемо опускати нижній рядок \(i'\), підтримуючи для кожного стовпця суму \(S_j\) його елементів у рядках від \(i\) до \(i'\). Тепер потрібно порахувати кількість відрізків стовпців \([j, j']\) з додатною сумою \(S_j + \ldots + S_{j'}\). Авторський розв’язок перебирає всі такі відрізки, накопичуючи суму, — це \(O(n^2 m^2 / 4) \approx 4 \cdot 10^8\) простих операцій, що вкладається в обмеження.
Можна швидше: якщо \(P\) — префіксні суми масиву \(S\), то відрізок \([j + 1, j']\) підходить, коли \(P_{j'} > P_j\), тож потрібно порахувати кількість пар префіксних сум, у яких пізніша більша. Це робиться деревом Фенвіка за \(O(m \log (nm))\), і загальна складність стає \(O(n^2 m \log (nm))\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|