If the answer is within a fragment, then the direct suspension method solves the time complexity of $o (n\sum) $.
If the $n$ is larger, then the $\sum$ is smaller.
To find out the length of each point up to extend, enumerate each point upward of this segment as a short board.
Figure out the sum of the length of the completely optional fragment and not the full selection, the largest extension distance to the right of the left, update the answer.
Time complexity $o (n\sum^2) $.
If the $n$ is relatively small, then the brute force enumerates the upper and lower bounds and computes the answer as above.
Time complexity $o (n^2\sum) $.
Total time complexity $o (N\sum\sqrt{n\sum}) $.
#include <cstdio>const int n=100010,m=320;int t,num,n,m,i,j,k,x,cnt,fl0,gl,fl1,fr0,gr,fr1,ans;inline void Up ( Int&f0,int&g0,int&f1,int X,int y) {if (x>f0) {F1=f0,f0=x,g0=y;return;} if (X>F1) f1=x;} inline void Uans (int x) {if (ans<x) ans=x;} namespace Nsmall{int St[n],en[n],f[n],g[n],w[n];char a[m][n],s[n];void solve () {for (i=1;i<=num;i++) {scanf ("%d", & AMP;X); st[i]=m+1; En[i]=m+x; for (j=1;j<=n;j++) {scanf ("%s", s); for (k=0;k<x;k++) a[j][k+st[i]]=s[k]-' 0 '; } m+=x; } for (i=1;i<=m;i++) f[i]=1,g[i]=m,w[i]=0; for (i=1;i<=n;i++) {for (gl=j=1;j<=m;j++) if (!a[i][j]) {w[j]++; if (Gl>f[j]) F[j]=gl; }else w[j]=0,f[j]=1,g[j]=m,gl=j+1; for (gr=j=m;j;j--) if (!a[i][j]) {if (gr<g[j]) g[j]=gr; Uans (w[j]* (g[j]-f[j]+1)); }else gr=j-1; } for (i=1;i<=n;i++) {for (k=1;k<=num;k++) f[k]=en[k],g[k]=st[k]; for (j=i;j<=n;j++) {cnt=fl0=gl=fl1=fr0=gr=fr1=0; for (k=1;k<=num;k++) {for (x=st[k];x<=en[k];x++) if (a[j][x]) break; if (f[k]>x-1) f[k]=x-1; for (x=en[k];x>=st[k];x--) if (a[j][x]) break; if (g[k]<x+1) g[k]=x+1; if (F[k]==en[k]) {cnt+=en[k]-st[k]+1;continue;} Up (FL0,GL,FL1,F[K]-ST[K]+1,K); Up (FR0,GR,FR1,EN[K]-G[K]+1,K); } if (GL!=GR) Uans ((j-i+1) * (CNT+FL0+FR0)); else{Uans ((j-i+1) * (CNT+FL0+FR1)); Uans ((j-i+1) * (CNT+FL1+FR0)); }}}}}namespace Nbig{int St[m],en[m],f[m],g[m],w[m];char a[n][m],s[m];void Solve () {for (i=1;i<=num;i++) {scanf ("%d", &x); st[i]=m+1; En[i]=m+x; for (j=1;j<=n;j++) {scanf ("%s", s); for (k=0;k<x;k++) a[j][k+st[i]]=s[k]-' 0 '; } m+=x; } for (i=1;i<=m;i++) f[i]=1,g[i]=m,w[i]=0; for (i=1;i<=n;i++) {for (gl=j=1;j<=m;j++) if (!a[i][j]) {w[j]++; if (Gl>f[j]) F[j]=gl; }else w[j]=0,f[j]=1,g[j]=m,gl=j+1; for (gr=j=m;j;j--) if (!a[i][j]) {if (gr<g[j]) g[j]=gr; Uans (w[j]* (g[j]-f[j]+1)); }else gr=j-1; } for (I=1;I≪=m;i++) w[i]=0; for (i=1;i<=n;i++) {for (j=1;j<=m;j++) if (A[i][j]) W[j]=0;else w[j]++; for (j=1;j<=m;j++) if (W[j]) {cnt=fl0=gl=fl1=fr0=gr=fr1=0; for (k=1;k<=num;k++) {for (x=st[k];x<=en[k];x++) if (w[x]<w[j]) break; F[k]=x-1; for (x=en[k];x>=st[k];x--) if (w[x]<w[j]) break; g[k]=x+1; if (F[k]==en[k]) {cnt+=en[k]-st[k]+1;continue;} Up (FL0,GL,FL1,F[K]-ST[K]+1,K); Up (FR0,GR,FR1,EN[K]-G[K]+1,K); } if (GL!=GR) Uans (w[j]* (CNT+FL0+FR0)); else{Uans (w[j]* (CNT+FL0+FR1)); Uans (w[j]* (cnt+fl1+fr0)); }}}}}int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d", &num,&n); m=ans=0; if (n<=315) nsmall::solve (); else nbig::solve (); printf ("%d\n", ans); } return 0;}
BZOJ3873: [Ahoi2014] Jigsaw puzzle