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"
#include <vector>
#include <algorithm>
using namespace std;
long long count_rectangles(std::vector<std::vector<int> > A) {
const int inf = 1012345678;
int H = A.size(), W = A[0].size();
vector<vector<bool> > flag_row(H, vector<bool>((W - 1) * (W - 2) / 2));
vector<vector<bool> > flag_col(W, vector<bool>((H - 1) * (H - 2) / 2));
for(int i = 0; i < H; ++i) {
for(int j = 1; j < W; ++j) {
int mx = -inf;
for(int k = j + 1; k < W; ++k) {
mx = max(mx, A[i][k - 1]);
flag_row[i][(k - 1) * (k - 2) / 2 + j - 1] = (A[i][j - 1] > mx && A[i][k] > mx);
}
}
}
for(int i = 0; i < W; ++i) {
for(int j = 1; j < H; ++j) {
int mx = -inf;
for(int k = j + 1; k < H; ++k) {
mx = max(mx, A[k - 1][i]);
flag_col[i][(k - 1) * (k - 2) / 2 + j - 1] = (A[j - 1][i] > mx && A[k][i] > mx);
}
}
}
vector<vector<int> > sum_row(H + 1, vector<int>((W - 1) * (W - 2) / 2));
vector<vector<int> > sum_col(W + 1, vector<int>((H - 1) * (H - 2) / 2));
for(int i = 0; i < H; ++i) {
for(int j = 0; j < (W - 1) * (W - 2) / 2; ++j) {
sum_row[i + 1][j] = sum_row[i][j] + flag_row[i][j];
}
}
for(int i = 0; i < W; ++i) {
for(int j = 0; j < (H - 1) * (H - 2) / 2; ++j) {
sum_col[i + 1][j] = sum_col[i][j] + flag_col[i][j];
}
}
long long ans = 0;
for(int xa = 1; xa < H; ++xa) {
for(int ya = 1; ya < W; ++ya) {
for(int yb = ya + 1; yb < W; ++yb) {
int yid = (yb - 1) * (yb - 2) / 2 + ya - 1;
if(!flag_row[xa][yid]) continue;
// ----- At most N patterns for each xa ----- //
for(int xb = xa + 1; xb < H; ++xb) {
int xid = (xb - 1) * (xb - 2) / 2 + xa - 1;
int sr = sum_row[xb][yid] - sum_row[xa][yid];
int sc = sum_col[yb][xid] - sum_col[ya][xid];
if(sr == xb - xa && sc == yb - ya) {
++ans;
}
}
}
}
}
return ans;
}
# | 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... |