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 "rect.h"
using namespace std;
//int max_in_row[205][205][205]; // max_in_row[i][j][k] = max{a[i][j..k]}
int col_prev_larger[705][705]; // stores prev row
//int min_col_prev_larger[205][205][205]; // stores prev row
int col_next_larger[705][705]; // stores next row
//int max_col_next_larger[205][205][205]; // stores next row
int mir[705][705]; // mir[i][k] = max{a[i][c..k]}
int mcpl[705][705];
int mcnl[705][705];
long long count_rectangles(std::vector<std::vector<int> > a) {
int n = (int)a.size();
int m = (int)a[0].size();
if(n < 3 || m < 3) return 0;
for(int i=1; i<n-1; i++){
for(int j=1; j<m-1; j++){
/*
max_in_row[i][j][j] = a[i][j];
for(int k=j+1; k<m-1; k++){
max_in_row[i][j][k] = max(max_in_row[i][j][k-1], a[i][k]);
}
*/
col_prev_larger[i][j] = 1e9;
for(int k=i-1; k>=0; k--){
if(a[k][j] > a[i][j]){
col_prev_larger[i][j] = k;
break;
}
}
if(col_prev_larger[i][j] == 1e9) col_prev_larger[i][j] = -1;
col_next_larger[i][j] = -1;
for(int k=i+1; k<n; k++){
if(a[k][j] > a[i][j]){
col_next_larger[i][j] = k;
break;
}
}
if(col_next_larger[i][j] == -1) col_next_larger[i][j] = 1e9;
}
}
/*
for(int r=1; r<n-1; r++){
for(int i=1; i<m-1; i++){
min_col_prev_larger[r][i][i] = col_prev_larger[r][i];
for(int j=i+1; j<m-1; j++){
min_col_prev_larger[r][i][j] = min(min_col_prev_larger[r][i][j-1], col_prev_larger[r][j]);
}
max_col_next_larger[r][i][i] = col_next_larger[r][i];
for(int j=i+1; j<m-1; j++){
max_col_next_larger[r][i][j] = max(max_col_next_larger[r][i][j-1], col_next_larger[r][j]);
}
}
}
*/
long long res = 0;
for(int c=1; c<m-1; c++){
for(int r=1; r<n-1; r++){
mir[r][c] = a[r][c];
for(int cc=c+1; cc<m-1; cc++){
mir[r][cc] = max(mir[r][cc-1], a[r][cc]);
}
}
for(int r=1; r<n-1; r++){
mcpl[r][c] = col_prev_larger[r][c];
for(int cc=c+1; cc<m-1; cc++){
mcpl[r][cc] = min(mcpl[r][cc-1], col_prev_larger[r][cc]);
}
mcnl[r][c] = col_next_larger[r][c];
for(int cc=c+1; cc<m-1; cc++){
mcnl[r][cc] = max(mcnl[r][cc-1], col_next_larger[r][cc]);
}
}
for(int r=1; r<n-1; r++){
for(int cc=c; cc<m-1; cc++){
int max_col_req = -1;
int min_col_req = 1e9;
for(int rr=r; rr<n-1; rr++){
// max_col_req = max(max_col_req, max_col_next_larger[rr][c][cc]);
// min_col_req = min(min_col_req, min_col_prev_larger[rr][c][cc]);
max_col_req = max(max_col_req, mcnl[rr][cc]);
min_col_req = min(min_col_req, mcpl[rr][cc]);
if(min_col_req != r-1) break;
//if(max_in_row[rr][c][cc] >= a[rr][c-1] || max_in_row[rr][c][cc] >= a[rr][cc+1]) break;
if(mir[rr][cc] >= a[rr][c-1] || mir[rr][cc] >= a[rr][cc+1]) break;
if(max_col_req == rr+1) res++;
}
}
}
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |