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>
typedef long long ll;
using namespace std;
ll ans = 0;
struct obdl
{
int x1, y1, x2, y2, t;
};
vector<obdl> make(vector<vector<int> > a)
{
int n = a.size(), m = a[0].size();
vector<set<pair<int, int> > > end(n);
for (int i = 0; i < n; i++)
{
vector<int> ri(m, m), li(m, -1);
vector<int> st;
for (int j = 0; j < m; j++) // najdeme najblizsi vacsi
{
while (!st.empty() && a[i][j] > a[i][st.back()]) st.pop_back();
if (!st.empty() && st.back() + 1 < j) li[j] = st.back();
st.push_back(j);
}
st.clear();
for (int j = m - 1; j >= 0; j--) // najdeme najblizsi vacsi
{
while (!st.empty() && a[i][j] > a[i][st.back()]) st.pop_back();
if (!st.empty() && st.back() - 1 > j) ri[j] = st.back();
st.push_back(j);
}
for (int j = 0; j < m; j++)
{
if (ri[j] != m && li[ri[j]] <= j) end[i].insert({ j + 1, ri[j] - 1 });
if (li[j] != -1 && ri[li[j]] >= j) end[i].insert({ li[j] + 1, j - 1 });
}
}
vector<obdl>o;
for (int i = 0; i < n; i++) for (pair<int, int> p : end[i])
{
int j = i + 1;
while (j < n && end[j].count(p)) end[j++].erase(p);
o.push_back({ i, p.first, j - 1, p.second });
}
return o;
}
const int maxn = 2505;
int st[maxn * 2];
void upd(int i, int x)
{
for (; i < maxn; i = (i | (i + 1))) st[i] += x;
}
int query(int r)
{
int ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
return ans;
}
struct udaj { int x, y, t; };
bool cmp(udaj a, udaj b)
{
if (a.x != b.x) return a.x < b.x;
return a.t > b.t;
}
ll count_rectangles(vector<vector<int> > a)
{
int n = a.size(), m = a[0].size();
vector<vector<vector<udaj> > > v(n, vector<vector<udaj> >(m));
vector<vector<vector<obdl> > > o(n, vector<vector<obdl> >(m));
vector<obdl> o1 = make(a);
vector<vector<int> > b(m, vector<int>(n));
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) b[j][i] = a[i][j];
vector<obdl> o2 = make(b);
for (obdl& i : o2) i.t = 2, swap(i.x1, i.y1), swap(i.x2, i.y2), o[i.x2][i.y2].push_back(i);
for (obdl i : o1) i.t = 1, o[i.x2][i.y2].push_back(i);
for (int x = 0; x < n; x++) for (int y = m-1; y >= 0; y--) for (obdl i : o[x][y])
{
if (i.t == 1) for (int x1 = i.x1; x1 <= i.x2; x1++) v[x1][i.y1].push_back({ x, y, 1 });
if (i.t == 2) for (int y1 = i.y1; y1 <= i.y2; y1++) v[i.x1][y1].push_back({ x, y, 2 });
}
ll ans = 0;
for (int x = 1; x + 1 < n; x++) for (int y = 1; y + 1 < m; y++)
{
ll sum = 0, all = 0;
for (udaj u : v[x][y])
{
if (u.t == 2) upd(u.y, 1), all++;
else sum += all - query(u.y - 1);
}
for (udaj u : v[x][y]) if (u.t == 2) upd(u.y, -1);
ans += sum;
//cout << sum << " \n"[y == m - 2];
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'int query(int)':
rect.cpp:58:1: warning: control reaches end of non-void function [-Wreturn-type]
58 | }
| ^
# | 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... |