#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
int h,w;
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};//UDLR
vi vals;
vector<vi> a;
vector<vector<vi>> d,mn,mx;
vector<vector<vector<vi>>> sm;
int main(){
sf("%d%d",&h,&w);
a.resize(h);
for(int i=0;i<h;++i){
a[i].resize(w);
for(int j=0;j<w;++j){
sf("%d",&a[i][j]);
vals.pb(a[i][j]);
}
}
disc(vals);
for(int i=0;i<h;++i){
for(int j=0;j<w;++j)a[i][j]=lb(vals,a[i][j])+1;
}
if(h<w){
vector<vi> b;
b.resize(w);
for(int i=0;i<w;++i)b[i].resize(h);
for(int i=0;i<h;++i){
for(int j=0;j<w;++j)b[j][i]=a[i][j];
}
swap(h,w);swap(a,b);
}
#ifdef DEBUG
dbg("new a:\n");
for(int i=0;i<h;++i){
for(int j=0;j<w;++j)dbg("%d ",a[i][j]);
dbg("\n");
}
dbg("\n");
#endif
d.resize(h);
for(int i=0;i<h;++i){
d[i].resize(w);
for(int j=0;j<w;++j){
d[i][j].resize(16);
}
}
dbg("d:\n");
for(int i=0;i<h;++i){
for(int j=0;j<w;++j){
dbg("%d %d: ",i,j);
for(int msk=0;msk<16;++msk){
bool valid=true;
vector<int> v={0,h*w+1};
for(int k=0;k<4;++k){
if(msk&(1<<k))continue;
int ni=i+dx[k],nj=j+dy[k];
if(ni<0||nj<0||ni>=h||nj>=w){
valid=false;continue;
}
v.pb(a[ni][nj]);
}
if(!valid){
dbg("-1 ");
d[i][j][msk]=-1;
continue;
}
sort(all(v));
int id=lb(v,a[i][j])-1;
d[i][j][msk]=v[id+1]-v[id];
dbg("%d ",d[i][j][msk]);
}
dbg("\n");
}
}
dbg("\n");
sm.resize(h);
mx.resize(h);
mn.resize(h);
for(int i=0;i<h;++i){
mx[i].resize(w);
mn[i].resize(w);
for(int j=0;j<w;++j){
mx[i][j].resize(w);
mn[i][j].resize(w);
}
}
for(int i=0;i<h;++i){
sm[i].resize(w);
for(int j=0;j<w;++j){
sm[i][j].resize(w);
for(int k=0;k<w;++k)sm[i][j][k].resize(4);
}
vector<vi> pfx;
pfx.resize(w);
for(int j=0;j<w;++j){
pfx[j].resize(4);
pfx[j][0]=d[i][j][0];//both open
pfx[j][1]=d[i][j][1];//top close
pfx[j][2]=d[i][j][2];//bottom close
pfx[j][3]=d[i][j][1+2];//both close
if(j==0)continue;
for(int k=0;k<4;++k)pfx[j][k]+=pfx[j-1][k];
}
for(int j=0;j<w;++j){
mn[i][j][j]=mx[i][j][j]=a[i][j];
for(int k=j;k<w;++k){
if(j==k){
sm[i][j][k][0]=d[i][j][4+8];//both open
sm[i][j][k][1]=d[i][j][1+4+8];//top clsoe
sm[i][j][k][2]=d[i][j][2+4+8];//bottom close
sm[i][j][k][3]=d[i][j][1+2+4+8];//both close
}
else{
mn[i][j][k]=min(mn[i][j][k-1],a[i][k]);
mx[i][j][k]=max(mx[i][j][k-1],a[i][k]);
sm[i][j][k][0]=d[i][j][4]+d[i][k][8];//both open
sm[i][j][k][1]=d[i][j][1+4]+d[i][k][1+8];//top close
sm[i][j][k][2]=d[i][j][2+4]+d[i][k][2+8];//bottom close
sm[i][j][k][3]=d[i][j][1+2+4]+d[i][k][1+2+8];//both close
if(j+1==k)continue;
for(int l=0;l<4;++l)sm[i][j][k][l]+=pfx[k-1][l]-pfx[j][l];
}
}
#ifdef DEBUG
for(int k=j;k<w;++k){
dbg("%d %d %d: ",i,j,k);
for(int l=0;l<4;++l)dbg("%d ",sm[i][j][k][l]);
dbg("\n");
}
#endif
}
}
dbg("here\n");
int ans=0;
for(int i=0;i<w;++i){
for(int j=i;j<w;++j){
vi pfx;
pfx.resize(h);
for(int k=0;k<h;++k){
pfx[k]=sm[k][i][j][0];
if(k!=0)pfx[k]+=pfx[k-1];
}
for(int k=0;k<h;++k){
int mnv=mn[k][i][j],mxv=mx[k][i][j];
for(int l=k;l<h;++l){
mnto(mnv,mn[l][i][j]);
mxto(mxv,mx[l][i][j]);
int tot=0;
if(k==l)tot+=sm[k][i][j][3];
else{
tot+=sm[k][i][j][1];
tot+=sm[l][i][j][2];
if(k+1!=l)tot+=pfx[l-1]-pfx[k];
}
if(tot==h*w+1+mxv-mnv)++ans;
}
}
}
}
pf("%d\n",ans);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:70:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | sf("%d%d",&h,&w);
| ^
Main.cpp:75:6: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | sf("%d",&a[i][j]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
5075 ms |
24820 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
952 KB |
Output is correct |
8 |
Correct |
11 ms |
1052 KB |
Output is correct |
9 |
Correct |
11 ms |
3768 KB |
Output is correct |
10 |
Correct |
10 ms |
3444 KB |
Output is correct |
11 |
Correct |
9 ms |
980 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
11 ms |
3956 KB |
Output is correct |
14 |
Correct |
7 ms |
2516 KB |
Output is correct |
15 |
Correct |
10 ms |
3412 KB |
Output is correct |
16 |
Correct |
10 ms |
3424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
952 KB |
Output is correct |
8 |
Correct |
11 ms |
1052 KB |
Output is correct |
9 |
Correct |
11 ms |
3768 KB |
Output is correct |
10 |
Correct |
10 ms |
3444 KB |
Output is correct |
11 |
Correct |
9 ms |
980 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
11 ms |
3956 KB |
Output is correct |
14 |
Correct |
7 ms |
2516 KB |
Output is correct |
15 |
Correct |
10 ms |
3412 KB |
Output is correct |
16 |
Correct |
10 ms |
3424 KB |
Output is correct |
17 |
Correct |
127 ms |
3764 KB |
Output is correct |
18 |
Correct |
134 ms |
24172 KB |
Output is correct |
19 |
Correct |
142 ms |
7308 KB |
Output is correct |
20 |
Correct |
153 ms |
36332 KB |
Output is correct |
21 |
Correct |
144 ms |
37680 KB |
Output is correct |
22 |
Correct |
149 ms |
37680 KB |
Output is correct |
23 |
Correct |
143 ms |
36276 KB |
Output is correct |
24 |
Correct |
113 ms |
26316 KB |
Output is correct |
25 |
Correct |
155 ms |
33444 KB |
Output is correct |
26 |
Correct |
162 ms |
35224 KB |
Output is correct |
27 |
Correct |
149 ms |
33436 KB |
Output is correct |
28 |
Correct |
149 ms |
35172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
952 KB |
Output is correct |
8 |
Correct |
11 ms |
1052 KB |
Output is correct |
9 |
Correct |
11 ms |
3768 KB |
Output is correct |
10 |
Correct |
10 ms |
3444 KB |
Output is correct |
11 |
Correct |
9 ms |
980 KB |
Output is correct |
12 |
Correct |
9 ms |
980 KB |
Output is correct |
13 |
Correct |
11 ms |
3956 KB |
Output is correct |
14 |
Correct |
7 ms |
2516 KB |
Output is correct |
15 |
Correct |
10 ms |
3412 KB |
Output is correct |
16 |
Correct |
10 ms |
3424 KB |
Output is correct |
17 |
Correct |
127 ms |
3764 KB |
Output is correct |
18 |
Correct |
134 ms |
24172 KB |
Output is correct |
19 |
Correct |
142 ms |
7308 KB |
Output is correct |
20 |
Correct |
153 ms |
36332 KB |
Output is correct |
21 |
Correct |
144 ms |
37680 KB |
Output is correct |
22 |
Correct |
149 ms |
37680 KB |
Output is correct |
23 |
Correct |
143 ms |
36276 KB |
Output is correct |
24 |
Correct |
113 ms |
26316 KB |
Output is correct |
25 |
Correct |
155 ms |
33444 KB |
Output is correct |
26 |
Correct |
162 ms |
35224 KB |
Output is correct |
27 |
Correct |
149 ms |
33436 KB |
Output is correct |
28 |
Correct |
149 ms |
35172 KB |
Output is correct |
29 |
Execution timed out |
5056 ms |
24872 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |