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 in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
// #define int long long
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
const int mxn = 2510;
int n, m, a[mxn][mxn], mx[mxn], fn[mxn];
vector<pair<int, int>> g[mxn];
void mnn(int &a, int b) {
if(a > b) a = b;
}
void mxx(int &a, int b) {
if(a < b) a = b;
}
int get(int i) {
int ret = 0;
for(; i > 0; i -= (i & -i)) {
ret += fn[i];
}
return ret;
}
void add(int i, int x) {
for(; i <= m; i += (i & -i)) {
fn[i] += x;
}
}
long long count_rectangles(vector<vector<int>> A) {
n = A.size(), m = A[0].size();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
a[i][j] = A[i - 1][j - 1];
}
}
for(int i = 1; i <= n; i++) {
stack<pair<int, int>> s;
for(int j = 1; j <= m; j++) {
while(s.size() && s.top().ff < a[i][j]) {
if(s.top().ss + 1 < j) g[i].eb(s.top().ss, j);
s.pop();
}
if(s.size()) {
if(s.top().ss + 1 < j) g[i].eb(s.top().ss, j);
if(s.top().ff == a[i][j]) {
s.pop();
}
}
s.push({a[i][j], j});
}
sort(all(g[i]));
}
long long ans = 0;
for(int l = 2; l < n; l++) {
for(int j = 1; j <= m; j++) {
mx[j] = 0;
fn[j] = 0;
}
vector<pair<int, int>> v;
for(int r = l; r <= n; r++) {
for(int j = 1; j <= m; j++) {
if(mx[j] >= a[r][j]) add(j, 1);
}
if(r == l) {
v = g[r];
} else {
for(auto it : v) {
ans += (get(it.ss - 1) - get(it.ff) == 0);
}
vector<pair<int, int>> tmp;
int id1 = 0, id2 = 0;
while(id1 < v.size() && id2 < g[r].size()) {
if(v[id1] == g[r][id2]) {
tmp.pb(v[id1]);
id1++;
id2++;
} else if(v[id1] < g[r][id2]) {
id1++;
} else {
id2++;
}
}
swap(tmp, v);
}
for(int j = 1; j <= m; j++) {
if(a[r][j] >= a[l - 1][j]) add(j, 1);
if(mx[j] >= a[r][j]) add(j, -1);
else {
mx[j] = a[r][j];
}
}
}
}
return ans;
}
Compilation message (stderr)
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:90: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]
90 | while(id1 < v.size() && id2 < g[r].size()) {
| ~~~~^~~~~~~~~~
rect.cpp:90:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | while(id1 < v.size() && id2 < g[r].size()) {
| ~~~~^~~~~~~~~~~~~| # | 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... |