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<array<int, 2>>> sp;
void init (vector<array<int, 2>> &a) {
int n = a.size();
sp.resize(lg[n]+1, vector<array<int, 2>>(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][0] = max(sp[i - 1][j][0], sp[i - 1][j + (1 << (i - 1))][0]);
sp[i][j][1] = max(sp[i - 1][j][1], sp[i - 1][j + (1 << (i - 1))][1]);
}
}
}
int query (int l, int r, int i) {
int k = lg[r-l+1];
return max(sp[k][l][i], sp[k][r - (1 << k) + 1][i]);
}
};
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
int left[n][m], right[n][m];
for (int i = 0; i < n; ++i) {
stack<int> st;
for (int j = 0; j < m; ++j) {
while (st.size() && a[i][st.top()] < a[i][j]) {
st.pop();
}
left[i][j] = st.size() ? st.top() + 1 : 0;
st.push(j);
}
}
for (int i = 0; i < n; ++i) {
stack<int> st;
for (int j = m - 1; ~j; --j) {
while (st.size() && a[i][st.top()] < a[i][j]) {
st.pop();
}
right[i][j] = st.size() ? st.top() - 1 : m - 1;
st.push(j);
}
}
for (int i = 1; i <= n; ++i) {
lg.push_back(lg.back()+!(i&(i-1)));
}
RMQ col[m];
for (int j = 0; j < m; ++j) {
vector<array<int, 2>> p(n);
for (int i = 0; i < n; ++i) {
p[i] = {a[i][j], left[i][j]};
}
col[j].init(p);
}
long long ans = 0;
for (int x = 1; x < n - 1; ++x) {
for (int y = 1; y < m - 1; ++y) {
int M = 1e9;
for (int i = x; i < n - 1; ++i) {
M = min(M, right[i][y - 1]);
if (a[i][y] >= a[i][y - 1] || a[i][y] >= a[x - 1][y]) {
break;
}
bool bad = 0;
int limit = min(m - 1, M + 1);
for (int j = y; j < limit && !bad; ++j) {
bool good = col[j + 1].query(x, i, 1) <= y;
int mx = col[j].query(x, i, 0);
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... |