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
pii rng[maxn];
int par[maxn];
bool ded[maxn];
int find(int u){
if(par[u]==u) return u;
return par[u]=find(par[u]);
}
void merge(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
rng[b]={min(rng[a].f,rng[b].f),max(rng[a].s,rng[b].s)};
par[a]=b;
}
int pf[maxn];
long long count_rectangles(std::vector<std::vector<int> > a) {
int n=sz(a),m=sz(a[0]);
map<pii,vector<int>> mp,m2;
REP1(i,n-2){
vector<pii> v;
REP(j,m) rng[j]={j,j},par[j]=j;
REP(j,m){
v.pb({a[i][j],j});
}
sort(ALL(v));
vector<pii> vv;
for(int j=0;j<sz(v);){
int p=j;
vector<int> ps;
while(j<sz(v) and v[p].f==v[j].f){
int z=v[j].s;
if(z and a[i][z-1]<=a[i][z]) merge(z-1,z);
if(z+1<m and a[i][z+1]<=a[i][z]) merge(z,z+1);
ps.pb(z);
++j;
}
for(int x:ps){
auto z=rng[find(x)];
if(z.f and z.s!=m-1) {
vv.pb(z);
}
}
}
SORT_UNIQUE(vv);
for(auto x:vv){
mp[x].pb(i);
}
}
ll ans=0;
REP1(i,m-2){
vector<pii> v;
REP(i,n) rng[i]={i,i},par[i]=i;
REP(j,n){
v.pb({a[j][i],j});
}
sort(ALL(v));
vector<pii> vv;
for(int j=0;j<sz(v);){
int p=j;
vector<int> ps;
while(j<sz(v) and v[p].f==v[j].f){
int z=v[j].s;
if(z and a[z-1][i]<=a[z][i]) merge(z-1,z);
if(z+1<n and a[z+1][i]<=a[z][i]) merge(z,z+1);
ps.pb(z);
++j;
}
for(int x:ps){
auto z=rng[find(x)];
if(z.f and z.s!=n-1) {
vv.pb(z);
}
}
}
SORT_UNIQUE(vv);
for(auto x:vv){
m2[x].pb(i);
}
}
map<pii,vector<pii>> mm;
for(auto x:m2){
for(int j=0;j<sz(x.s);){
int p=j;
++j;
while(j<sz(x.s) and x.s[j]==x.s[j-1]+1){
++j;
}
int l=x.s[p],r=x.s[j-1];
mm[x.f].pb({l,r});
}
}
for(auto x:mp){
for(int j=0;j<sz(x.s);){
int p=j;
++j;
while(j<sz(x.s) and x.s[j]==x.s[j-1]+1){
++j;
}
int l=x.s[p],r=x.s[j-1];
for(int i=l;i<=r;i++){
for(int k=i;k<=r;k++){
if(!mm.count({i,k})) continue;
auto it=lower_bound(ALL(mm[make_pair(i,k)]),make_pair(x.f.f,INT_MAX));
if(it==mm[{i,k}].begin()) continue;
--it;
if((*it).s>=x.f.s) ++ans;
}
}
}
}
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... |