#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 N = 2505, LOG = 12;
struct SparseTable{
vector<vector<int>> sp;
int n;
SparseTable (int n): n(n) { sp = vector(n+1, vector<int>(LOG, -1e9)); }
SparseTable (int n, vector<int> &a): n(n) {
sp = vector(n+1, vector<int>(LOG, -1e9));
fo (i, n) sp[i][0] = a[i];
}
void build ( ) {
fore (b, 1, LOG) {
fo (i, n-(1<<b)+1) {
sp[i][b] = max (sp[i][b-1], sp[i + (1<<(b-1))][b-1]);
}
}
}
int query (int l, int r) {
int lg = log2(r - l + 1);
return max (sp[l][lg], sp[r - (1<<lg)+1][lg]);
}
};
int qans[N*N*3], L[N][N], R[N][N], pr[N][N], prc[N][N], down[N][N], up[N][N], ft[N];
vector<array<int, 4>> off[N], allt;
vector<array<int, 3>> updates, qrys;
int idx;
void update (int i, int v) {
i++;
for (; i < N; i += lsb(i))
ft[i] += v;
}
int query (int i) {
i++;
int r = 0;
for (; i > 0; i -= lsb(i))
r += ft[i];
return r;
}
int query (int l, int r) { if (l>r) return 0ll; return query(r) - query(l-1); }
ll count_rectangles(vector<vector<int> > a) {
int n = a.size(), m = a[0].size();
ll ans = 0;
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() * -1;
stk.push(i);
}
}
forex (i, n-1, 1) {
stack<ll> stk;
stk.push(-1);
fo (j, m) {
while (stk.top() != -1 && a[i][stk.top()] <= a[i][j]) stk.pop();
L[i][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[i][j] = stk.top() - 1;
stk.push(j);
}
fo (j, m) {
if (L[i][j] < 1 || m-2 < R[i][j] || a[i][R[i][j]+1] == a[i][j]) continue;
if (pr[L[i][j]][R[i][j]] == 0 || pr[L[i][j]][R[i][j]] > i+1) {
off[i+1].pb({L[i][j], R[i][j], 1, idx});
idx++;
}
off[i].pb({L[i][j], R[i][j], 1, idx});
idx++;
off[i-1].pb({L[i][j], R[i][j], 0, idx});
idx++;
pr[L[i][j]][R[i][j]] = i;
}
}
SparseTable mn(m), mx(m);
fo (i, n) {
fo (j, m) {
mn.sp[j][0] = down[i][j];
mx.sp[j][0] = up[i][j];
}
mn.build();
mx.build();
for (auto e: off[i]) {
qans[e[3]] = (e[2] == 1 ? mx.query(e[0], e[1]) : mn.query(e[0], e[1])*-1);
}
}
idx = 0;
fo (i, m) {
fo (j, m) {
pr[i][j] = 0;
}
}
forex (i, n-1, 1) {
fo (j, m) {
if (L[i][j] < 1 || m-2 < R[i][j] || a[i][R[i][j]+1] == a[i][j]) continue;
if (pr[L[i][j]][R[i][j]] == 0 || pr[L[i][j]][R[i][j]] > i+1) {
if (qans[idx] < i) {
allt.pb(array<int, 4>{L[i][j]*m+R[i][j], qans[idx]+1, -1, int(i+1)});
allt.pb(array<int, 4>{L[i][j]*m+R[i][j], int(i+1), +1, int(i+1)});
}
idx++;
}
if (pr[L[i][j]][R[i][j]] > i+1 || pr[L[i][j]][R[i][j]] == 0)
prc[L[i][j]][R[i][j]] = i;
if (qans[idx]+1 < i) {
allt.pb(array<int, 4>{L[i][j]*m+R[i][j], qans[idx]+1, -1, int(i)});
allt.pb(array<int, 4>{L[i][j]*m+R[i][j], int(i), +1, int(i)});
}
idx++;
int k = min(qans[idx], prc[L[i][j]][R[i][j]]+1);
if (k > i)
allt.pb(array<int, 4>{L[i][j]*m+R[i][j], int(i+1), k, -1000000000});
idx++;
pr[L[i][j]][R[i][j]] = i;
}
}
sort(all(allt));
// return 0;
// for (auto q: allt) {
// cout << q[0] << ' ' << q[1] << ' ' << q[2] << ' ' << q[3] << '\n';
// }
fo (_, allt.size()) {
ll __ = _;
while (__ < allt.size() && allt[_][0] == allt[__][0]) __++;
// cout << "Range " << l << ' ' << r << '\n';
qrys.clear();
fore (___, _, __) {
if (allt[___][3] == -1e9) qrys.pb({allt[___][1], allt[___][2], allt[___][3]});
else updates.pb({allt[___][1], allt[___][2], allt[___][3]});
}
reverse(all(qrys));
// cout << qrys.size() << '\n';
for (auto [lb, rb, ______]: qrys) {
while (updates.size() && updates.back()[0] >= lb) {
// cout << "Make " << updates.back()[0] << ' ' << updates.back()[1] << ' ' << updates.back()[2] << '\n';
update(updates.back()[2], updates.back()[1]);
updates.pop_back();
}
// cout << "Row " << i << ' ' << lb << ' ' << rb << ' ' << ls << '\n';
// cout << query(lb, min(rb, ls+1)) << '\n';
ans += query(lb, rb);
}
while (updates.size()) {
update(updates.back()[2], updates.back()[1]);
updates.pop_back();
}
_ = __-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... |