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;
typedef long long ll;
const short mn=2510;
struct seg{
short seg[mn][12];
inline void up(short a,short b){
seg[a][0]=b;
}
inline void init(){
for(short i=1;i<12;i++){
for(short j=0;j+(1<<i-1)<mn;j++){
seg[j][i]=min(seg[j][i-1],seg[j+(1<<i-1)][i-1]);
}
}
}
inline short qu(short a,short b){
short ret=0x3f3f;
for(short i=11;i>=0;i--){
if(a+(1<<i)<=b+1)ret=min(ret,seg[a][i]),a+=(1<<i);
}
return ret;
}
};
seg lef[mn],rig[mn],up[mn],dow[mn];
short elef[mn][mn],erig[mn][mn],eup[mn][mn],edow[mn][mn];
multiset<ll>ss;
long long hsh(ll a,ll b,ll c,ll d){
return (a<<48)|(b<<32)|(c<<16)|d;
}
long long count_rectangles(std::vector<std::vector<int> > a) {
short n=a.size(),m=a[0].size();
short i,j;
stack<int>s;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
while(s.size()&&a[i][s.top()]<=a[i][j])rig[s.top()].up(i,j),s.pop();
s.push(j);
}
while(s.size())rig[s.top()].up(i,j),s.pop();
for(j=m-1;j>=0;j--){
while(s.size()&&a[i][s.top()]<=a[i][j])lef[s.top()].up(i,-j),s.pop();
s.push(j);
}
while(s.size())lef[s.top()].up(i,-j),s.pop();
for(j=0;j<m;j++){
while(s.size()&&a[i][s.top()]<a[i][j])erig[i][s.top()]=j,s.pop();
s.push(j);
}
while(s.size())erig[i][s.top()]=j,s.pop();
for(j=m-1;j>=0;j--){
while(s.size()&&a[i][s.top()]<a[i][j])elef[i][s.top()]=j,s.pop();
s.push(j);
}
while(s.size())elef[i][s.top()]=j,s.pop();
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
while(s.size()&&a[s.top()][i]<=a[j][i])dow[s.top()].up(i,j),s.pop();
s.push(j);
}
while(s.size())dow[s.top()].up(i,j),s.pop();
for(j=n-1;j>=0;j--){
while(s.size()&&a[s.top()][i]<=a[j][i])up[s.top()].up(i,-j),s.pop();
s.push(j);
}
while(s.size())up[s.top()].up(i,-j),s.pop();
for(j=0;j<n;j++){
while(s.size()&&a[s.top()][i]<a[j][i])edow[s.top()][i]=j,s.pop();
s.push(j);
}
while(s.size())edow[s.top()][i]=j,s.pop();
for(j=n-1;j>=0;j--){
while(s.size()&&a[s.top()][i]<a[j][i])eup[s.top()][i]=j,s.pop();
s.push(j);
}
while(s.size())eup[s.top()][i]=j,s.pop();
}
for(i=0;i<mn;i++)lef[i].init(),rig[i].init(),up[i].init(),dow[i].init();
for(i=1;i<n-1;i++){
for(j=1;j<m-1;j++){
short d,u,l,r;
d=edow[i][j];
u=eup[i][j];
l=elef[i][j];
r=erig[i][j];
if(l==-1||u==-1||r==m||d==n)continue;
if(rig[l].qu(u+1,d-1)<r)continue;
if(-lef[r].qu(u+1,d-1)>l)continue;
if(dow[u].qu(l+1,r-1)<d)continue;
if(-up[d].qu(l+1,r-1)>u)continue;
ss.insert(hsh(d,u,l,r));
}
}
return ss.size();
}
Compilation message (stderr)
rect.cpp: In member function 'void seg::init()':
rect.cpp:13:34: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
for(short j=0;j+(1<<i-1)<mn;j++){
~^~
rect.cpp:14:54: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
seg[j][i]=min(seg[j][i-1],seg[j+(1<<i-1)][i-1]);
~^~
# | 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... |