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;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> inter;
const int len = 2505;
int po[len];
inter row_inter[len], col_inter[len];
void print(inter now){
for (int i = 0; i < now.size(); i++)
printf(" (%d, %d)\n", now[i].fi, now[i].se);
printf("\n");
}
inter make_inter(vector<int> vec){
inter res;
vector<int> stck;
for (int i = 0; i < vec.size(); i++){
while (!stck.empty() && vec[i] > vec[stck.back()])
stck.pop_back();
if (!stck.empty() && stck.back() < i-1)
res.pb(mp(stck.back()+1, i-1));
stck.pb(i);
}
stck.clear();
for (int i = (int)vec.size()-1; i >= 0; i--){
while (!stck.empty() && vec[i] > vec[stck.back()])
stck.pop_back();
if (!stck.empty() && stck.back() > i+1)
res.pb(mp(i+1, stck.back()-1));
stck.pb(i);
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
inter join(inter &fir, inter &sec){
inter res;
for (int i = 0, j = 0; i < fir.size(); i++){
while (j < sec.size() && sec[j] < fir[i])
j++;
if (j < sec.size() && sec[j] == fir[i])
res.pb(fir[i]);
}
return res;
}
ll count_rectangles(vector<vector<int> > mat) {
int n = mat.size(), m = mat[0].size();
//printf("n = %d, m = %d\n", n, m);
for (int i = 0; i < n; i++){
vector<int> vec;
for (int j = 0; j < m; j++)
vec.pb(mat[i][j]);
row_inter[i] = make_inter(vec);
//printf("row = %d\n", i);
//print(row_inter[i]);
}
for (int j = 0; j < m; j++){
vector<int> vec;
for (int i = 0; i < n; i++)
vec.pb(mat[i][j]);
col_inter[j] = make_inter(vec);
//printf("col = %d\n", j);
//print(col_inter[j]);
}
ll ans = 0;
for (int st_row = 0; st_row < n; st_row++){
inter cur = row_inter[st_row];
for (int j = 0; j < m; j++)
po[j] = 0;
for (int en_row = st_row; en_row < n; en_row++){
vector<int> can(m, 0);
for (int j = 0; j < m; j++){
while (po[j] < col_inter[j].size() && col_inter[j][po[j]] < mp(st_row, en_row))
po[j]++;
if (po[j] < col_inter[j].size() && col_inter[j][po[j]] == mp(st_row, en_row))
can[j] = 1;
}
for (int j = 1; j < m; j++)
can[j] += can[j-1];
if (en_row != st_row)
cur = join(cur, row_inter[en_row]);
// upd ans
for (auto in: cur){
if (can[in.se]-can[in.fi-1] == in.se-in.fi+1)
ans++; //, printf("st = %d, en = %d, in = (%d, %d)\n", st_row, en_row, in.fi, in.se);
}
}
}
return ans;
}
/*
10 10
3 2 2 2 5 1 1 1 1 1
3 1 1 1 2 1 1 1 1 1
6 2 2 2 3 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
*/
Compilation message (stderr)
rect.cpp: In function 'void print(inter)':
rect.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int i = 0; i < now.size(); i++)
| ~~^~~~~~~~~~~~
rect.cpp: In function 'inter make_inter(std::vector<int>)':
rect.cpp:28:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
rect.cpp: In function 'inter join(inter&, inter&)':
rect.cpp:57:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 0, j = 0; i < fir.size(); i++){
| ~~^~~~~~~~~~~~
rect.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while (j < sec.size() && sec[j] < fir[i])
| ~~^~~~~~~~~~~~
rect.cpp:61:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if (j < sec.size() && sec[j] == fir[i])
| ~~^~~~~~~~~~~~
rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:100:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | while (po[j] < col_inter[j].size() && col_inter[j][po[j]] < mp(st_row, en_row))
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
rect.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (po[j] < col_inter[j].size() && col_inter[j][po[j]] == mp(st_row, en_row))
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |