# The Second H.Second Large Rectangle Monotone Stack of Niuke Multicultural College in 2019

Second Large Rectangle

Main idea of the title:
Find out what the area of the second largest all-1 matrix in N*M matrix is.

Analysis:
A two-dimensional array dp [i] [j] is used to represent the maximum number of consecutive 1 up the j column of row I.
Then go to violence and enumerate every line.
If the height of the matrix maintained in the stack is higher than the current dp[i][j], then the stack will be out.
Update the answer every time you update the stack.

You can actually simulate the stack manually

```#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int dp[1010][1010];
struct node{
int h,w;
};
stack<node>s,tem;
int max1,max2;
void update(int x){
if(x>max1)
max2=max1,max1=x;
else
max2=max(max2,x);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n,m;
scanf("%d%d",&n,&m);
rep(i,1,n){
rep(j,1,m) {
scanf("%1d",&dp[i][j]);
if(dp[i][j]!=0)
dp[i][j]+=dp[i-1][j];
}
}

for(int i=1;i<=n;i++){
while(!s.empty()) s.pop();//To traverse each line, a monotone stack is created from the new stack to ensure that the height of the matrix in the stack is stored incrementally.
for(int j=1;j<=m;j++){
if(dp[i][j]==0){//Disconnected, indicating that the existing matrix is meaningless and needs to start anew
while(!s.empty()) s.pop();
continue;
}
int res=j;
while(!s.empty() && s.top().h>dp[i][j]){//The previously stored matrices were too high
res=s.top().w;//The Right Boundary of the Matrix
s.pop();
}

if(s.empty() || s.top().h!=dp[i][j]) s.push((node){dp[i][j],res});//Add new right boundary and high
while(!s.empty()){
node u=s.top();
s.pop();
update(u.h*(j-u.w+1));
tem.push(u);
}
while(!tem.empty()){
node u=tem.top();
tem.pop();
s.push(u);
}
}
}
printf("%d\n",max2);
return 0;
}

```

Added by FamousMortimer on Sun, 06 Oct 2019 07:04:18 +0300