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>
#include <tuple>
#include <unordered_set>
using namespace std;
typedef long long llong;
typedef pair<int, int> pii;
const int inf = 1e8;
int n, m;
int L[2501][2501];
int R[2501][2501];
int U[2501][2501];
int D[2501][2501];
int M[2501][2501];
int idx[2501][2501];
int ans[2501 * 2501];
struct {
vector<pii> Q[2501];
void clear() {
for (int i = 1; i <= 2500; ++i) Q[i].clear();
}
void add(int x, int y, int i) {
Q[y].emplace_back(x, i);
}
void query(vector<int> &v) {
vector<pii> st;
for (int i = 1; i <= v.size(); ++i) {
st.emplace_back(i, v[i - 1]);
sort(Q[i].rbegin(), Q[i].rend());
for (pii j : Q[i]) {
int x, it;
tie(x, it) = j;
int val = inf;
while (!st.empty() && x <= st.back().first) {
val = min(val, st.back().second);
st.pop_back();
}
ans[it] = val;
st.emplace_back(x, val);
}
}
}
} Qs[2501];
llong count_rectangles(vector<vector<int>> a) {
n = a.size();
m = a[0].size();
//step 1
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= m; ++j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
st.pop_back();
L[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(m + 1);
for (int j = m; j > 0; --j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] <= a[i - 1][j - 1])
st.pop_back();
R[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= n; ++j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
st.pop_back();
U[j][i] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(n + 1);
for (int j = n; j > 0; --j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] <= a[j - 1][i - 1])
st.pop_back();
D[j][i] = st.back();
st.push_back(j);
}
}
//step 2
unordered_set<llong> sp;
int q = 0;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
int Lx = L[i][j] + 1;
if (Lx == 1) continue;
int Rx = R[i][j] - 1;
if (Rx == m) continue;
int Ux = U[i][j] + 1;
if (Ux == 1) continue;
int Dx = D[i][j] - 1;
if (Dx == n) continue;
llong id = ((llong)(Lx << 12 | Rx)) << 24 | (Ux << 12 | Dx);
if (sp.count(id)) continue;
sp.insert(id);
idx[i][j] = ++q;
}
}
//step 3
//Li
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= m; ++j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
st.pop_back();
M[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) Qs[i].clear();
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
Qs[Rx + 1].add(Ux, Dx, idx[i][j]);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> v;
for (int j = 1; j <= n; ++j) {
v.push_back(-M[j][i]);
}
Qs[i].query(v);
}
q = 0;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
if (-ans[idx[i][j]] + 1 > Lx) continue;
idx[i][j] = ++q;
}
}
//Ri
for (int i = 1; i <= n; ++i) {
vector<int> st;
st.push_back(m + 1);
for (int j = m; j > 0; --j) {
while (st.size() > 1 && a[i - 1][st.back() - 1] < a[i - 1][j - 1])
st.pop_back();
M[i][j] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= m; ++i) Qs[i].clear();
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
Qs[Lx - 1].add(Ux, Dx, idx[i][j]);
}
}
for (int i = 1; i <= m; ++i) {
vector<int> v;
for (int j = 1; j <= n; ++j) {
v.push_back(M[j][i]);
}
Qs[i].query(v);
}
q = 0;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
if (ans[idx[i][j]] - 1 > Rx) continue;
idx[i][j] = ++q;
}
}
//Ui
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(0);
for (int j = 1; j <= n; ++j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
st.pop_back();
M[j][i] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= n; ++i) Qs[i].clear();
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
Qs[Dx + 1].add(Lx, Rx, idx[i][j]);
}
}
for (int i = 1; i <= n; ++i) {
vector<int> v;
for (int j = 1; j <= m; ++j) {
v.push_back(-M[i][j]);
}
Qs[i].query(v);
}
q = 0;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
if (-ans[idx[i][j]] + 1 > Ux) continue;
idx[i][j] = ++q;
}
}
//Di
for (int i = 1; i <= m; ++i) {
vector<int> st;
st.push_back(n + 1);
for (int j = n; j > 0; --j) {
while (st.size() > 1 && a[st.back() - 1][i - 1] < a[j - 1][i - 1])
st.pop_back();
M[j][i] = st.back();
st.push_back(j);
}
}
for (int i = 1; i <= n; ++i) Qs[i].clear();
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
Qs[Ux - 1].add(Lx, Rx, idx[i][j]);
}
}
for (int i = 1; i <= n; ++i) {
vector<int> v;
for (int j = 1; j <= m; ++j) {
v.push_back(M[i][j]);
}
Qs[i].query(v);
}
q = 0;
for (int i = 2; i < n; ++i) {
for (int j = 2; j < m; ++j) {
if (idx[i][j] == 0) continue;
int Lx = L[i][j] + 1;
int Rx = R[i][j] - 1;
int Ux = U[i][j] + 1;
int Dx = D[i][j] - 1;
if (ans[idx[i][j]] - 1 < Dx) continue;
idx[i][j] = ++q;
}
}
return q;
}
Compilation message (stderr)
rect.cpp: In member function 'void<unnamed struct>::query(std::vector<int>&)':
rect.cpp:31:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i <= v.size(); ++i) {
~~^~~~~~~~~~~
rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:128:17: warning: unused variable 'Lx' [-Wunused-variable]
int Lx = L[i][j] + 1;
^~
rect.cpp:147:17: warning: unused variable 'Rx' [-Wunused-variable]
int Rx = R[i][j] - 1;
^~
rect.cpp:148:17: warning: unused variable 'Ux' [-Wunused-variable]
int Ux = U[i][j] + 1;
^~
rect.cpp:149:17: warning: unused variable 'Dx' [-Wunused-variable]
int Dx = D[i][j] - 1;
^~
rect.cpp:170:17: warning: unused variable 'Rx' [-Wunused-variable]
int Rx = R[i][j] - 1;
^~
rect.cpp:187:17: warning: unused variable 'Lx' [-Wunused-variable]
int Lx = L[i][j] + 1;
^~
rect.cpp:189:17: warning: unused variable 'Ux' [-Wunused-variable]
int Ux = U[i][j] + 1;
^~
rect.cpp:190:17: warning: unused variable 'Dx' [-Wunused-variable]
int Dx = D[i][j] - 1;
^~
rect.cpp:212:17: warning: unused variable 'Ux' [-Wunused-variable]
int Ux = U[i][j] + 1;
^~
rect.cpp:228:17: warning: unused variable 'Lx' [-Wunused-variable]
int Lx = L[i][j] + 1;
^~
rect.cpp:229:17: warning: unused variable 'Rx' [-Wunused-variable]
int Rx = R[i][j] - 1;
^~
rect.cpp:231:17: warning: unused variable 'Dx' [-Wunused-variable]
int Dx = D[i][j] - 1;
^~
rect.cpp:254:17: warning: unused variable 'Dx' [-Wunused-variable]
int Dx = D[i][j] - 1;
^~
rect.cpp:269:17: warning: unused variable 'Lx' [-Wunused-variable]
int Lx = L[i][j] + 1;
^~
rect.cpp:270:17: warning: unused variable 'Rx' [-Wunused-variable]
int Rx = R[i][j] - 1;
^~
rect.cpp:271:17: warning: unused variable 'Ux' [-Wunused-variable]
int Ux = U[i][j] + 1;
^~
# | 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... |