#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 5e3;
#define endl "\n"
#define sp <<" "<<
#define REP(i, a, b) for(ll i = a; i < b; i++)
#define mp make_pair
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define all(x) (x).begin(), (x).end()
struct stuff {
short coll, colr, rowl, rowr;
stuff(short coll = -INF, short colr = INF, short rowl = -INF, short rowr = INF) : coll(coll), colr(colr), rowl(rowl), rowr(rowr) {;;}
// use static for ref
static stuff merge(const stuff &a, const stuff &b) {
return stuff{min(a.coll, b.coll), max(a.colr, b.colr), min(a.rowl, b.rowl), max(a.rowr, b.rowr)};
}
};
const stuff nothing = stuff{INF, -INF, INF, -INF};
struct twotree {
int n, m;
vector<vector<stuff>> tree;
const vector<vector<stuff>> &base; // avoid copies
twotree(const vector<vector<stuff>> &base) : n(base.size()), m(base[0].size()), tree(4*n, vector<stuff>(4*m)), base(base) {
buildX(1, 0, n-1);
}
void buildY(int x, int l, int r, int y, int ll, int rr) {
if (ll == rr) {
if (l == r) {
tree[x][y] = base[l][ll];
} else {
tree[x][y] = stuff::merge(tree[x*2][y], tree[x*2+1][y]);
}
} else {
int m = (ll+rr)/2;
buildY(x, l, r, y*2, ll, m);
buildY(x, l, r, y*2+1, m+1, rr);
tree[x][y] = stuff::merge(tree[x][y*2], tree[x][y*2+1]);
}
}
void buildX(int x, int l, int r) {
if (l != r) {
int m = (l+r)/2;
buildX(x*2, l, m);
buildX(x*2+1, m+1, r);
}
buildY(x, l, r, 1, 0, m-1);
}
stuff queryY(int x, int y, int ll, int rr, int qll, int qrr) {
if (qrr < ll or qll > rr) {
return nothing;
}
if (qll <= ll and rr <= qrr) {
return tree[x][y];
}
int m = (ll+rr)/2;
return stuff::merge(queryY(x, y*2, ll, m, qll, qrr), queryY(x, y*2+1, m+1, rr, qll, qrr));
}
stuff queryX(int x, int l, int r, int ql, int qr, int qll, int qrr) {
if (qr < l or ql > r) {
return nothing;
}
if (ql <= l and r <= qr) {
return queryY(x, 1, 0, m-1, qll, qrr);
}
int m = (l+r)/2;
return stuff::merge(queryX(x*2, l, m, ql, qr, qll, qrr), queryX(x*2+1, m+1, r, ql, qr, qll, qrr));
}
stuff query(int x, int y, int xx, int yy) {
return queryX(1, 0, n-1, x, xx, y, yy);
}
};
ll count_rectangles(vector<vector<int>> a) {
short n = a.size(), m = a[0].size();
vector<vector<stuff>> base(n, vector<stuff>(m));
ll ans = 0;
stack<short> st;
// O(nm)
for (short i = 0; i < n; i++) {
while (!st.empty()) st.pop();
for (short j = 0; j < m; j++) {
while (!st.empty() and a[i][st.top()] <= a[i][j]) {
st.pop();
}
if (!st.empty()) {
base[i][j].rowl = st.top();
}
st.push(j);
}
while (!st.empty()) st.pop();
for (short j = m-1; j >= 0; j--) {
while (!st.empty() and a[i][st.top()] <= a[i][j]) {
st.pop();
}
if (!st.empty()) {
base[i][j].rowr = st.top();
}
st.push(j);
}
}
for (short i = 0; i < m ; i++) {
while (!st.empty()) st.pop();
for (short j = 0; j < n; j++) {
while (!st.empty() and a[st.top()][i] <= a[j][i]) {
st.pop();
}
if (!st.empty()) {
base[j][i].coll = st.top();
}
st.push(j);
}
while (!st.empty()) st.pop();
for (short j = n-1; j >= 0; j--) {
while (!st.empty() and a[st.top()][i] <= a[j][i]) {
st.pop();
}
if (!st.empty()) {
base[j][i].colr = st.top();
}
st.push(j);
}
}
twotree tree(base);
auto in = [&](short x, short y) -> bool {
return (0 < x and x < n-1 and 0 < y and y < m-1);
};
auto encode = [&](ll x, ll y, ll xx, ll yy) -> ll {
return ((x<<48)|(y<<32)|(xx<<16)|yy);
};
// O(1)
unordered_set<ll> vis;
auto checkRect = [&](short x, short y, short xx, short yy) -> int {
if (!in(x,y) or !in(xx, yy)) return 0;
if (x > xx) swap(x, xx);
if (y > yy) swap(y, yy);
ll code = encode(x, y, xx, yy);
if (vis.count(code)) return 0;
vis.insert(code);
stuff q = tree.query(x, y, xx, yy);
if (q.coll < x-1 or q.colr > xx+1 or q.rowl < y-1 or q.rowr > yy+1) {
return 0;
}
return 1;
};
// O(nm)
for (short i = 1; i < n - 1; i++) {
for (short j = 1; j < m - 1; j++) {
stuff q = base[i][j];
if (q.coll == -INF or q.colr == INF or q.rowl == -INF or q.rowr == INF) {
continue;
}
ans += checkRect(q.coll+1, q.rowl+1, q.colr-1, q.rowr-1);
}
}
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... |