int rows = grid.size(); int cols = grid[0].size(); long long count = 0; // Precompute the count of '1's in each row and each column std::vector rowCount(rows, 0); std::vector colCount(cols, 0); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (grid[i][j]) { rowCount[i]++; colCount[j]++; } } } // Iterate through each cell of the grid for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { // If current cell is 1 if (grid[i][j]) { // Calculate the count of right triangles with this cell as the right angle count += static_cast(rowCount[i] - 1) * (colCount[j] - 1); } } } return count;
Пікірлер: 1