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 "bits/stdc++.h"
#include "rect.h"
using namespace std;
const int maxn = 2505;
struct rect {
int i, j;
int l, r;
rect (int i, int j, int l, int r) : i(i), j(j), l(l), r(r) {}
rect () {}
};
vector <pair <int, int>> ver[maxn][maxn], hor[maxn][maxn];
vector <rect> getLines(vector<std::vector<int>> a) {
int n = a.size();
int m = a.front().size();
vector <int> v[m];
vector <rect> vert;
for(int i = 0; i < n; i++) {
set <int> last;
vector <int> arr (n, -1);
for(int j = 0; j < m; j++) {
set <int> aux;
for(int k = int(v[j].size()) - 1; k >= 0; k--) {
aux.insert(v[j][k]);
if(a[v[j][k]][j] >= a[i][j]) break;
}
while(!v[j].empty() && a[v[j].back()][j] <= a[i][j]) {
v[j].pop_back();
}
v[j].push_back(i);
for(int k : last) {
if(aux.find(k) == aux.end()) {
if(k < i - 1) vert.emplace_back(k, i, arr[k], j - 1);
arr[k] = -1;
}
}
for(int k : aux) {
if(arr[k] == -1) arr[k] = j;
}
last = aux;
}
for(int k : last) {
if(k < i - 1) vert.emplace_back(k, i, arr[k], m - 1);
}
}
return vert;
}
vector <vector <int>> rotate(vector <vector <int>> a) {
int n = a.size();
int m = a.front().size();
vector <vector <int>> b (m, vector <int> (n, 0));
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[j][i];
}
}
return b;
}
/*
0 2 1 2
2 4 0 1
1 4 4 4
*/
long long solve(int i, int j) {
long long ans = 0;
for(auto x : ver[i][j]) {
for(auto y : hor[i][j]) {
if(y.first >= x.first && y.second <= x.second) ++ans;
}
}
/* for(auto x : hor[i][j]) {
cout << x.first << " " << x.second << endl;
}
cout << i << " " << j << " " << ans << endl;
*/
return ans;
}
long long count_rectangles(vector <vector <int>> a) {
auto h = getLines(a);
auto v = getLines(rotate(a));
int n = a.size();
int m = a.front().size();
for(auto &x : v) {
int aux = x.l;
x.l = max(0, x.l - 1);
x.r = min(n - 1, x.r + 1);
for(int i = x.l; i <= x.r; i++) {
ver[i][x.j].emplace_back(x.l, x.i);
// cout << x.j << " " << i << " " << x.i << " " << x.l << endl;
}
// cout << x.i << " " << x.j << " " << x.l << " " << x.r << endl;
}
for(auto &x : h) {
int aux = x.l;
x.l = max(0, x.l - 1);
x.r = min(m - 1, x.r + 1);
for(int i = x.l; i <= x.r; i++) {
hor[x.j][i].emplace_back(x.i, x.l);
}
}
long long ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
ans += solve(i, j);
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:84:9: warning: unused variable 'aux' [-Wunused-variable]
84 | int aux = x.l;
| ^~~
rect.cpp:94:9: warning: unused variable 'aux' [-Wunused-variable]
94 | int aux = x.l;
| ^~~
# | 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... |