This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2505;
const int inf = 1e9;
typedef pair<int, int> pii;
typedef pair<pii, pii> rect;
int prv[2][MAXN][MAXN];
int nxt[2][MAXN][MAXN];
int n, m;
void make(int store[][MAXN], bool rc, bool dir, vector<vector<int>> &a, int v) {
vector<pii> stck;
stck.push_back(pii(inf, -1));
int sz = (rc ? n : m);
for (int i = (dir ? sz-1 : 0); (dir ? i >= 0 : i < sz); (dir ? i-- : i++)) {
int cur = (rc) ? a[i][v] : a[v][i];
while (cur >= stck.back().first) stck.pop_back();
if (rc) store[i][v] = stck.back().second;
else store[v][i] = stck.back().second;
stck.push_back(pii(cur, i));
}
}
vector<int> occ[2][MAXN][MAXN];
int cnt_leq(vector<int> &v, int i) {
int mn = -1;
int mx = v.size();
while (mx > mn+1) {
int cur = (mn+mx)/2;
if (v[cur] <= i) mn = cur;
else mx = cur;
}
return mx;
}
void print(rect &r) {
cout << r.first.first << ' ' << r.first.second << ' ' << r.second.first << ' ' << r.second.second << '\n';
}
vector<rect> cands;
long long count_rectangles(vector<vector<int>> a) {
n = a.size();
m = a[0].size();
for (int i = 0; i < n; i++) {
make(prv[0], 0, 0, a, i);
make(nxt[0], 0, 1, a, i);
}
for (int i = 0; i < m; i++) {
make(prv[1], 1, 0, a, i);
make(nxt[1], 1, 1, a, i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int suc = 0;
for (int t = 0; t < 2; t++) {
int rcpos = (t ? i : j);
if (nxt[t][i][j] != -1 && prv[t][i][j] != -1) {
suc++;
if (nxt[t][i][j]) {
occ[t][prv[t][i][j]+1][nxt[t][i][j]-1].push_back(i^j^rcpos);
}
}
}
if (suc == 2) {
// cerr << i << ' ' << j << '\n';
cands.push_back(rect(pii(prv[1][i][j]+1, prv[0][i][j]+1), pii(nxt[1][i][j]-1, nxt[0][i][j]-1)));
}
}
}
for (int u = 1; u < m-1; u++) {
for (int v = u; v < m-1; v++) {
sort(occ[0][u][v].begin(), occ[0][u][v].end());
int p = 0;
int pv = -1;
for (int i = 0; i < occ[0][u][v].size(); i++) {
if (occ[0][u][v][i] == pv) continue;
pv = occ[0][u][v][i];
swap(occ[0][u][v][p++], occ[0][u][v][i]);
}
occ[0][u][v].resize(p);
}
}
for (int u = 1; u < n-1; u++) {
for (int v = u; v < n-1; v++) {
sort(occ[1][u][v].begin(), occ[1][u][v].end());
int p = 0;
int pv = -1;
for (int i = 0; i < occ[1][u][v].size(); i++) {
if (occ[1][u][v][i] == pv) continue;
pv = occ[1][u][v][i];
swap(occ[1][u][v][p++], occ[1][u][v][i]);
}
occ[1][u][v].resize(p);
}
}
int ans = 0;
// cerr << cands.size() << '\n';
sort(cands.begin(), cands.end());
int p = 0;
rect pv(pii(-1, -1), pii(-1, -1));
for (int i = 0; i < cands.size(); i++) {
if (cands[i] == pv) continue;
pv = cands[i];
swap(cands[p++], cands[i]);
}
cands.resize(p);
for (rect r: cands) {
// print(r);
pii up = r.first;
pii down = r.second;
pii xrange = pii(up.first, down.first);
pii yrange = pii(up.second, down.second);
if ((cnt_leq(occ[0][yrange.first][yrange.second], xrange.second)-cnt_leq(occ[0][yrange.first][yrange.second], xrange.first-1) == xrange.second-xrange.first+1)
&& (cnt_leq(occ[1][xrange.first][xrange.second], yrange.second)-cnt_leq(occ[1][xrange.first][xrange.second], yrange.first-1) == yrange.second-yrange.first+1)) {
// cerr << xrange.second-xrange.first+1 << ' ' << cnt_leq(occ[0][yrange], xrange.second)
// << ' ' << cnt_leq(occ[0][yrange], xrange.first-1) << '\n';
// print(r);
ans++;
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < occ[0][u][v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
rect.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i < occ[1][u][v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~
rect.cpp:112:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for (int i = 0; i < cands.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | 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... |