fork(2) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/a34a075d70f7f8f450de3a0252049f32
  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 spacecount = 0;
  22.  
  23. List<Position> startposition2left = new ArrayList<Position>(H * W);
  24. List<Position> startposition2top = new ArrayList<Position>(H * W);
  25.  
  26. for (int y = 0; y < H; y++)
  27. {
  28. String line = in.readLine();
  29. int space2leftSize = 0;
  30. for (int x = 0; x < W; x++)
  31. {
  32. char ch = line.charAt(x);
  33. if (ch == '1')
  34. {
  35. space2leftSize = 0;
  36. space2top[y][x] = 0;
  37. if (x > 0 && space2left[y][x - 1] > 0)
  38. {
  39. startposition2left.add(
  40. Position.getPosition(y, x - 1));
  41. }
  42. if (y > 0 && space2top[y - 1][x] > 0)
  43. {
  44. startposition2top.add(
  45. Position.getPosition(y - 1, x));
  46. }
  47. }
  48. else // if (ch == '0')
  49. {
  50. spacecount++;
  51. space2leftSize++;
  52. if (y > 0)
  53. {
  54. space2top[y][x] = space2top[y - 1][x] + 1;
  55. if (y == H - 1)
  56. {
  57. startposition2top.add(
  58. Position.getPosition(y, x));
  59. }
  60. }
  61. else // if (y == 0)
  62. {
  63. space2top[y][x] = 1;
  64. }
  65. if (x == W - 1)
  66. {
  67. startposition2left.add(
  68. Position.getPosition(y, x));
  69. }
  70. }
  71. space2left[y][x] = space2leftSize;
  72. }
  73. }
  74. // print2dArray(space2left);
  75. // print2dArray(space2top);
  76. // System.out.println(startposition2left);
  77. // System.out.println(startposition2top);
  78.  
  79. final int N = Integer.valueOf(in.readLine()); // ウィジェット数
  80. HashSet<Position> placeablePosition = new HashSet<Position>(H * W);
  81. Map<Position, Integer> result = new HashMap<Position, Integer>(N);
  82.  
  83. for (int i = 0; i < N; i++)
  84. {
  85. String[] st = in.readLine().split(" ");
  86. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  87. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  88. Position key = Position.getPosition(s, t);
  89. Integer value;
  90. if ((value = result.get(key)) != null)
  91. {
  92. System.out.println(value.intValue());
  93. continue;
  94. }
  95. int placeableCount = 0;
  96.  
  97. if (s == 1) // 横長のウィジェット
  98. {
  99. if (t == 1) // 1x1のウィジェット
  100. {
  101. placeableCount = spacecount;
  102. }
  103. else // if (t != 1) // 横長のウィジェット
  104. {
  105. for (Position position : startposition2left)
  106. {
  107. int spaceWSize = space2left[position.y][position.x];
  108. if (spaceWSize >= t)
  109. {
  110. placeableCount += spaceWSize - t + 1;
  111. }
  112. }
  113. }
  114. }
  115. else if (t == 1) // 縦長のウィジェット
  116. {
  117. for (Position position : startposition2top)
  118. {
  119. int spaceHSize = space2top[position.y][position.x];
  120. if (spaceHSize >= s)
  121. {
  122. placeableCount += spaceHSize - s + 1;
  123. }
  124. }
  125. }
  126. else // if (s != 1 && t != 1)
  127. {
  128. placeablePosition.clear();
  129. // 横に走査していく
  130. for (Position position : startposition2left)
  131. {
  132. int spaceWSize = space2left[position.y][position.x];
  133. for (int dx = 0; dx < spaceWSize; ++dx)
  134. {
  135. int x = position.x - dx;
  136. int spaceHSize = space2top[position.y][x];
  137. if (spaceHSize < s)
  138. {
  139. continue; // 縦サイズが足りない
  140. }
  141. // 縦に走査して横サイズ以上の連続する空所を数える
  142. int count = 0;
  143. for (int dy = 0; dy < spaceHSize; ++dy)
  144. {
  145. int y = position.y - dy;
  146. if (space2left[y][x] >= t)
  147. {
  148. count++;
  149. if (count >= s) // ウィジェットが確保できる
  150. {
  151. boolean placed = placeablePosition.add(
  152. Position.getPosition(y, x));
  153. if (placed == false)
  154. {
  155. break; // これ以上の縦の走査は走査済み
  156. }
  157. }
  158. }
  159. else
  160. {
  161. count = 0; // 連続の途切れ
  162. }
  163. }
  164. }
  165. }
  166. placeableCount = placeablePosition.size();
  167. }
  168. System.out.println(placeableCount);
  169. result.put(key, Integer.valueOf(placeableCount));
  170. }
  171.  
  172. } // end of main(String[])
  173.  
  174. static void print2dArray(int[][] array)
  175. {
  176. for (int i = 0; i < array.length; i++)
  177. {
  178. for (int j = 0; j < array[i].length; j++)
  179. {
  180. System.out.print(array[i][j] + " ");
  181. }
  182. System.out.println();
  183. }
  184. System.out.println();
  185. }
  186.  
  187. static class Position
  188. {
  189. static Position[] pool = new Position[90000];
  190. static Position getPosition(int y, int x)
  191. {
  192. int index = y * 300 + x;
  193. if (pool[index] == null)
  194. {
  195. pool[index] = new Position(y, x);
  196. }
  197. return pool[index];
  198. }
  199.  
  200. final int y;
  201. final int x;
  202. private Position(int y, int x)
  203. {
  204. this.y = y;
  205. this.x = x;
  206. }
  207.  
  208. @Override public String toString()
  209. {
  210. return "(" + x + ", " + y + ")";
  211. }
  212.  
  213. @Override public int hashCode()
  214. {
  215. return y ^ x;
  216. }
  217.  
  218. @Override public boolean equals(Object obj)
  219. {
  220. Position pos = (Position)obj;
  221. return y == pos.y && x == pos.x;
  222. }
  223. } // end of class Position
  224. }
  225.  
Success #stdin #stdout 0.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2