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 <bits/stdc++.h>
using namespace std;
#include "rect.h"
using ll = long long;
template<class T>
using vec = vector<T>;
vec<vec<int>> transpose(const vec<vec<int>>& g) {
vec<vec<int>> h(g[0].size(), vec<int>(g.size()));
for (int i = 0; i < g.size(); i++) {
for (int j = 0; j < g[i].size(); j++) h[j][i] = g[i][j];
}
return h;
}
ll _count_rectangles(const vec<vec<int>>& g) {
int r, c;
if ((r = g.size()) < (c = g[0].size())) return _count_rectangles(transpose(g));
vec<vec<int>> U(r, vec<int>(c, -1));
vec<vec<int>> D(r, vec<int>(c, r));
vec<vec<int>> L(r, vec<int>(c, -1));
vec<vec<int>> R(r, vec<int>(c, c));
for (int j = 0; j < c; j++) {
{
vec<int> stak;
for (int i = 0; i < r; i++) {
while (not stak.empty() and g[stak.back()][j] < g[i][j]) stak.pop_back();
if (not stak.empty()) U[i][j] = stak.back();
stak.push_back(i);
}
}
{
vec<int> stak;
for (int i = r; i--;) {
while (not stak.empty() and g[stak.back()][j] < g[i][j]) stak.pop_back();
if (not stak.empty()) D[i][j] = stak.back();
stak.push_back(i);
}
}
}
for (int i = 0; i < r; i++) {
{
vec<int> stak;
for (int j = 0; j < c; j++) {
while (not stak.empty() and g[i][stak.back()] < g[i][j]) stak.pop_back();
if (not stak.empty()) L[i][j] = stak.back();
stak.push_back(j);
}
}
{
vec<int> stak;
for (int j = c; j--;) {
while (not stak.empty() and g[i][stak.back()] < g[i][j]) stak.pop_back();
if (not stak.empty()) R[i][j] = stak.back();
stak.push_back(j);
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
assert(-1 <= U[i][j] and U[i][j] < i and i < D[i][j] and D[i][j] <= r);
assert(-1 <= L[i][j] and L[i][j] < j and j < R[i][j] and R[i][j] <= c);
}
}
vec<vec<vec<int>>> poss(c + 1, vec<vec<int>>(c + 1));
vec<vec<vec<pair<int,int>>>> ques(c + 1, vec<vec<pair<int,int>>>(c + 1));
ll ans = 0;
vec<pair<int,int>> jque = {{0, c-1}};
vec<vec<int>> ips(r);
vec<vec<pair<int,int>>> jps(r);
while (not jque.empty()) {
auto [j1, j2] = jque.back(); jque.pop_back();
if (j2 - j1 <= 1) continue;
int jm = j1 + j2 >> 1;
jque.emplace_back(j1, jm);
jque.emplace_back(jm, j2);
for (int i = 0; i < r; i++) {
for (int J1 = j1; J1 < jm; J1++) {
int J2 = R[i][J1];
if (jm < J2 and J2 <= j2) jps[i].emplace_back(J1, J2);
}
for (int J2 = jm+1; J2 < j2+1; J2++) {
int J1 = L[i][J2];
if (j1 <= J1 and J1 < jm and R[i][J1] != J2) jps[i].emplace_back(J1, J2);
}
}
for (int i1 = 0; i1 < r; i1++) {
int i2 = D[i1][jm];
if (0 <= i2 and i2 < r and i2 - i1 >= 2) ips[i1].push_back(i2);
}
for (int i2 = 0; i2 < r; i2++) {
int i1 = U[i2][jm];
if (0 <= i1 and i1 < r and i2 - i1 >= 2 and i2 != D[i1][jm]) ips[i1].push_back(i2);
}
for (int i = 0; i < r; i++) {
for (auto [J1, J2] : jps[i]) poss[J1][J2].push_back(i);
}
for (int i1 = 0; i1 < r; i1++) {
while (not ips[i1].empty()) {
int i2 = ips[i1].back(); ips[i1].pop_back();
int j1x = jm;
while (j1 < j1x and j1x < j2 and (D[i1][j1x] == i2 or U[i2][j1x] == i1)) j1x--;
int j2x = jm;
while (j1 < j2x and j2x < j2 and (D[i1][j2x] == i2 or U[i2][j2x] == i1)) j2x++;
for (auto [J1, J2] : jps[i1+1]) {
if (j1x <= J1 and J1 < J2 and J2 <= j2x) ques[J1][J2].emplace_back(i1+1, i2);
}
}
}
for (int i = 0; i < r; i++) {
while (not jps[i].empty()) {
auto [J1, J2] = jps[i].back(); jps[i].pop_back();
int im = c, ix = c;
while (not poss[J1][J2].empty()) {
int i = poss[J1][J2].back(); poss[J1][J2].pop_back();
if (ix != i + 1) im = i + 1;
ix = i;
while (not ques[J1][J2].empty() and ques[J1][J2].back().first >= i) {
auto [i1, i2] = ques[J1][J2].back(); ques[J1][J2].pop_back();
assert(i1 == i);
if (i2 <= im) ans++;
}
}
}
}
}
return ans;
}
ll count_rectangles(vec<vec<int>> g) {
return _count_rectangles(g);
}
Compilation message (stderr)
rect.cpp: In function 'vec<std::vector<int> > transpose(vec<std::vector<int> >&)':
rect.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i = 0; i < g.size(); i++) {
| ~~^~~~~~~~~~
rect.cpp:11:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int j = 0; j < g[i].size(); j++) h[j][i] = g[i][j];
| ~~^~~~~~~~~~~~~
rect.cpp: In function 'll _count_rectangles(vec<std::vector<int> >&)':
rect.cpp:77:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int jm = j1 + j2 >> 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... |