#include<bits/stdc++.h>
using namespace std;
const int N=5e3+5;
int n,m,x,y;
int s[N][N];
long long q[N],l[N],r[N];
long long calc(int h[],int n){
long long res=0,tt=0;
q[0]=0,h[n+1]=-1;
for(int i=1;i<=n;++i){
while (tt&&h[q[tt]]>=h[i]) tt--;
l[i]=q[tt];
q[++tt]=i;
}
tt=0,q[0]=n+1;
for(int i=n;i;--i){
while (tt&&h[q[tt]]>h[i]) tt--;
r[i]=q[tt];
q[++tt]=i;
}
for(int i=1;i<=n;++i)
res+=h[i]*(i-l[i])*(r[i]-i);
return res;
}
signed main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j) s[i][j]=1;
while (m--){
scanf("%d%d",&x,&y);
s[x][y]=0;
}
long long ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)
if(s[i][j]) s[i][j]+=s[i-1][j];
ans+=calc(s[i],n);
}
printf("%lld\n",ans);
}
求01矩阵矩形个数
最新推荐文章于 2022-06-24 13:19:08 发布