fork download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/fafd0f37b8136a209785c35a052b325e
  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. int tempCount;
  80. if (y > 0 && (tempCount = tempSP2Ty1[x]) > 1) {
  81. space2topCount[tempCount]++;
  82. if (tempCount > maxSpace2topLen)
  83. {
  84. maxSpace2topLen = tempCount;
  85. }
  86. }
  87. }
  88. space2left[y][x] = spaceCount;
  89. } // for(x)
  90. if (spaceCount > 1)
  91. {
  92. startPointX[startPointCount] = W - 1;
  93. startPointY[startPointCount] = y;
  94. startPointCount++;
  95. space2leftCount[spaceCount]++;
  96. if (spaceCount > maxSpace2leftLen)
  97. {
  98. maxSpace2leftLen = spaceCount;
  99. }
  100. }
  101. tempSP2Ty1 = tempSP2Ty;
  102. } // for(y)
  103.  
  104. final int N = Integer.parseInt(in.readLine());
  105. int[][] result = new int[301][301];
  106.  
  107. StringBuilder output = new StringBuilder(N * (6 + NEWLINE.length()));
  108.  
  109. for (int i = 0; i < N; i++)
  110. {
  111. String[] st = in.readLine().split(" ", 2);
  112. int s = Integer.parseInt(st[0]);
  113. int t = Integer.parseInt(st[1]);
  114.  
  115. int count = 0;
  116.  
  117. if (s <= H && t <= W)
  118. {
  119. if (result[s][t] > 0)
  120. {
  121. count = result[s][t] - 1;
  122. }
  123. else if (s != 1 && t != 1)
  124. {
  125. for (int j = 0; j < startPointCount; j++)
  126. {
  127. int x = startPointX[j];
  128. int y = startPointY[j];
  129. int seekLength = space2left[y][x] - t + 1;
  130.  
  131. int[] tempSP2Ty = space2top[y];
  132. for (int dx = 0; dx < seekLength; dx++)
  133. {
  134. int tx = x - dx;
  135. if (tempSP2Ty[tx] < s)
  136. {
  137. continue;
  138. }
  139. int dy;
  140. for (dy = 0; dy < s; dy++)
  141. {
  142. if (space2left[y - dy][tx] < t)
  143. {
  144. break;
  145. }
  146. } // for(dy)
  147. if (dy == s)
  148. {
  149. count++;
  150. }
  151. } // for(dx)
  152. } // for(j)
  153. }
  154. else if (s != 1)
  155. {
  156. for (int j = s; j <= maxSpace2topLen; j++)
  157. {
  158. count += space2topCount[j] * (j - s + 1);
  159. }
  160. }
  161. else if (t != 1)
  162. {
  163. for (int j = t; j <= maxSpace2leftLen; j++)
  164. {
  165. count += space2leftCount[j] * (j - t + 1);
  166. }
  167. }
  168. else // if (s == 1 && t == 1)
  169. {
  170. count = allSpaceCount;
  171. }
  172. }
  173. output.append(count);
  174. output.append(NEWLINE);
  175. result[s][t] = count + 1;
  176. } // for(i)
  177.  
  178. System.out.print(output);
  179.  
  180. } // void main([String)
  181.  
  182. } // class Main
Success #stdin #stdout 0.08s 380160KB
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