令 为 为 # 或者 已经超出边界了,则可以推出 ,不过前提是 是不适合种草莓的地方。
然后用一行一行枚举,对于每一行,需用单调栈去统计面积。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2005;
typedef long long LL;
typedef pair<int,int> PII;
int n,m,h[N][N];
char a[N][N];
LL ans;
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='.')
h[i][j]=h[i-1][j]+1;
stack<PII> st;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int w=j;
while(st.size()&&st.top().second!=h[i][j])
{
if(st.top().second<h[i][j])
{
st.push(make_pair(w,h[i][j]));
break;
}
PII head=st.top();
st.pop();
int H=h[i][j];
if(st.size()) H=max(h[i][j],st.top().second);
H++;
ans+=(H+head.second)*(head.second-H+1)/2*(j-head.first)*(j-head.first+1)*(j-head.first+2)/6;
w=head.first;
}
if(st.empty()) st.push(make_pair(w,h[i][j]));
}
while(st.size())
{
PII head=st.top();
st.pop();
ans+=(head.second+(st.size()?st.top().second:0)+1)*(head.second-(st.size()?st.top().second:0))/2*(m+1-head.first)*(m-head.first+2)*(m-head.first+3)/6;
}
}
cout<<ans<<'\n';
return 0;
}