fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/2205173e25de67eb8b181dc573a577f9
  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. for (int y = 0; y < H; y++)
  35. {
  36. String line = in.readLine();
  37. int spaceCount = 0;
  38. for (int x = 0; x < W; x++)
  39. {
  40. char ch = line.charAt(x);
  41. if (ch == '0') // 空
  42. {
  43. spaceCount++;
  44. allSpaceCount++;
  45. if (y > 0)
  46. {
  47. space2top[y][x] = space2top[y - 1][x] + 1;
  48. if (y == H - 1 && space2top[y][x] > 1)
  49. {
  50. space2topCount[space2top[y][x]]++;
  51. if (space2top[y][x] > maxSpace2topLen)
  52. {
  53. maxSpace2topLen = space2top[y][x];
  54. }
  55. }
  56. }
  57. else // if (y == 0)
  58. {
  59. space2top[y][x] = 1;
  60. }
  61. }
  62. else // if (ch == '1') // 埋
  63. {
  64. if (spaceCount > 1)
  65. {
  66. startPointX[startPointCount] = x - 1;
  67. startPointY[startPointCount] = y;
  68. startPointCount++;
  69. space2leftCount[spaceCount]++;
  70. if (spaceCount > maxSpace2leftLen)
  71. {
  72. maxSpace2leftLen = spaceCount;
  73. }
  74. }
  75. spaceCount = 0;
  76. // space2top[y][x] = 0; // 初期値のまんま
  77. if (y > 0) {
  78. space2topCount[space2top[y - 1][x]]++;
  79. if (space2top[y - 1][x] > maxSpace2topLen)
  80. {
  81. maxSpace2topLen = space2top[y - 1][x];
  82. }
  83. }
  84. }
  85. space2left[y][x] = spaceCount;
  86. } // for(x)
  87. if (spaceCount > 1)
  88. {
  89. startPointX[startPointCount] = W - 1;
  90. startPointY[startPointCount] = y;
  91. startPointCount++;
  92. space2leftCount[spaceCount]++;
  93. if (spaceCount > maxSpace2leftLen)
  94. {
  95. maxSpace2leftLen = spaceCount;
  96. }
  97. }
  98. } // for(y)
  99.  
  100. final int N = Integer.parseInt(in.readLine());
  101. int[][] result = new int[301][301];
  102.  
  103. StringBuilder output = new StringBuilder(N * (1 + NEWLINE.length()));
  104.  
  105. for (int i = 0; i < N; i++)
  106. {
  107. String[] st = in.readLine().split(" ", 2);
  108. int s = Integer.parseInt(st[0]);
  109. int t = Integer.parseInt(st[1]);
  110.  
  111. int count = 0;
  112.  
  113. if (s <= H && t <= W)
  114. {
  115. if (result[s][t] > 0)
  116. {
  117. count = result[s][t] - 1;
  118. }
  119. else if (s != 1 && t != 1)
  120. {
  121. for (int j = 0; j < startPointCount; j++)
  122. {
  123. int x = startPointX[j];
  124. int y = startPointY[j];
  125. int seekLength = space2left[y][x] - t + 1;
  126.  
  127. for (int dx = 0; dx < seekLength; dx++)
  128. {
  129. int tx = x - dx;
  130. if (space2top[y][tx] < s)
  131. {
  132. continue;
  133. }
  134. int dy;
  135. for (dy = 0; dy < s; dy++)
  136. {
  137. if (space2left[y - dy][tx] < t)
  138. {
  139. break;
  140. }
  141. } // for(dy)
  142. if (dy == s)
  143. {
  144. count++;
  145. }
  146. } // for(dx)
  147. } // for(j)
  148. }
  149. else if (s != 1)
  150. {
  151. for (int j = s; j <= maxSpace2topLen; j++)
  152. {
  153. count += space2topCount[j] * (j - s + 1);
  154. }
  155. }
  156. else if (t != 1)
  157. {
  158. for (int j = t; j <= maxSpace2leftLen; j++)
  159. {
  160. count += space2leftCount[j] * (j - t + 1);
  161. }
  162. }
  163. else // if (s == 1 && t == 1)
  164. {
  165. count = allSpaceCount;
  166. }
  167. }
  168. output.append(count);
  169. output.append(NEWLINE);
  170. result[s][t] = count + 1;
  171. } // for(i)
  172.  
  173. System.out.print(output);
  174.  
  175. } // void main([String)
  176.  
  177. } // class Main
Success #stdin #stdout 0.07s 380224KB
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