# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
144308 | Lyestria | Rectangles (IOI19_rect) | C++14 | 0 ms | 0 KiB |
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;
#define int short
const int mn=2510;
struct seg{
int seg[mn][12];
inline void up(int a,int b){
seg[a][0]=b;
}
inline void init(){
for(int i=1;i<12;i++){
for(int j=0;j+(1<<i-1)<mn;j++){
seg[j][i]=min(seg[j][i-1],seg[j+(1<<i-1)][i-1]);
}
}
}
inline int qu(int a,int b){
int ret=0x3f3f3f3f;
for(int 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];
int 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) {
int n=a.size(),m=a[0].size();
int 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++){
int 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();
}