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;
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]);
}
}
vector<vector<vector<int> > > min_left_precomputed(m, vector<vector<int> > (n, vector<int>()));
for (int j = 0; j < m; j++){
for (int i1 = 0; i1 < n; i1++){
min_left_precomputed[j][i1].push_back(min_left[i1][j]);
for (int d = 1; i1 + d < n; d++){
min_left_precomputed[j][i1].push_back(max(min_left[i1 + d][j], min_left_precomputed[j][i1][d - 1]));
}
}
}
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 (min_left_precomputed[c2 + 1][r1][r2 - r1] < c1){
total++;
}
} else{
b = true;
break;
}
} else{
break;
}
}
}
}
}
return total;
}
# | 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... |