版本比较
题解:P15723 [JAG 2023 Summer Camp #3] Digit-only subrectangles
正文
增加 3 行删除 580 行
43 行未改动44454647484849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331450513163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286295243 行未改动对于第四类。通过跟第二类一样的办法能推出结果,这里不过多赘述。
其中第一、二、四类都是确定行然后枚举列,所以可以放在一起算。第三类是确定列然后枚举行,单独算一遍就行。
代码(笛卡尔树版本):代码(笛卡尔树版本):[详见这里](https://www.luogu.com.cn/paste/tz160ta5)。
```cpp#include<bits/stdc++.h>#define int long long#define code using#define by namespace#define plh stdcode 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];//我把第二类和第三类合并在一起变成了 Bint 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;}```代码(单调栈版本):[详见这里](https://www.luogu.com.cn/paste/t1f06dtg)。
代码(单调栈版本):
```cpp#include<bits/stdc++.h>#define int long long#define code using#define by namespace#define plh stdcode 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;}```麻烦管理员大大再审核一遍,谢谢。