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>
#define ll long long
#define st first
#define nd second
#define mp make_pair
using namespace std;
using vec2d = vector<vector<pair<int, int>>>;
int n, m;
struct Segment {
int l, r, h;
};
ll count_rectangles(vector<vector<int>> a) {
n = a.size();
m = a[0].size();
vector<vec2d> horiz(n, vec2d(m));
for(int i=0; i<n; ++i) {
vector<int> mono;
for(int j=0; j<m; ++j) {
while(mono.size() && a[i][mono.back()] < a[i][j]) {
if(mono.back()+1 <= j-1) {
horiz[i][mono.back()+1].push_back(mp(j-1, 1));
}
mono.pop_back();
}
if(mono.size() && mono.back()+1 <= j-1) {
horiz[i][mono.back()+1].push_back(mp(j-1, 1));
}
if(mono.size() && a[i][mono.back()] == a[i][j]) {
mono.pop_back();
}
mono.push_back(j);
}
if(i!=0) {
for(int j=0; j<m; ++j) {
int k = 0;
for(auto &it: horiz[i][j]) {
while(k<(int)horiz[i-1][j].size() && horiz[i-1][j][k].st < it.st) {
k++;
}
if(k<(int)horiz[i-1][j].size() && horiz[i-1][j][k].st == it.st) {
it.nd = horiz[i-1][j][k].nd + 1;
}
}
}
}
}
vector<vector<Segment>> segh(m);
for(int i=0; i<n; ++i) {
if(i < n-1) {
for(int j=0; j<m; ++j) {
int k = 0;
for(auto it: horiz[i][j]) {
while(k<(int)horiz[i+1][j].size() && horiz[i+1][j][k].st < it.st) {
k++;
}
if(k==(int)horiz[i+1][j].size() || horiz[i+1][j][k].st != it.st) {
segh[j].push_back({i-it.nd+1, i, it.st-j+1});
}
}
}
}
else {
for(int j=0; j<m; ++j) {
for(auto it: horiz[i][j]) {
segh[j].push_back({i-it.nd+1, i, it.st-j+1});
}
}
}
}
/// **************************************
/// *** horizontal segments calculated ***
/// **************************************
vector<vec2d> vert(m, vec2d(n));
for(int j=m-1; j>=0; --j) {
vector<int> mono;
for(int i=0; i<n; ++i) {
while(mono.size() && a[mono.back()][j] < a[i][j]) {
if(mono.back()+1 <= i-1) {
vert[j][mono.back()+1].push_back(mp(i-1, 1));
}
mono.pop_back();
}
if(mono.size() && mono.back()+1 <= i-1) {
vert[j][mono.back()+1].push_back(mp(i-1, 1));
}
if(mono.size() && a[mono.back()][j] == a[i][j]) {
mono.pop_back();
}
mono.push_back(i);
}
if(j < m-1) {
for(int i=0; i<n; ++i) {
int k = 0;
for(auto &it: vert[j][i]) {
while(k<(int)vert[j+1][i].size() && vert[j+1][i][k].st < it.st) {
k++;
}
if(k<(int)vert[j+1][i].size() && vert[j+1][i][k].st == it.st) {
it.nd = vert[j+1][i][k].nd + 1;
}
}
}
}
}
vector<vector<Segment>> segv(m);
for(int j=m-1; j>=0; --j) {
/*if(j > 0) {
for(int i=0; i<n; ++i) {
int k = 0;
for(auto it: vert[j][i]) {
while(k<(int)vert[j-1][i].size() && vert[j-1][i][k].st < it.st) {
k++;
}
if(k==(int)vert[j-1][i].size() || vert[j-1][i][k].st != it.st) {
segv[j].push_back({i, it.st, it.nd});
}
}
}
}*/
for(int i=0; i<n; ++i) {
for(auto it: vert[j][i]) {
segv[j].push_back({i, it.st, it.nd});
}
}
}
ll ans = 0;
for(int col=0; col<m; ++col) {
//cerr<<"col: "<<col<<'\n';
for(auto it: segh[col]) {
//cerr<<it.l<<' '<<it.r<<' '<<it.h<<'\n';
int cnt = 0;
for(auto i: segv[col]) {
if(i.h >= it.h && i.l >= it.l && i.r <= it.r) {
cnt++;
}
}
ans += cnt;
}
}
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... |