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