#include "rect.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
using vlld = vector<lld>;
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
#define fore(i, l, r) for (auto i = l; i < r; i++)
#define fo(i, n) fore (i, 0, n)
#define forex(i, r, l) for (auto i = r-1; i >= l; i--)
#define ffo(i, n) forex (i, n, 0)
bool cmin(ll &a, ll b) { if (b < a) { a=b; return 1; } return 0; }
bool cmax(ll &a, ll b) { if (b > a) { a=b; return 1; } return 0; }
const ll INF = 1e18;
const int mod = 1e9 + 7, LOG = 20;
const int N = 2505;
ll mp[N][N], vis[N][N];
ll count_rectangles(vector<vector<int> > a) {
ll n = a.size(), m = a[0].size(), ans = 0;
fo (i, n) {
fo (j, m) {
mp[i][j] = vis[i][j] = 0;
}
}
vector<vll> down(n, vll(m)), up(n, vll(m));
fo (j, m) {
stack<ll> stk;
stk.push(-1);
fo (i, n) {
while (stk.top() != -1 && a[stk.top()][j] < a[i][j]) stk.pop();
up[i][j] = stk.top();
stk.push(i);
}
while (stk.size()) stk.pop();
stk.push(n);
ffo (i, n) {
while (stk.top() != n && a[stk.top()][j] < a[i][j]) stk.pop();
down[i][j] = stk.top();
stk.push(i);
}
}
vector<array<ll, 2>> pr;
forex (i, n-1, 1) {
vll L(m), R(m);
stack<ll> stk;
stk.push(-1);
fo (j, m) {
while (stk.top() != -1 && a[i][stk.top()] <= a[i][j]) stk.pop();
L[j] = stk.top() + 1;
stk.push(j);
}
while (stk.size()) stk.pop();
stk.push(m);
ffo (j, m) {
while (stk.top() != m && a[i][stk.top()] < a[i][j]) stk.pop();
R[j] = stk.top() - 1;
stk.push(j);
}
fo (j, m) {
if (L[j] < 1 || m-2 < R[j] || a[i][R[j]+1] == a[i][j]) continue;
vis[L[j]][R[j]] = i;
if (mp[L[j]][R[j]] == 0) mp[L[j]][R[j]] = i;
ll lim = mp[L[j]][R[j]] + 1;
fore (j2, L[j], R[j]+1) lim = min(lim, down[i-1][j2]);
fore (i2, i+1, lim+1) {
ll mx = 0;
fore (j2, L[j], R[j]+1) mx = max(mx, up[i2][j2]);
if (mx >= i) continue;
ans++;
}
}
for (auto [l, r]: pr) {
if (vis[l][r] == i) continue;
mp[l][r] = 0;
}
pr.clear();
fo (j, m) {
if (L[j] < 1 || m-2 < R[j]) continue;
pr.pb({L[j], R[j]});
}
}
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... |