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;
const int MAXN = 2507;
const int inf = 1e9;
struct b_str{
int l, r, u, d;
b_str(){}
};
int a[MAXN][MAXN], n, m;
b_str b[MAXN][MAXN];
struct segment_tree{
int mn[4 * MAXN];
char type;
int val;
segment_tree(){}
void init(int idx, int l, int r){
if(l == r){
switch(type){
case 'l':
mn[idx] = b[l][val].l;
break;
case 'r':
mn[idx] = b[l][val].r;
break;
case 'u':
mn[idx] = b[val][l].u;
break;
case 'd':
mn[idx] = b[val][l].d;
break;
}
return;
}
int mid = (l + r) >> 1;
init(2 * idx, l, mid);
init(2 * idx + 1, mid + 1, r);
mn[idx] = min(mn[2 * idx], mn[2 * idx + 1]);
}
void do_init(char type, int val){
this->type = type;
this->val = val;
init(1, 0, MAXN - 1);
}
int query(int idx, int l, int r, int sl, int sr){
if(l > sr || r < sl){
return inf;
}
if(sl <= l && r <= sr){
return mn[idx];
}
int mid = (l + r) >> 1;
int lvalue = query(2 * idx, l, mid, sl, sr);
int rvalue = query(2 * idx + 1, mid + 1, r, sl, sr);
return min(lvalue, rvalue);
}
};
struct rect{
int x1, y1, x2, y2;
rect(){}
friend bool operator<(rect lvalue, rect rvalue){
if(lvalue.x1 != rvalue.x1){
return lvalue.x1 < rvalue.x1;
}
if(lvalue.x2 != rvalue.x2){
return lvalue.x2 < rvalue.x2;
}
if(lvalue.y1 != rvalue.y1){
return lvalue.y1 < rvalue.y1;
}
return lvalue.y2 < rvalue.y2;
}
};
map<rect, bool> mp;
segment_tree st_l[MAXN], st_r[MAXN], st_u[MAXN], st_d[MAXN];
bool check(int x1, int y1, int x2, int y2){
//cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
int lvalue = st_l[y2 + 1].query(1, 0, MAXN - 1, x1, x2);
int rvalue = st_r[y1 - 1].query(1, 0, MAXN - 1, x1, x2);
int uvalue = st_u[x2 + 1].query(1, 0, MAXN - 1, y1, y2);
int dvalue = st_d[x1 - 1].query(1, 0, MAXN - 1, y1, y2);
if(min(lvalue, rvalue) < y2 - y1 + 2){
return false;
}
if(min(uvalue, dvalue) < x2 - x1 + 2){
return false;
}
return true;
}
long long count_rectangles(vector<vector<int> > _a) {
n = _a.size();
m = _a[0].size();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
a[i][j] = _a[i][j];
}
}
for(int i = 0; i < n; ++i){
stack<int> st;
st.push(0);
for(int j = 1; j < m; ++j){
while(!st.empty() && a[i][j] >= a[i][st.top()]){
b[i][st.top()].r = j - st.top();
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[i][st.top()].r = m - st.top();
st.pop();
}
}
for(int i = 0; i < n; ++i){
stack<int> st;
st.push(m - 1);
for(int j = m - 2 ; j >= 0; --j){
while(!st.empty() && a[i][j] >= a[i][st.top()]){
b[i][st.top()].l = st.top() - j;
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[i][st.top()].l = st.top() + 1;
st.pop();
}
}
for(int i = 0; i < m; ++i){
stack<int> st;
st.push(0);
for(int j = 1; j < n; ++j){
while(!st.empty() && a[j][i] >= a[st.top()][i]){
b[st.top()][i].d = j - st.top();
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[st.top()][i].d = n - st.top();
st.pop();
}
}
for(int i = 0; i < m; ++i){
stack<int> st;
st.push(n - 1);
for(int j = n - 2; j >= 0; --j){
while(!st.empty() && a[j][i] >= a[st.top()][i]){
b[st.top()][i].u = st.top() - j;
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[st.top()][i].u = st.top() + 1;
st.pop();
}
}
for(int i = 0; i < n; ++i){
st_u[i].do_init('u', i);
st_d[i].do_init('d', i);
}
for(int i = 0; i < m; ++i){
st_l[i].do_init('l', i);
st_r[i].do_init('r', i);
}
for(int i = 0; i < n; ++i){
stack<int> st;
st.push(0);
for(int j = 1; j < m; ++j){
while(!st.empty() && a[i][j] > a[i][st.top()]){
b[i][st.top()].r = j - st.top();
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[i][st.top()].r = m - st.top();
st.pop();
}
}
for(int i = 0; i < n; ++i){
stack<int> st;
st.push(m - 1);
for(int j = m - 2 ; j >= 0; --j){
while(!st.empty() && a[i][j] > a[i][st.top()]){
b[i][st.top()].l = st.top() - j;
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[i][st.top()].l = st.top() + 1;
st.pop();
}
}
for(int i = 0; i < m; ++i){
stack<int> st;
st.push(0);
for(int j = 1; j < n; ++j){
while(!st.empty() && a[j][i] > a[st.top()][i]){
b[st.top()][i].d = j - st.top();
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[st.top()][i].d = n - st.top();
st.pop();
}
}
for(int i = 0; i < m; ++i){
stack<int> st;
st.push(n - 1);
for(int j = n - 2; j >= 0; --j){
while(!st.empty() && a[j][i] > a[st.top()][i]){
b[st.top()][i].u = st.top() - j;
st.pop();
}
st.push(j);
}
while(!st.empty()){
b[st.top()][i].u = st.top() + 1;
st.pop();
}
}
long long ans = 0;
for(int i = 1; i < n - 1; ++i){
for(int j = 1; j < m - 1; ++j){
rect r;
r.x1 = min(n - 2, max(1, i - b[i][j].u + 1));
r.x2 = min(n - 2, max(1, i + b[i][j].d - 1));
r.y1 = min(m - 2, max(1, j - b[i][j].l + 1));
r.y2 = min(m - 2, max(1, j + b[i][j].r - 1));
//cout << i << " " << j << " - " << r.x1 << " " << r.y1 << " " << r.x2 << " " << r.y2 << endl;
mp[r] = true;
}
}
for(auto it: mp){
rect r = it.first;
ans += check(r.x1, r.y1, r.x2, r.y2);
}
return ans;
}
/*
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6
*/
| # | 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... |