# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1080405 | speedcode | Rectangles (IOI19_rect) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
long long count_rectangles(std::vector<std::vector<int>> a)
{
long long n = a.size();
long long m = a[0].size();
vector<vector<int>> explored(n);
for(int i = 0; i < n; i++) explored[i].resize(m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
explored[i][j] = 0;
vector<vector<int>> prefix(n);
for(int i = 0; i < n; i++) prefix[i].resize(m);
prefix[0][0] = a[0][0];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(i+j==0) continue;
prefix[i][j] = a[i][j];
if(i > 0) prefix[i][j] += prefix[i-1][j];
if(j > 0) prefix[i][j] += prefix[i][j-1];
if(j > 0 && i > 0) prefix[i][j] -= prefix[i-1][j-1];
}
}
vector<vector<int>> prefixLines(n);
for(int i = 0; i < n; i++) prefixLines[i].resize(m+1);
for(int i = 0; i < n; i++){
prefixLines[i][0] = 0;
for(int j = 0; j < m; j++){
prefixLines[i][j+1] = a[i][j] + prefixLines[i][j];
}
}
vector<vector<int>> prefixColumns(n+1);
for(int i = 0; i <= n; i++) prefixColumns[i].resize(m);
for(int i = 0; i < m; i++){
prefixColumns[0][i] = 0;
for(int j = 0; j < n; j++){
prefixColumns[j+1][i] = a[j][i] + prefixColumns[j][i];
}
}
long long res = 0;
for(long long i = 1; i < n-1; i++){
for(long long j = 1; j < m-1; j++){
if(explored[i][j]) continue;
explored[i][j] = 1;
if(a[i][j] == 1){
continue;
}
long long endX = i;
long long endY = j;
while(endX < n-2 && a[endX+1][endY] == 0){
endX++;
explored[endX][j] = 1;
}
while(endY < m-2 && a[endX][endY+1] == 0){
endY++;
for(int u = i; u <= endX; u++){
explored[u][endY] = 1;
}
}
if(prefix[endX][endY] + prefix[i-1][j-1] - prefix[i-1][endY]-prefix[endX][j-1] != 0) continue;
long long nbOnes = prefixLines[i-1][endY+1] - prefixLines[i-1][j]
+ prefixLines[endX+1][endY+1] - prefixLines[endX+1][j]
+ prefixColumns[endX+1][endY+1] - prefixColumns[i][endY+1]
+ prefixColumns[endX+1][j-1] - prefixColumns[i][j-1];
if(nbOnes == 2*(endX-i+1) + 2*(endY-j+1)){
res++;
}
}
}
return res;
}
int main(){
vector<vector<int>> hi(2500);
for(int i = 0; i < 2500; i++){
vector<int> u(2500);
fill(u.begin(), u.end(), 0);
hi[i] = u;
}
for(int i = 0; i < 2500; i++){
hi[i][0] = 1;
hi[i][2499] = 1;
hi[0][i] = 1;
hi[2499][i] = 1;
}
cout << count_rectangles(hi) << endl;
}