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<int> > TA(W, vector<int>(H));
for(int i = 0; i < H; ++i) {
for(int j = 0; j < W; ++j) {
TA[j][i] = A[i][j];
}
}
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 d = (j - 1) * (j - 2) / 2 - 1;
int mx = -inf;
for(int k = j - 1; k >= 1; --k) {
mx = max(mx, A[i][k]);
flag_row[i][d + k] = (A[i][k - 1] > mx && A[i][j] > mx);
}
}
}
for(int i = 0; i < W; ++i) {
for(int j = 1; j < H; ++j) {
int d = (j - 1) * (j - 2) / 2 - 1;
int mx = -inf;
for(int k = j - 1; k >= 1; --k) {
mx = max(mx, TA[i][k]);
flag_col[i][d + k] = (TA[i][k - 1] > mx && TA[i][j] > mx);
}
}
}
vector<vector<short> > sum_row(H + 1, vector<short>((W - 1) * (W - 2) / 2));
vector<vector<short> > sum_col(W + 1, vector<short>((H - 1) * (H - 2) / 2));
for(int i = 0; i < H; ++i) {
int d = (W - 1) * (W - 2) / 2;
for(int j = 0; j < d; ++j) {
sum_row[i + 1][j] = sum_row[i][j] + flag_row[i][j];
}
}
for(int i = 0; i < W; ++i) {
int d = (H - 1) * (H - 2) / 2;
for(int j = 0; j < d; ++j) {
sum_col[i + 1][j] = sum_col[i][j] + flag_col[i][j];
}
}
long long ans = 0;
for(int xb = 1; xb < H; ++xb) {
int dx = (xb - 1) * (xb - 2) / 2 - 1;
for(int yb = 1; yb < W; ++yb) {
int dy = (yb - 1) * (yb - 2) / 2 - 1;
for(int ya = 1; ya < yb; ++ya) {
int yid = dy + ya;
if(!flag_row[xb - 1][yid]) continue;
// ----- At most N patterns for each xa ----- //
for(int xa = 1; xa < xb; ++xa) {
int xid = dx + xa;
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... |