题目分析
我们考虑设问的潜力值之和是什么:每个点被覆盖次数和——面积和。也就是我们要求所有矩形面积和。
矩形题我们套路的枚举底边,我们知道每个位置可以最高到哪。
我们考虑使用扫描线来从左往右扫描,我们需要对于当前高度处理掉之前比他高的。这是非常板子的单调栈优化扫描线。
怎么处理掉之前比他高的?考虑按照高度分段(因为按照宽度分段要考虑要类似分治处理中间)处理。
这就变成了求底满足 ,考虑长度为 的有 个,所以是 ,前面可以快速求,后面可以前缀和预处理。考虑求高的和,等差数列求和即可,乘起来就是面积和。
时间复杂度 。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N=2001;
int n,m,a[N][N],sum[N],ans;
string tmp;
void init(){
for(int i=1;i<N;i++)
sum[i]=sum[i-1]+(i*i);
return;
}
int getlensum(int len){
return(len+1)*((len+1)*len/2)-sum[len];
}
int getsum(int l,int r,int h0,int h1){
if(h0>h1)
return 0;
int h=(h0+h1)*(h1-h0+1)/2;
return getlensum(r-l+1)*h;
}
int calc(int x){
static stack<pair<int,int> >st;//st is clean
int res=0;
for(int i=1;i<=m;i++){
while(!st.empty()&&st.top().second>=a[x][i]){
auto[pos,val]=st.top();
st.pop();
res+=getsum(st.empty()?1:(st.top().first+1),i-1,max(a[x][i],st.empty()?0:st.top().second)+1,val);
}
st.push({i,a[x][i]});
}
while(!st.empty()){
auto[pos,val]=st.top();
st.pop();
res+=getsum(st.empty()?1:(st.top().first+1),m,max(0ll,st.empty()?0:st.top().second)+1,val);
}
return res;
}
signed main(){
init();
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>tmp;
tmp=" "+tmp;
for(int j=1;j<=m;j++)
a[i][j]=(tmp[j]=='.');
}
for(int j=1;j<=m;j++)
for(int i=1;i<=n;i++)
if(a[i][j])
a[i][j]+=a[i-1][j];
for(int i=1;i<=n;i++)
ans+=calc(i);
cout<<ans;
return 0;
}