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