Topic Portal: Http://www.nocow.cn/index.php/Translate:USACO/range
If this problem is not done before the same topic, I think I can not do it, probably is pre-treatment s[i][j], the first I row before the J column number of 1, and then O (1) time to determine whether the square has 1.
S[I][J] is obviously equal to s[i-1][j]+s[i][j-1]-s[i-1][j-1]+f[i][j] (two polygons repeating part is s[i-1][j-1])
And judging from line I to line l, column J to page K in this polygon there is no (j-k+1) * (l-i+1) 1, obviously only to determine whether s[i][j]-s[i-1][k]-s[l][j-1]+s[i-1][j-1] equals (j-k+1) * (l-i+1) On the line. (the repeating portion of two polygons is s[i-1][j-1])
There will be a bit of chaos, but just think about the diagram, and this problem is square, as long as the length of the enumeration side is good.
/*id:abc31261lang:c++task:range*/#include <cstdio> #include <cstring> #include <iostream>using namespace Std;const int Maxn=260;int n,f[maxn][maxn],s[maxn][maxn],num[maxn];int main () {int I,j,l;freopen ("range.in" , "R", stdin), Freopen ("Range.out", "w", stdout), scanf ("%d", &n), for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf (" %01d ", &f[i][j]); Memset (s,0,sizeof (s)); memset (num,0,sizeof (num)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) s[ I][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+f[i][j];for (i=1;i<=n;i++) for (j=1;j<=n;j++) for (L=2;l<=min (n-i+1, n-j+1); l++) {int x=i+l-1,y=j+l-1;if (s[x][y]-s[x][j-1]-s[i-1][y]+s[i-1][j-1]==l*l) num[l]++;} for (i=1;i<=n;i++) if (num[i]!=0) printf ("%d%d\n", I,num[i]); return 0;}
[Usaco3.3.4]range