超级难写的一道题,成功的作为了我们考试的 T4。
首先我们可以写出一个 的超级暴力做法。现在我们尝试优化。
首先观察我们要计算的式子:若当前枚举到的矩形左上角是第 行第 列,右下角是第 行第 列,则我们应加上的贡献是:
好吧这个平方看着很烦人,我们拆开来看一下:
好吧看着很杂乱,我们整理一下:把平方项放在一起,然后把系数为 的放在一起,系数为 的放在一起,于是我们得到了三部分:
也就是说:对于任意的一个矩形,它们之间的乘法关系是这样的:
对于每一条边,我们把这条边两端的前缀和相乘再乘上这条边的边权然后全加在一起就是答案。
由此我们可以把求和的过程大致分成四类:
- 自环类(即平方项)。
- 横向类(即系数为 且两端同行不同列)。
- 纵向类(即系数为 且两端同列不同行)。
- 交叉类(即系数为 的这些项)。
首先说自环类。正常的思考顺序是我们确定了右下角的位置,然后枚举矩形高度,然后找到有多少个位置能作为这个矩形的左下角,然后算就对了。但是这样时间复杂度就超了。
在上述过程中我们会发现:如果我们已知一个左下角和一个右下角,那么我们能形成的矩形高度是会有限制的。设 表示第 行第 列向上延伸能延伸的最大高度。那么如果左下角为第 行第 列,右下角为第 行第 列,则这个限制明显是 。
于是我们可以反过来考虑:如果我已知当前限制是 ,那么求经过这个限制的矩形产生的贡献。因为我们规定了限制必须是 ,所以在 (左下角能到的最左边一列)到 (右下角能到的最右边一列)之间的 都必须小于 (其实这里说的不准确,准确来讲应该是一边小于等于,一边小于,这样既不会漏算,也不会多算)。因此我们可以用单调栈或笛卡尔树来算出左右边界。
假设对于第 行第 列来讲,它的左右边界是 和 ,那么第 行第 列之间所有的数都可以作为左下角,第 行第 之间的所有数可以作为右下角。然后左右两两搭配,我们就能算出答案。
最后简单说一下如果确定了限制和左右边界该怎么算答案:首先对于第一类,我们先考虑这两个位置是左下角和右下角时,每个左下角能和 个右下角匹配,同时高度在 之间可以任取,列出式子合并就能得到其实就是第 行第 列之间的前缀和的平方和乘 乘 。如果是左上角和右上角同理,只不过这里的 是向下延伸了。
对于第二类。以左下角和右下角为例:我们通过简单的合并就可以得到最终答案其实就是所有可能的左下角的前缀和的和乘上所有可能的右下角的前缀和的和。如果是其他两个角同理,每次只不过是 的定义变了一下。
对于第三类。跟第二类类似,只不过这里我们要竖着考虑, 就成了横向延伸。
对于第四类。通过跟第二类一样的办法能推出结果,这里不过多赘述。
其中第一、二、四类都是确定行然后枚举列,所以可以放在一起算。第三类是确定列然后枚举行,单独算一遍就行。
代码(笛卡尔树版本):
#include<bits/stdc++.h>
#define int long long
#define code using
#define by namespace
#define plh std
code by plh;
namespace fastio
{
inline int read()
{
int z=0,f=1;
char c=getchar();
if(c==EOF)
{
exit(0);
}
while(c<'0'||c>'9')
{
if(c==EOF)
{
exit(0);
}
if(c=='-')
{
f=-1;
}
c=getchar();
}
while(c>='0'&&c<='9')
{
z=z*10+c-'0';
c=getchar();
}
return z*f;
}
inline void write(int x)
{
if(x<0)
{
putchar('-');
x=-x;
}
static int top=0,stk[106];
while(x)
{
stk[++top]=x%10;
x/=10;
}
if(!top)
{
stk[++top]=0;
}
while(top)
{
putchar(char(stk[top--]+'0'));
}
}
inline void write(string s)
{
for(auto i:s)
{
putchar(i);
}
}
}
using namespace fastio;
const int mod=998244353;
int n,m,A,B,C,h[2506],ls[2506],rs[2506],a[2506][2506],s[2506][2506],ss[2506][2506],s2[2506][2506];//我把第二类和第三类合并在一起变成了 B
int M(int x)
{
if(x>=mod||x<=-mod)
{
x%=mod;
}
if(x<0)
{
x+=mod;
}
return x;
}
void build(int x)//建树
{
for(int i=1;i<=x;i++)
{
ls[i]=rs[i]=0;
}
stack<int>st;
for(int i=1;i<=x;i++)
{
int la=0;
while(!st.empty()&&h[st.top()]>h[i])
{
la=st.top();
st.pop();
}
ls[i]=la;
if(!st.empty())
{
rs[st.top()]=i;
}
st.push(i);
}
}
int calc(int sum[2506][2506],int u,int d,int l,int r)//算和
{
l=max(0ll,l-1),u=max(0ll,u-1);
return M(sum[d][r]-sum[d][l]-sum[u][r]+sum[u][l]);
}
void dfs1(int x,int y,int l,int r,int op)
{
if(ls[y])
{
dfs1(x,ls[y],l,y-1,op);
}
if(rs[y])
{
dfs1(x,rs[y],y+1,r,op);
}
A=M(A+M(M(h[y]*(y-l+1))*calc(ss,x-op,x-op,y,r))+M(M(h[y]*(r-y+1))*calc(ss,x-op,x-op,l-1,y-1)));
B=M(B+M(M(h[y]*calc(s2,x-op,x-op,y,r))*calc(s2,x-op,x-op,l-1,y-1)));
if(!op)
{
C=M(C+M(calc(s2,x,x,y,r)*calc(s2,x-h[y],x-1,l-1,y-1))+M(calc(s2,x,x,l-1,y-1)*calc(s2,x-h[y],x-1,y,r)));
}
}
void dfs2(int x,int y,int l,int r,int op)
{
if(ls[x])
{
dfs2(ls[x],y,l,x-1,op);
}
if(rs[x])
{
dfs2(rs[x],y,x+1,r,op);
}
if(!h[x])
{
return;
}
B=M(B+M(M(h[x]*calc(s2,x,r,y-op,y-op))*calc(s2,l-1,x-1,y-op,y-op)));
}
signed main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char c;
cin>>c;
if(c=='*')
{
a[i][j]=-1;
}
else
{
a[i][j]=c-'0';
}
s[i][j]=M(s[i][j-1]+s[i-1][j]-s[i-1][j-1]+max(a[i][j],0ll));
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ss[i][j]=M(ss[i][j-1]+ss[i-1][j]-ss[i-1][j-1]+M(s[i][j]*s[i][j]));
s2[i][j]=M(s2[i][j-1]+s2[i-1][j]-s2[i-1][j-1]+s[i][j]);
}
}
for(int i=1;i<=n;i++)
{
int mn=1e18,id=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1)
{
h[j]=0;
}
else
{
h[j]++;
}
if(h[j]<mn)
{
mn=h[j];
id=j;
}
}
build(m);
dfs1(i,id,1,m,0);
}
memset(h,0,sizeof(h));
for(int i=n;i>=1;i--)
{
int mn=1e18,id=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1)
{
h[j]=0;
}
else
{
h[j]++;
}
if(h[j]<mn)
{
mn=h[j];
id=j;
}
}
build(m);
dfs1(i,id,1,m,1);
}
memset(h,0,sizeof(h));
for(int j=1;j<=m;j++)
{
int mn=1e18,id=0;
for(int i=1;i<=n;i++)
{
if(a[i][j]==-1)
{
h[i]=0;
}
else
{
h[i]++;
}
if(h[i]<mn)
{
mn=h[i];
id=i;
}
}
build(n);
dfs2(id,j,1,n,0);
}
memset(h,0,sizeof(h));
for(int j=m;j>=1;j--)
{
int mn=1e18,id=0;
for(int i=1;i<=n;i++)
{
if(a[i][j]==-1)
{
h[i]=0;
}
else
{
h[i]++;
}
if(h[i]<mn)
{
mn=h[i];
id=i;
}
}
build(n);
dfs2(id,j,1,n,1);
}
write(M(A-2*B+2*C));
return 0;
}代码(单调栈版本):
#include<bits/stdc++.h>
#define int long long
#define code using
#define by namespace
#define plh std
code by plh;
namespace fastio
{
inline int read()
{
int z=0,f=1;
char c=getchar();
if(c==EOF)
{
exit(0);
}
while(c<'0'||c>'9')
{
if(c==EOF)
{
exit(0);
}
if(c=='-')
{
f=-1;
}
c=getchar();
}
while(c>='0'&&c<='9')
{
z=z*10+c-'0';
c=getchar();
}
return z*f;
}
inline void write(int x)
{
if(x<0)
{
putchar('-');
x=-x;
}
static int top=0,stk[106];
while(x)
{
stk[++top]=x%10;
x/=10;
}
if(!top)
{
stk[++top]=0;
}
while(top)
{
putchar(char(stk[top--]+'0'));
}
}
inline void write(string s)
{
for(auto i:s)
{
putchar(i);
}
}
}
using namespace fastio;
const int mod=998244353;
int n,m,A,B,C,h[2506],l[2506],r[2506],a[2506][2506],s[2506][2506],ss[2506][2506],s2[2506][2506];
int M(int x)
{
if(x>=mod||x<=-mod)
{
x%=mod;
}
if(x<0)
{
x+=mod;
}
return x;
}
int calc(int sum[2506][2506],int u,int d,int l,int r)
{
l=max(0ll,l-1),u=max(0ll,u-1);
return M(sum[d][r]-sum[d][l]-sum[u][r]+sum[u][l]);
}
signed main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
char c;
cin>>c;
if(c=='*')
{
a[i][j]=-1;
}
else
{
a[i][j]=c-'0';
}
s[i][j]=M(s[i][j-1]+s[i-1][j]-s[i-1][j-1]+max(a[i][j],0ll));
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ss[i][j]=M(ss[i][j-1]+ss[i-1][j]-ss[i-1][j-1]+M(s[i][j]*s[i][j]));
s2[i][j]=M(s2[i][j-1]+s2[i-1][j]-s2[i-1][j-1]+s[i][j]);
}
}
for(int i=1;i<=n;i++)
{
int mn=1e18,id=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1)
{
h[j]=0;
}
else
{
h[j]++;
}
if(h[j]<mn)
{
mn=h[j];
id=j;
}
}
stack<int>st;
for(int j=1;j<=m;j++)
{
while(!st.empty()&&h[st.top()]>=h[j])
{
st.pop();
}
l[j]=(st.empty()?1:st.top()+1);
st.push(j);
}
while(!st.empty())
{
st.pop();
}
for(int j=m;j>=1;j--)
{
while(!st.empty()&&h[st.top()]>h[j])
{
st.pop();
}
r[j]=(st.empty()?m:st.top()-1);
st.push(j);
}
for(int j=1;j<=m;j++)
{
A=M(A+M(M(h[j]*(j-l[j]+1))*calc(ss,i,i,j,r[j]))+M(M(h[j]*(r[j]-j+1))*calc(ss,i,i,l[j]-1,j-1)));
B=M(B+M(M(h[j]*calc(s2,i,i,j,r[j]))*calc(s2,i,i,l[j]-1,j-1)));
C=M(C+M(calc(s2,i,i,j,r[j])*calc(s2,i-h[j],i-1,l[j]-1,j-1))+M(calc(s2,i,i,l[j]-1,j-1)*calc(s2,i-h[j],i-1,j,r[j])));
}
}
memset(h,0,sizeof(h));
for(int i=n;i>=1;i--)
{
int mn=1e18,id=0;
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1)
{
h[j]=0;
}
else
{
h[j]++;
}
if(h[j]<mn)
{
mn=h[j];
id=j;
}
}
stack<int>st;
for(int j=1;j<=m;j++)
{
while(!st.empty()&&h[st.top()]>=h[j])
{
st.pop();
}
l[j]=(st.empty()?1:st.top()+1);
st.push(j);
}
while(!st.empty())
{
st.pop();
}
for(int j=m;j>=1;j--)
{
while(!st.empty()&&h[st.top()]>h[j])
{
st.pop();
}
r[j]=(st.empty()?m:st.top()-1);
st.push(j);
}
for(int j=1;j<=m;j++)
{
A=M(A+M(M(h[j]*(j-l[j]+1))*calc(ss,i-1,i-1,j,r[j]))+M(M(h[j]*(r[j]-j+1))*calc(ss,i-1,i-1,l[j]-1,j-1)));
B=M(B+M(M(h[j]*calc(s2,i-1,i-1,j,r[j]))*calc(s2,i-1,i-1,l[j]-1,j-1)));
}
}
memset(h,0,sizeof(h));
for(int j=1;j<=m;j++)
{
int mn=1e18,id=0;
for(int i=1;i<=n;i++)
{
if(a[i][j]==-1)
{
h[i]=0;
}
else
{
h[i]++;
}
if(h[i]<mn)
{
mn=h[i];
id=i;
}
}
stack<int>st;
for(int i=1;i<=n;i++)
{
while(!st.empty()&&h[st.top()]>=h[i])
{
st.pop();
}
l[i]=(st.empty()?1:st.top()+1);
st.push(i);
}
while(!st.empty())
{
st.pop();
}
for(int i=n;i>=1;i--)
{
while(!st.empty()&&h[st.top()]>h[i])
{
st.pop();
}
r[i]=(st.empty()?n:st.top()-1);
st.push(i);
}
for(int i=1;i<=n;i++)
{
B=M(B+M(M(h[i]*calc(s2,i,r[i],j,j))*calc(s2,l[i]-1,i-1,j,j)));
}
}
memset(h,0,sizeof(h));
for(int j=m;j>=1;j--)
{
int mn=1e18,id=0;
for(int i=1;i<=n;i++)
{
if(a[i][j]==-1)
{
h[i]=0;
}
else
{
h[i]++;
}
if(h[i]<mn)
{
mn=h[i];
id=i;
}
}
stack<int>st;
for(int i=1;i<=n;i++)
{
while(!st.empty()&&h[st.top()]>=h[i])
{
st.pop();
}
l[i]=(st.empty()?1:st.top()+1);
st.push(i);
}
while(!st.empty())
{
st.pop();
}
for(int i=n;i>=1;i--)
{
while(!st.empty()&&h[st.top()]>h[i])
{
st.pop();
}
r[i]=(st.empty()?n:st.top()-1);
st.push(i);
}
for(int i=1;i<=n;i++)
{
B=M(B+M(M(h[i]*calc(s2,i,r[i],j-1,j-1))*calc(s2,l[i]-1,i-1,j-1,j-1)));
}
}
write(M(A-2*B+2*C));
return 0;
}