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;
void updateMinSegTree(vector<int> &segtree, int p, int a){
int N = segtree.size() / 2;
p += N;
segtree[p] = a;
p /= 2;
while (p > 0){
segtree[p] = min(segtree[2 * p], segtree[2 * p + 1]);
p /= 2;
}
}
int queryMinSegTree(vector<int> &segtree, int l, int r){
int N = segtree.size() / 2;
int total = 2501;
for (l += N, r += N; l < r; l /= 2, r /= 2){
if (l % 2 == 1){
total = min(segtree[l++], total);
}
if (r % 2 == 1){
total = min(segtree[--r], total);
}
}
return total;
}
void updateMaxSegTree(vector<int> &segtree, int p, int a){
int N = segtree.size() / 2;
p += N;
segtree[p] = a;
p /= 2;
while (p > 0){
segtree[p] = max(segtree[2 * p], segtree[2 * p + 1]);
p /= 2;
}
}
int queryMaxSegTree(vector<int> &segtree, int l, int r){
int N = segtree.size() / 2;
int total = -1;
for (l += N, r += N; l < r; l /= 2, r /= 2){
if (l % 2 == 1){
total = max(segtree[l++], total);
}
if (r % 2 == 1){
total = max(segtree[--r], total);
}
}
return total;
}
long long count_rectangles(vector<vector<int> > a) {
int n = a.size();
int m = a[0].size();
long long total = 0;
bool all_one = true;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] > 1){
all_one = false;
break;
}
}
}
if (all_one){
vector<vector<bool> > visited (n, vector<bool> (m, false));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] == 0 && !visited[i][j]){
vector<pair<int, int> > ones;
bool bad = false;
queue<pair<int, int> > to_visit;
to_visit.push(make_pair(i, j));
visited[i][j] = true;
while(!to_visit.empty()){
pair<int, int> current = to_visit.front();
to_visit.pop();
if (current.first == 0 || current.first == n - 1 || current.second == 0 || current.second == m - 1){
bad = true;
}
int ic = current.first;
int jc = current.second;
if (ic < n - 1 && !visited[ic + 1][jc]){
if (a[ic + 1][jc] == 1){
ones.push_back(make_pair(ic + 1, jc));
} else {
visited[ic + 1][jc] = true;
to_visit.push(make_pair(ic + 1, jc));
}
}
if (ic >= 0 && !visited[ic - 1][jc]){
if (a[ic - 1][jc] == 1){
ones.push_back(make_pair(ic - 1, jc));
} else {
visited[ic - 1][jc] = true;
to_visit.push(make_pair(ic - 1, jc));
}
}
if (jc < m - 1 && !visited[ic][jc + 1]){
if (a[ic][jc + 1] == 1){
ones.push_back(make_pair(ic, jc + 1));
} else {
visited[ic][jc + 1] = true;
to_visit.push(make_pair(ic, jc + 1));
}
}
if (jc >= 0 && !visited[ic][jc - 1]){
if (a[ic][jc - 1] == 1){
ones.push_back(make_pair(ic, jc - 1));
} else {
visited[ic][jc] = true;
to_visit.push(make_pair(ic, jc - 1));
}
}
}
if (!bad){
int mini = n;
int maxi = 0;
int minj = m;
int maxj = 0;
for (int x = 0; x < ones.size(); x++){
mini = min(mini, ones[x].first);
minj = min(minj, ones[x].second);
maxi = max(maxi, ones[x].first);
maxj = max(maxj, ones[x].second);
}
for (int x = 0; x < ones.size(); x++){
if (ones[x].first != mini && ones[x].first != maxi && ones[x].second != minj && ones[x].second != maxj){
bad = true;
break;
}
}
}
if (!bad){
total++;
}
}
}
}
return total;
}
vector<vector<int> > min_left (vector<vector<int> > (n, vector<int> (m, 0)));
vector<vector<int> > min_up (vector<vector<int> > (n, vector<int> (m, 0)));
vector<vector<int> > max_right (vector<vector<int> > (n, vector<int> (m, 0)));
vector<vector<int> > max_down (vector<vector<int> > (n, vector<int> (m, 0)));
for (int i = 0; i < n; i++){
vector<int> current_row = a[i];
vector<pair<int, int> > order_to_process (m, pair<int, int>());
for (int j = 0; j < m; j++){
order_to_process[j].first = a[i][j];
order_to_process[j].second = j;
}
sort(order_to_process.begin(), order_to_process.end());
for (int x = 0; x < m; x++){
int current_value = order_to_process[x].first;
int current_index = order_to_process[x].second;
int current_right_index = current_index + 1;
int current_left_index = current_index - 1;
while (current_right_index < m){
if (a[i][current_right_index] >= current_value){
break;
} else {
current_right_index = max_right[i][current_right_index];
}
}
while (current_left_index >= 0){
if (a[i][current_left_index] >= current_value){
break;
} else {
current_left_index = min_left[i][current_left_index];
}
}
max_right[i][current_index] = current_right_index;
min_left[i][current_index] = current_left_index;
}
}
for (int j = 0; j < m; j++){
vector<int> current_column (n, 0);
for (int i = 0; i < n; i++){
current_column[i] = a[i][j];
}
vector<pair<int, int> > order_to_process (n, pair<int, int>());
for (int i = 0; i < n; i++){
order_to_process[i].first = a[i][j];
order_to_process[i].second = i;
}
sort(order_to_process.begin(), order_to_process.end());
for (int x = 0; x < n; x++){
int current_value = order_to_process[x].first;
int current_index = order_to_process[x].second;
int current_down_index = current_index + 1;
int current_up_index = current_index - 1;
while (current_down_index < n){
if (a[current_down_index][j] >= current_value){
break;
} else {
current_down_index = max_down[current_down_index][j];
}
}
while (current_up_index >= 0){
if (a[current_up_index][j] >= current_value){
break;
} else {
current_up_index = min_up[current_up_index][j];
}
}
max_down[current_index][j] = current_down_index;
min_up[current_index][j] = current_up_index;
}
}
vector<vector<int> > min_left_seg_trees (vector<vector<int> > (m, vector<int> (2 * n, 0)));
//vector<vector<int> > min_up_seg_trees (vector<vector<int> > (n, vector<int> (2 * m, 0)));
//vector<vector<int> > max_right_seg_trees (vector<vector<int> > (m, vector<int> (2 * n, 0)));
//vector<vector<int> > max_down_seg_trees (vector<vector<int> > (n, vector<int> (2 * m, 0)));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
updateMaxSegTree(min_left_seg_trees[j], i, min_left[i][j]);
//updateMinSegTree(max_right_seg_trees[j], i, max_right[i][j]);
//updateMaxSegTree(min_up_seg_trees[i], j, min_up[i][j]);
//updateMinSegTree(max_down_seg_trees[i], j, max_down[i][j]);
}
}
for (int r1 = 1; r1 < n - 1; r1++){
for (int c1 = 1; c1 < m - 1; c1++){
bool b = false;
if (b){
break;
}
int right = max_right[r1][c1 - 1];
for (int r2 = r1; r2 < n - 1; r2++){
right = min(right, max_right[r2][c1 - 1]);
int down = max_down[r1 - 1][c1];
int up = min_up[r2 + 1][c1];
for (int c2 = c1; c2 < m - 1 && c2 < right; c2++){
down = min(down, max_down[r1 - 1][c2]);
up = max(up, min_up[r2 + 1][c2]);
if (down > r2){
if (up < r1){
if (queryMaxSegTree(min_left_seg_trees[c2 + 1], r1, r2 + 1) < c1){
total++;
}
} else{
b = true;
break;
}
} else{
break;
}
}
}
}
}
return total;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:166:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for (int x = 0; x < ones.size(); x++){
| ~~^~~~~~~~~~~~~
rect.cpp:173:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for (int x = 0; x < ones.size(); x++){
| ~~^~~~~~~~~~~~~
# | 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... |