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;
typedef long long ll;
const int MAXN = 2505;
int rows, cols;
vector<int> lr[MAXN][MAXN], tb[MAXN][MAXN];
vector<pair<int, int>> lrc[MAXN][MAXN], tbc[MAXN][MAXN];
ll fw[MAXN];
void update(int x, ll v){
while (x < cols){
fw[x] += v;
x += (x & (-x));
}
}
void rupdate(int x, int y, ll v){
update(x, v); update(y + 1, -v);
}
ll query(int x){
ll ans = 0;
while (x){
ans += fw[x];
x -= (x & (-x));
}
return ans;
}
ll count_rectangles(vector<vector<int>> a) {
rows = a.size(), cols = a[0].size();
for (int i = 0; i < rows; i++){
stack<int> s;
for (int j = 0; j < cols; j++){
while (!s.empty() && a[i][j] > a[i][s.top()]){
if (s.top() + 1 != j) lr[s.top()][j].push_back(i);
s.pop();
}
if (!s.empty() && s.top() + 1 != j) lr[s.top()][j].push_back(i);
s.push(j);
}
}
for (int i = 0; i < cols; i++)
for (int j = i + 2; j < cols; j++){
int sz = lr[i][j].size();
for (int s = 0; s < sz; s++){
int e = s;
while (e != sz - 1 && lr[i][j][e + 1] == lr[i][j][e] + 1) e++;
for (int k = s; k <= e; k++) lrc[lr[i][j][k]][i + 1].push_back({lr[i][j][e], j - 1});
s = e;
}
}
for (int j = 0; j < cols; j++){
stack<int> s;
for (int i = 0; i < rows; i++){
while (!s.empty() && a[i][j] > a[s.top()][j]){
if (s.top() + 1 != i) tb[s.top()][i].push_back(j);
s.pop();
}
if (!s.empty() && s.top() + 1 != i) tb[s.top()][i].push_back(j);
s.push(i);
}
}
for (int i = 0; i < rows; i++)
for (int j = i + 2; j < rows; j++){
int sz = tb[i][j].size();
for (int s = 0; s < sz; s++){
int e = s;
while (e != sz - 1 && tb[i][j][e + 1] == tb[i][j][e] + 1) e++;
for (int k = s; k <= e; k++) tbc[i + 1][tb[i][j][k]].push_back({j - 1, tb[i][j][e]});
s = e;
}
}
ll ans = 0;
for (int i = 1; i < rows - 1; i++)
for (int j = 1; j < cols - 1; j++){
sort(lrc[i][j].begin(), lrc[i][j].end());
sort(tbc[i][j].begin(), tbc[i][j].end());
int ind = 0;
for (auto p : lrc[i][j]){
int r, c; tie(r, c) = p;
while (ind != tbc[i][j].size() && tbc[i][j][ind].first <= r){
rupdate(1, tbc[i][j][ind].second, 1);
ind++;
}
ans += query(c);
}
for (int k = 0; k < ind; k++) rupdate(1, tbc[i][j][k].second, -1);
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:83:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | while (ind != tbc[i][j].size() && tbc[i][j][ind].first <= r){
| ~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |