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 <bits/stdc++.h>
using namespace std;
vector<int> lg{-1};
struct RMQ {
vector<vector<int>> sp;
void init (vector<int> &a) {
int n = a.size();
sp.resize(lg[n]+1, vector<int>(n));
sp[0] = a;
for (int i = 1; i < lg[n]+1; ++i) {
for (int j = 0; j < n - (1 << i) + 1; ++j) {
sp[i][j] = max(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
}
}
}
int query (int l, int r) {
assert(r >= l);
int k = lg[r-l+1];
return max(sp[k][l], sp[k][r - (1 << k) + 1]);
}
};
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
for (int i = 1; i <= n; ++i) {
lg.push_back(lg.back()+!(i&(i-1)));
}
vector<RMQ> col(m);
for (int j = 1; j < m - 1; ++j) {
vector<int> p(n);
for (int i = 0; i < n; ++i) {
p[i] = a[i][j];
}
col[j].init(p);
}
vector<int> row(n), vis(n);
int vid = 0;
long long ans = 0;
for (int x = 1; x < n - 1; ++x) {
for (int y = 1; y < m - 1; ++y) {
for (int i = x; i < n - 1; ++i) {
if (a[i][y] >= a[i][y - 1] || a[i][y] >= a[x - 1][y]) {
break;
}
++vid;
bool bad = 0;
for (int j = y; j < m - 1 && !bad; ++j) {
bool good = 1;
for (int l = x; l <= i; ++l) {
if (vis[l] != vid) {
vis[l] = vid;
row[l] = 0;
}
int mx = row[l] = max(row[l], a[l][j]);
// assert(mx == *max_element(a[l].begin()+y, a[l].begin()+j+1));
good &= mx < a[l][j + 1] && mx < a[l][y - 1];
bad |= mx >= a[l][y - 1];
}
int mx = col[j].query(x, i);
good &= mx < a[i + 1][j] && mx < a[x - 1][j];
bad |= mx >= a[x - 1][j] || mx >= a[i + 1][j];
ans += good;
}
}
}
}
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... |