跳到正文

题解

题解:P7306 [COCI 2018/2019 #1] Strah

__liujy约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
2
题目
P7306

版本

  1. v1当前

令 hi,jh_{i,j} 为 ai−hi,j,ja_{i-h_{i,j},j} 为 # 或者 i−hi,ji-h_{i,j} 已经超出边界了,则可以推出 hi,j=hi−1,j+1h_{i,j}=h_{i-1,j}+1,不过前提是 ai,ja_{i,j} 是不适合种草莓的地方。

然后用一行一行枚举,对于每一行,需用单调栈去统计面积。

C++
#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;
}
原文