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