01 demand matrix in the number of different matrix 1
First, the pre-pre [i] [j] denotes the number of consecutive i 1 above, the height of each row of stack processing monotone
The stack elements maintain two values: pre [i] [j] and a forwardly extending up to maintain the position pos
Then calculate contributions, maintain a rightmost column of Table 1 below no position p sweep from left to right,
It can be determined whether the element includes a p-pos, when the pop-up, if this can be described is a matrix element represents contributes
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>pii; typedef vector<int>vi; #define rep(i,a,b) for(int i=(a);i<(b);i++) #define fi first #define se second #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" " ; #define pb(x) push_back(x) #define per(i,a,b) for(int i=(b)-1;i>=(a);--i) const int N=5e3+5; char mp[N][N]; int h[N][N]; int area[N][N]; int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;++i) scanf("%s",mp[i]+1); memset(h,0,sizeof(h)); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(mp[i][j]=='1'){ h[i][j]=h[i-1][j]+1; } int ans=0; for(int i=1;i<=n;++i){ stack<pii>stk; int ma=-1; for(int j=1;j<=m+1;++j){ int pos=j; while(!stk.empty()&&stk.top().fi>h[i][j]){ cout<<stk.top().fi<<'\n'; if(stk.top().se<=ma){ ans++; } pos=stk.top().se; stk.pop(); } if(!h[i+1][j])ma=j; if(h[i][j]&& ( stk.empty() || (stk.top().fi<h[i][j]) ) ) stk.push(pii(h[i][j],pos)); } } cout<<ans<<endl; return 0; }