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 forn(i,n) for(int i=0; i<n; ++i)
#define pi pair<int,int>
#define f first
#define s second
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
const int N=700;
struct dsu {
vector<int> p,sz,l,r;
dsu(int n) {
forn(i,n) p.pb(i), sz.pb(1);
l=r=p;
}
int get(int u) {
return p[u]==u?u:get(p[u]);
}
void uni(int u, int v) {
u=get(u),v=get(v);
if (u==v) return;
if (sz[u]<sz[v]) swap(u,v);
p[v]=u;
sz[u]+=sz[v];
l[u]=min(l[u],l[v]);
r[u]=max(r[u],r[v]);
}
};
vector<dsu> dsu1(N,dsu(N)), dsu2(N,dsu(N));
int ok[N][N];
int a[N][N];
int pre[N+1];
const int K=11;
int st1[N][N][K], st2[N][N][K];
void init() {
int p=0;
forn(i,N+1) {
while (2*(1<<p) < i) ++p;
pre[i]=p;
}
}
void build(int k, vector<int>&a) {
int n=a.size();
forn(i,n) st1[k][i][0]=a[i];
for(int j=1; j<K; ++j) {
forn(i,n) st1[k][i][j]=max(st1[k][i][j-1], (i+(1<<(j-1)))<n ? st1[k][i+(1<<(j-1))][j-1] : 0 );
}
}
inline int query1(int k, int l, int r) {
int p=pre[r-l+1];
return max( st1[k][l][p] , st1[k][r-(1<<p)+1][p] );
}
inline int query2(int k, int l, int r) {
int p=pre[r-l+1];
return max( st2[k][l][p] , st2[k][r-(1<<p)+1][p] );
}
int ite=0;
int check(int l, int u, int r, int d) {
int ok=1;
for (int i=l; i<=r; ++i) {
int x=query1(i,u,d);
ok&=a[i][u-1]>x && x<a[i][d+1];
}
for (int i=u; i<=d; ++i) {
int x=query2(i,l,r);
ok&=a[l-1][i]>x && x<a[r+1][i];
}
return ok;
}
ll count_rectangles(vector<vector<int>> b) {
int n=b.size(), m=b[0].size();
if (min(n,m)<3) return 0;
if (n==3) {
ll ans=0;
for (int i=0; i<m; ++i) {
int mx=0;
for (int j=i+1; j<m-1; ++j) {
if (b[0][j]<=b[1][j] || b[1][j]>=b[2][j]) break;
mx=max(mx,b[1][j]);
ans += b[1][i]>mx && mx<b[1][j+1];
}
}
return ans;
}
if (max(n,m)>N) exit(0);
init();
forn(i,n) forn(j,m) a[i][j]=b[i][j];
forn(i,n) build(i,b[i]);
swap(st1,st2);
forn(j,m) {
vector<int> v; forn(i,n) v.pb(a[i][j]);
build(j,v);
}
swap(st1,st2);
vector<pi> v;
forn(i,n) forn(j,m) v.pb({a[i][j],i*m+j});
sort(all(v));
ll ans=0;
int N=v.size();
map<ll,ll> was;
for(int i=0; i<N; ) {
vector<int> z;
int x=v[i].f;
while (i<N && v[i].f==x) {
z.pb(v[i].s);
++i;
}
for(auto&it:z) {
int x=it/m, y=it%m;
if (x) if (ok[x-1][y]) dsu1[y].uni(x-1,x);
if (x<n-1) if (ok[x+1][y]) dsu1[y].uni(x+1,x);
if (y) if (ok[x][y-1]) dsu2[x].uni(y-1,y);
if (y<m-1) if (ok[x][y+1]) dsu2[x].uni(y+1,y);
ok[x][y]=1;
}
for(auto&it:z) {
int x=it/m, y=it%m;
int u=dsu1[y].get(x);
int a=dsu1[y].l[u], c=dsu1[y].r[u];
int v=dsu2[x].get(y);
int b=dsu2[x].l[v], d=dsu2[x].r[v];
if (a==0 || b==0 || c==n-1 || d==m-1) continue;
if (was[1ll*a*m+b+1ll*n*m*(1ll*c*m+d)]) continue;
was[1ll*a*m+b+1ll*n*m*(1ll*c*m+d)]=1;
if (ite<=2e8) {
int q = check(a,b,c,d);
ans+=q;
ite+=c-a+d-b+2;
} else exit(0);
}
}
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... |