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;
struct bit {
vector<int> tree;
bit(int &n) {
tree.assign(n + 1, 0);
}
void update(int &n, int &x) {
for (int i = n + 1; i < tree.size(); i += (i & -i)) tree[i] += x;
}
int query(int n) {
int res = 0;
for (int i = n + 1; i > 0; i -= (i & -i)) res += tree[i];
return res;
}
};
void get_interval(vector<int> &a, vector<int> &L, vector<int> &R) {
int n = a.size(); vector<int> s; s.reserve(n);
for (int i = 0; i < n; i++) L[i] = -1, R[i] = -1;
for (int i = 0; i < n; i++) { //find biggest on left side
while (s.size() && a[s.back()] < a[i]) s.pop_back();
if (s.size()) L[i] = s.back();
s.push_back(i);
}
s.clear();
for (int i = n - 1; i >= 0; i--) { //find biggest on right side
while (s.size() && a[s.back()] < a[i]) s.pop_back();
if (s.size()) R[i] = s.back();
s.push_back(i);
}
}
long long count_rectangles(vector<vector<int>> a) {
int n = a.size(), m = a[0].size();
vector<vector<vector<int>>> right(n, vector<vector<int>>(m)), down(n, vector<vector<int>>(m));
vector<int> b(m), L(m), R(m);
for (int i = 1; i < n - 1; i++) { //get valid pairs horizontally and vertically
for (int j = 0; j < m; j++) b[j] = a[i][j];
get_interval(b, L, R);
for (int j = 0; j < m; j++) {
if (L[j] != -1 && L[j] + 1 <= j - 1) right[i][L[j] + 1].push_back(j - 1);
if (R[j] != -1 && j + 1 <= R[j] - 1 && L[R[j]] != j) right[i][j + 1].push_back(R[j] - 1); //prevent double counting, when a[i][j1] = a[i][j2]
}
}
b.resize(n), L.resize(n), R.resize(n);
for (int j = 1; j < m - 1; j++) {
for (int i = 0; i < n; i++) b[i] = a[i][j];
get_interval(b, L, R);
for (int i = 0; i < n; i++) {
if (L[i] != -1 && L[i] + 1 <= i - 1) down[L[i] + 1][j].push_back(i - 1);
if (R[i] != -1 && i + 1 <= R[i] - 1 && L[R[i]] != i) down[i + 1][j].push_back(R[i] - 1); //prevent double counting
}
}
vector<vector<int>> last_right(m, vector<int>(m, -1)), span_right(m, vector<int>(m, 0)), last_down(n, vector<int>(n, -1)), span_down(n, vector<int>(n, 0));
vector<pair<pair<int, int>, int>> update;
long long res = 0;
bit cnt(m);
for (int i = n - 1; i >= 0; i--) {
for (int j = m - 1; j >= 0; j--) {
for (auto &k : right[i][j]) {
if (last_right[j][k] != i + 1) span_right[j][k] = 0;
span_right[j][k]++;
last_right[j][k] = i;
}
for (auto &k : down[i][j]) {
if (last_down[i][k] != j + 1) span_down[i][k] = 0;
span_down[i][k]++;
last_down[i][k] = j;
}
update.clear();
for (auto &k : right[i][j]) {
update.push_back({{i, k}, 1}); //adding a rectangle
update.push_back({{i + span_right[j][k], k}, -1}); //erasing a rectangle
}
sort(update.begin(), update.end());
sort(down[i][j].begin(), down[i][j].end());
int p = 0; //check if a rectangle exists if valid_down and valid_right shares a cell in the top left corner
for (auto &k : down[i][j]) { //also count the rectangle's spanning, not just width / height 1
while (p < update.size() && update[p].first.first <= k) { //updating cnt
cnt.update(update[p].first.second, update[p].second); p++;
}
res += cnt.query(j + span_down[i][k] - 1);
}
while (p < update.size()) { //restoring cnt to default
cnt.update(update[p].first.second, update[p].second); p++;
}
}
}
return res;
}
Compilation message (stderr)
rect.cpp: In member function 'void bit::update(int&, int&)':
rect.cpp:13:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = n + 1; i < tree.size(); i += (i & -i)) tree[i] += x;
~~^~~~~~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:85:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (p < update.size() && update[p].first.first <= k) { //updating cnt
~~^~~~~~~~~~~~~~~
rect.cpp:90:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (p < update.size()) { //restoring cnt to default
~~^~~~~~~~~~~~~~~
# | 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... |