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 sz(x) (int)x.size()
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define lowb(x) (x&(-x))
#define ALL(_x) _x.begin(),_x.end()
#define pii pair<int,int>
#define f first
#define s second
#define SORT_UNIQUE(x) sort(ALL(x)),x.erase(unique(ALL(x)),x.end())
const int maxn=3e3+5;
#define ll long long
vector<int> mp[maxn][maxn],m2[maxn][maxn];
int lok[maxn][maxn],rok[maxn][maxn];
inline vector<pii> get(vector<int> &val){
int n=sz(val);
vector<int> v;
vector<pii> vv;
REP(i,n){
while(sz(v) and val[v.back()]<val[i]){
if(i>v.back()+1) vv.pb({v.back()+1,i-1});
v.pop_back();
}
if(sz(v)){
if(i>v.back()+1) vv.pb({v.back()+1,i-1});
if(val[v.back()]==val[i]) v.pop_back();
}
v.pb(i);
}
return vv;
}
int ans=0;
vector<vector<int>> a;
void calc(int l,int r,int u,int d){
vector<int> vv;
for(int i=u-1;i<=d+1;i++){
vv.pb(a[i][r]);
}
auto zz=get(vv);
for(auto x:zz){
if(lok[x.f+u-1][x.s+u-1]<=l){
++ans;
}
}
}
long long count_rectangles(std::vector<std::vector<int> > b) {
a=b;
int n=sz(a),m=sz(a[0]);
REP1(i,n-2){
vector<int> val;
REP(j,m) val.pb(a[i][j]);
auto vv=get(val);
for(auto x:vv){
mp[x.f][x.s].pb(i);
}
}
REP(i,n) REP(j,n) lok[i][j]=rok[i][j]=-1;
REP1(i,m-2){
vector<int> val;
REP(j,n){
val.pb(a[j][i]);
}
auto vv=get(val);
for(auto x:vv){
if(rok[x.f][x.s]<i-1){
lok[x.f][x.s]=i;
}
rok[x.f][x.s]=i;
}
REP1(j,i){
if(!sz(mp[j][i])) continue;
int pv=mp[j][i][0];
REP1(z,sz(mp[j][i])-1){
int x=mp[j][i][z];
if(x>mp[j][i][z-1]+1){
calc(j,i,pv,mp[j][i][z-1]);
pv=x;
}
}
calc(j,i,pv,mp[j][i].back());
}
}
return ans;
}
# | 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... |