fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/d482f7d8f273718b3b30121e60747eb6
  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. int tx = x;
  132. for (int dx = 0; dx < seekLength; dx++)
  133. {
  134. if (tempSP2Ty[tx] >= s)
  135. {
  136. int dy;
  137. int ty = y;
  138. for (dy = 0; dy < s; dy++)
  139. {
  140. if (space2left[ty][tx] < t)
  141. {
  142. break;
  143. }
  144. ty--;
  145. } // for(dy)
  146. if (dy == s)
  147. {
  148. count++;
  149. }
  150. }
  151. tx--;
  152. } // for(dx)
  153. } // for(j)
  154. }
  155. else if (s != 1)
  156. {
  157. for (int j = s; j <= maxSpace2topLen; j++)
  158. {
  159. count += space2topCount[j] * (j - s + 1);
  160. }
  161. }
  162. else if (t != 1)
  163. {
  164. for (int j = t; j <= maxSpace2leftLen; j++)
  165. {
  166. count += space2leftCount[j] * (j - t + 1);
  167. }
  168. }
  169. else // if (s == 1 && t == 1)
  170. {
  171. count = allSpaceCount;
  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 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