fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/7fd32d665969513bffe7864c52735137
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. String[] hw = in.readLine().split(" ", 2);
  16. final int H = Integer.parseInt(hw[0]);
  17. final int W = Integer.parseInt(hw[1]);
  18. final String NEWLINE = System.getProperty("line.separator");
  19.  
  20. int[][] space2left = new int[H][W];
  21. int[][] space2top = new int[H][W];
  22. int allSpaceCount = 0;
  23.  
  24. int[] startPointX = new int[H * (1 + W / 3)];
  25. int[] startPointY = new int[H * (1 + W / 3)];
  26. int startPointCount = 0;
  27.  
  28. int[] space2leftCount = new int[W + 1];
  29. int[] space2topCount = new int[H + 1];
  30.  
  31. int maxSpace2leftLen = 0;
  32. int maxSpace2topLen = 0;
  33.  
  34. int[] tempSP2Ty1 = null;;
  35.  
  36. for (int y = 0; y < H; y++)
  37. {
  38. int[] tempSP2Ty = space2top[y];
  39. String line = in.readLine();
  40. int spaceCount = 0;
  41. for (int x = 0; x < W; x++)
  42. {
  43. if (line.charAt(x) == '0') // 空
  44. {
  45. spaceCount++;
  46. allSpaceCount++;
  47. if (y > 0)
  48. {
  49. int tempCount = tempSP2Ty[x] = tempSP2Ty1[x] + 1;
  50. if (y == H - 1 && tempCount > 1)
  51. {
  52. space2topCount[tempCount]++;
  53. if (tempCount > maxSpace2topLen)
  54. {
  55. maxSpace2topLen = tempCount;
  56. }
  57. }
  58. }
  59. else // if (y == 0)
  60. {
  61. tempSP2Ty[x] = 1;
  62. }
  63. }
  64. else // if (ch == '1') // 埋
  65. {
  66. if (spaceCount > 1)
  67. {
  68. startPointX[startPointCount] = x - 1;
  69. startPointY[startPointCount] = y;
  70. startPointCount++;
  71. space2leftCount[spaceCount]++;
  72. if (spaceCount > maxSpace2leftLen)
  73. {
  74. maxSpace2leftLen = spaceCount;
  75. }
  76. }
  77. spaceCount = 0;
  78. // space2top[y][x] = 0; // 初期値のまんま
  79. if (y > 0) {
  80. space2topCount[tempSP2Ty1[x]]++;
  81. if (tempSP2Ty1[x] > maxSpace2topLen)
  82. {
  83. maxSpace2topLen = tempSP2Ty1[x];
  84. }
  85. }
  86. }
  87. space2left[y][x] = spaceCount;
  88. } // for(x)
  89. if (spaceCount > 1)
  90. {
  91. startPointX[startPointCount] = W - 1;
  92. startPointY[startPointCount] = y;
  93. startPointCount++;
  94. space2leftCount[spaceCount]++;
  95. if (spaceCount > maxSpace2leftLen)
  96. {
  97. maxSpace2leftLen = spaceCount;
  98. }
  99. }
  100. tempSP2Ty1 = tempSP2Ty;
  101. } // for(y)
  102.  
  103. final int N = Integer.parseInt(in.readLine());
  104. int[][] result = new int[301][301];
  105.  
  106. result[1][1] = allSpaceCount + 1;
  107. for (int i = 2; i <= maxSpace2topLen; i++)
  108. {
  109. int count = 0;
  110. for (int j = i; j <= maxSpace2topLen; j++)
  111. {
  112. count += space2topCount[j] * (j - i + 1);
  113. }
  114. result[i][1] = count + 1;
  115. }
  116. for (int i = 2; i <= maxSpace2leftLen; i++)
  117. {
  118. int count = 0;
  119. for (int j = i; j <= maxSpace2leftLen; j++)
  120. {
  121. count += space2leftCount[j] * (j - i + 1);
  122. }
  123. result[1][i] = count + 1;
  124. }
  125.  
  126. StringBuilder output = new StringBuilder(N * (6 + NEWLINE.length()));
  127.  
  128. for (int i = 0; i < N; i++)
  129. {
  130. String[] st = in.readLine().split(" ", 2);
  131. int s = Integer.parseInt(st[0]);
  132. int t = Integer.parseInt(st[1]);
  133.  
  134. int count = 0;
  135.  
  136. if (s <= maxSpace2topLen && t <= maxSpace2leftLen)
  137. {
  138. if (result[s][t] > 0)
  139. {
  140. count = result[s][t] - 1;
  141. }
  142. else // if (s != 1 && t != 1)
  143. {
  144. for (int j = 0; j < startPointCount; j++)
  145. {
  146. int x = startPointX[j];
  147. int y = startPointY[j];
  148. int seekLength = space2left[y][x] - t + 1;
  149.  
  150. int[] tempSP2Ty = space2top[y];
  151. for (int dx = 0; dx < seekLength; dx++)
  152. {
  153. int tx = x - dx;
  154. if (tempSP2Ty[tx] < s)
  155. {
  156. continue;
  157. }
  158. int dy;
  159. for (dy = 0; dy < s; dy++)
  160. {
  161. if (space2left[y - dy][tx] < t)
  162. {
  163. break;
  164. }
  165. } // for(dy)
  166. if (dy == s)
  167. {
  168. count++;
  169. }
  170. } // for(dx)
  171. } // for(j)
  172. }
  173. }
  174. output.append(count);
  175. output.append(NEWLINE);
  176. result[s][t] = count + 1;
  177. } // for(i)
  178.  
  179. System.out.print(output);
  180.  
  181. } // void main([String)
  182.  
  183. } // class Main
Success #stdin #stdout 0.08s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
7
2 2
1 1
3 2
3 2
2 3
3 1
1 3
stdout
6
20
2
2
1
6
7