fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/fe965782d45f70aeef2f48cfbb70e0e4
  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 = maxSpace2topLen; i <= H; i++)
  117. {
  118. result[i][1] = 1;
  119. }
  120. for (int i = 2; i <= maxSpace2leftLen; i++)
  121. {
  122. int count = 0;
  123. for (int j = i; j <= maxSpace2leftLen; j++)
  124. {
  125. count += space2leftCount[j] * (j - i + 1);
  126. }
  127. result[1][i] = count + 1;
  128. }
  129. for (int i = maxSpace2leftLen; i <= W; i++)
  130. {
  131. result[1][i] = 1;
  132. }
  133.  
  134. StringBuilder output = new StringBuilder(N * (6 + NEWLINE.length()));
  135.  
  136. for (int i = 0; i < N; i++)
  137. {
  138. String[] st = in.readLine().split(" ", 2);
  139. int s = Integer.parseInt(st[0]);
  140. int t = Integer.parseInt(st[1]);
  141.  
  142. int count = 0;
  143.  
  144. if (s <= H && t <= W)
  145. {
  146. if (result[s][t] > 0)
  147. {
  148. count = result[s][t] - 1;
  149. }
  150. else // if (s != 1 && t != 1)
  151. {
  152. for (int j = 0; j < startPointCount; j++)
  153. {
  154. int x = startPointX[j];
  155. int y = startPointY[j];
  156. int seekLength = space2left[y][x] - t + 1;
  157.  
  158. int[] tempSP2Ty = space2top[y];
  159. for (int dx = 0; dx < seekLength; dx++)
  160. {
  161. int tx = x - dx;
  162. if (tempSP2Ty[tx] < s)
  163. {
  164. continue;
  165. }
  166. int dy;
  167. for (dy = 0; dy < s; dy++)
  168. {
  169. if (space2left[y - dy][tx] < t)
  170. {
  171. break;
  172. }
  173. } // for(dy)
  174. if (dy == s)
  175. {
  176. count++;
  177. }
  178. } // for(dx)
  179. } // for(j)
  180. }
  181. }
  182. output.append(count);
  183. output.append(NEWLINE);
  184. result[s][t] = count + 1;
  185. } // for(i)
  186.  
  187. System.out.print(output);
  188.  
  189. } // void main([String)
  190.  
  191. } // class Main
Success #stdin #stdout 0.07s 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