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;
typedef long long ll;
typedef pair<int, int> pll;
#define X first
#define Y second
#define all(x) (x).begin(), (x).end()
#define sep ' '
#define debug(x) cerr << #x << ": " << x << endl;
const int MAXN = 2500 + 10;
struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
int n, m, A[MAXN][MAXN], L[MAXN], R[MAXN];
unordered_map<pll, int, HASH> mp_r[2], mp_c[MAXN];
vector<int> F[MAXN][MAXN];
vector<pair<pll, pll>> ans_vec;
inline bool valid(int l1, int l2, int r1, int r2) {
if (l2 - l1 <= 1 || r2 - r1 <= 1) return false;
return true;
}
inline void add(int l1, int l2, int r1, int r2) {
ans_vec.push_back({{l1, l2}, {r1, r2}});
}
inline int get(unordered_map<pll, int, HASH>& mp, pll f) {
auto it = mp.find(f);
if (it == mp.end()) return 0;
return it -> Y;
}
ll count_rectangles(vector<vector<int>> vec_) {
n = vec_.size();
m = vec_[0].size();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
A[i][j] = vec_[i - 1][j - 1];
vec_.clear();
vec_.shrink_to_fit();
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++)
for (L[i] = i - 1; L[i] > 0 && A[L[i]][j] < A[i][j]; L[i] = L[L[i]]);
for (int i = n; i > 0; i--)
for (R[i] = i + 1; R[i] <= n && A[R[i]][j] < A[i][j]; R[i] = R[R[i]]);
for (int i = 1; i <= n; i++) {
if (L[i] && i - L[i] > 1) {
mp_c[j][pll(L[i], i)] = get(mp_c[j - 1], pll(L[i], i)) + 1;
F[i][j].push_back(L[i]);
}
if (R[i] <= n && R[i] - i > 1) {
mp_c[j][pll(i, R[i])] = get(mp_c[j - 1], pll(i, R[i])) + 1;
F[R[i]][j].push_back(i);
}
F[i][j].shrink_to_fit();
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
sort(all(F[i][j]));
F[i][j].resize(unique(all(F[i][j])) - F[i][j].begin());
}
}
for (int i = 1; i <= n; i++) {
int ind = i & 1;
mp_r[ind].clear();
for (int j = 1; j <= m; j++)
for (L[j] = j - 1; L[j] > 0 && A[i][L[j]] < A[i][j]; L[j] = L[L[j]]);
for (int j = m; j > 0; j--)
for (R[j] = j + 1; R[j] <= m && A[i][R[j]] < A[i][j]; R[j] = R[R[j]]);
for (int j = 1; j <= m; j++) {
if (L[j] && j - L[j] > 1) mp_r[ind][pll(L[j], j)] = mp_r[ind ^ 1][pll(L[j], j)] + 1;
if (R[j] <= m && R[j] - j > 1) mp_r[ind][pll(j, R[j])] = mp_r[ind ^ 1][pll(j, R[j])] + 1;
}
if (i == n) continue;
for (int j = 1; j <= m; j++) {
if (L[j]) {
for (int f : F[i + 1][j - 1]) {
if (!valid(f, i + 1, L[j], j)) continue;
if (mp_r[ind][pll(L[j], j)] >= i - f && mp_c[j - 1][pll(f, i + 1)] >= j - L[j] - 1)
add(f, i + 1, L[j], j);
}
}
if (R[j] <= m) {
for (int f : F[i + 1][j + 1]) {
if (!valid(f, i + 1, j, R[j])) continue;
if (mp_r[ind][pll(j, R[j])] >= i - f && mp_c[R[j] - 1][pll(f, i + 1)] >= R[j] - j - 1)
add(f, i + 1, j, R[j]);
}
}
}
}
sort(all(ans_vec));
ans_vec.resize(unique(all(ans_vec)) - ans_vec.begin());
return ans_vec.size();
}
# | 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... |