fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/d49e324a9921efb4c5c64a2a857b83f6
  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. static final String NEWLINE = System.getProperty("line.separator");
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23.  
  24. String[] hw = in.readLine().split(" ", 2);
  25. final int H = Integer.parseInt(hw[0]);
  26. final int W = Integer.parseInt(hw[1]);
  27.  
  28. int allSpaceCount = 0;
  29.  
  30. int startPointCount = 0;
  31.  
  32.  
  33. int maxSpace2leftLen = 0;
  34. int maxSpace2topLen = 0;
  35.  
  36. int[] tempSP2Ty1 = null;;
  37.  
  38. for (int y = 0; y < H; y++)
  39. {
  40. int[] tempSP2Ty = space2top[y];
  41. String line = in.readLine();
  42. int spaceCount = 0;
  43. for (int x = 0; x < W; x++)
  44. {
  45. if (line.charAt(x) == '0') // 空
  46. {
  47. spaceCount++;
  48. allSpaceCount++;
  49. if (y > 0)
  50. {
  51. int tempCount = tempSP2Ty[x] = tempSP2Ty1[x] + 1;
  52. if (y == H - 1 && tempCount > 1)
  53. {
  54. space2topCount[tempCount]++;
  55. if (tempCount > maxSpace2topLen)
  56. {
  57. maxSpace2topLen = tempCount;
  58. }
  59. }
  60. }
  61. else // if (y == 0)
  62. {
  63. tempSP2Ty[x] = 1;
  64. }
  65. }
  66. else // if (ch == '1') // 埋
  67. {
  68. if (spaceCount > 1)
  69. {
  70. startPointX[startPointCount] = x - 1;
  71. startPointY[startPointCount] = y;
  72. startPointCount++;
  73. space2leftCount[spaceCount]++;
  74. if (spaceCount > maxSpace2leftLen)
  75. {
  76. maxSpace2leftLen = spaceCount;
  77. }
  78. }
  79. spaceCount = 0;
  80. // space2top[y][x] = 0; // 初期値のまんま
  81. if (y > 0) {
  82. space2topCount[tempSP2Ty1[x]]++;
  83. if (tempSP2Ty1[x] > maxSpace2topLen)
  84. {
  85. maxSpace2topLen = tempSP2Ty1[x];
  86. }
  87. }
  88. }
  89. space2left[y][x] = spaceCount;
  90. } // for(x)
  91. if (spaceCount > 1)
  92. {
  93. startPointX[startPointCount] = W - 1;
  94. startPointY[startPointCount] = y;
  95. startPointCount++;
  96. space2leftCount[spaceCount]++;
  97. if (spaceCount > maxSpace2leftLen)
  98. {
  99. maxSpace2leftLen = spaceCount;
  100. }
  101. }
  102. tempSP2Ty1 = tempSP2Ty;
  103. } // for(y)
  104.  
  105. final int N = Integer.parseInt(in.readLine());
  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. int tx = x;
  133. for (int dx = 0; dx < seekLength; dx++)
  134. {
  135. if (tempSP2Ty[tx] >= s)
  136. {
  137. int dy;
  138. int ty = y;
  139. for (dy = 0; dy < s; dy++)
  140. {
  141. if (space2left[ty][tx] < t)
  142. {
  143. break;
  144. }
  145. ty--;
  146. } // for(dy)
  147. if (dy == s)
  148. {
  149. count++;
  150. }
  151. }
  152. tx--;
  153. } // for(dx)
  154. } // for(j)
  155. }
  156. else if (s != 1)
  157. {
  158. for (int j = s; j <= maxSpace2topLen; j++)
  159. {
  160. count += space2topCount[j] * (j - s + 1);
  161. }
  162. }
  163. else if (t != 1)
  164. {
  165. for (int j = t; j <= maxSpace2leftLen; j++)
  166. {
  167. count += space2leftCount[j] * (j - t + 1);
  168. }
  169. }
  170. else // if (s == 1 && t == 1)
  171. {
  172. count = allSpaceCount;
  173. }
  174. }
  175. output.append(count);
  176. output.append(NEWLINE);
  177. result[s][t] = count + 1;
  178. } // for(i)
  179.  
  180. System.out.print(output);
  181.  
  182. } // void main([String)
  183.  
  184. } // class Main
Success #stdin #stdout 0.07s 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