fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/bba797d400dc19498afbb6f047089f15
  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. Paiza.getInstance().resolve(new MyResolver());
  15. }
  16. }
  17.  
  18. class MyResolver extends Paiza.Resolver
  19. {
  20. private int[][] space2right = null;
  21. private int[][] space2bottom = null;
  22. private int[][] result = null;
  23. private int[] maxSpace2right = null;
  24. private int[] maxSpace2bottom = null;
  25.  
  26. @Override
  27. public void setHome(Paiza.Home home) {
  28. super.setHome(home);
  29.  
  30. space2right = new int[home.getH()][home.getW()];
  31. maxSpace2right = new int[home.getH()];
  32. int allSpaceCount = 0;
  33.  
  34. for (int y = 0; y < home.getH(); y++) {
  35. int count = 0, max = 0;
  36. for (int x = home.getW() - 1; x >= 0; x--) {
  37. if (home.isSpace(x, y)) {
  38. count++;
  39. allSpaceCount++;
  40. if (count > max) {
  41. max = count;
  42. }
  43. } else {
  44. count = 0;
  45. }
  46. space2right[y][x] = count;
  47. }
  48. maxSpace2right[y] = max;
  49. }
  50.  
  51. space2bottom = new int[home.getH()][home.getW()];
  52. maxSpace2bottom = new int[home.getW()];
  53.  
  54. for (int x = 0; x < home.getW(); x++) {
  55. int count = 0, max = 0;
  56. for (int y = home.getH() - 1; y >= 0; y--) {
  57. if (home.isSpace(x, y)) {
  58. count++;
  59. if (count > max) {
  60. max = count;
  61. }
  62. } else {
  63. count = 0;
  64. }
  65. space2bottom[y][x] = count;
  66. }
  67. maxSpace2bottom[x] = max;
  68. }
  69.  
  70. result = new int[301][301];
  71. result[1][1] = allSpaceCount + 1;
  72.  
  73. }
  74.  
  75. @Override
  76. public int resolve(Paiza.Widget widget) {
  77. if (result[widget.getS()][widget.getT()] > 0) {
  78. return result[widget.getS()][widget.getT()] - 1;
  79. }
  80. int count;
  81. if (widget.getS() > widget.getT()) {
  82. count = resolve2bottom(widget);
  83. } else {
  84. count = resolve2right(widget);
  85. }
  86. result[widget.getS()][widget.getT()] = count + 1;
  87. return count;
  88. }
  89.  
  90. private int resolve2right(Paiza.Widget widget) {
  91. int count = 0;
  92. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  93. if (maxSpace2right[y] < widget.getT()) {
  94. continue;
  95. }
  96. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  97. if (space2bottom[y][x] >= widget.getS()) {
  98. boolean placeable = true;
  99. for (int dy = 0; placeable && dy < widget.getS(); dy++) {
  100. if (space2right[y + dy][x] < widget.getT()) {
  101. placeable = false;
  102. x += space2right[y + dy][x];
  103. }
  104. }
  105. if (placeable) {
  106. count++;
  107. }
  108. } else if (space2right[y][x] < widget.getT()) {
  109. x += space2right[y][x];
  110. }
  111. }
  112. }
  113. return count;
  114. }
  115.  
  116. private int resolve2bottom(Paiza.Widget widget) {
  117. int count = 0;
  118. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  119. if (maxSpace2bottom[x] < widget.getS()) {
  120. continue;
  121. }
  122. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  123. if (space2right[y][x] >= widget.getT()) {
  124. boolean placeable = true;
  125. for (int dx = 0; placeable && dx < widget.getT(); dx++) {
  126. if (space2bottom[y][x + dx] < widget.getS()) {
  127. placeable = false;
  128. y += space2bottom[y][x + dx];
  129. }
  130. }
  131. if (placeable) {
  132. count++;
  133. }
  134. } else if (space2bottom[y][x] < widget.getS()) {
  135. y += space2bottom[y][x];
  136. }
  137. }
  138. }
  139. return count;
  140. }
  141.  
  142. }
  143.  
  144. final class Paiza
  145. {
  146. public static abstract class Resolver
  147. {
  148. protected Paiza.Home home;
  149.  
  150. public void setHome(Paiza.Home home) {
  151. this.home = home;
  152. }
  153.  
  154. public abstract int resolve(Paiza.Widget widget);
  155. }
  156.  
  157. public static Paiza getInstance() throws java.lang.Exception {
  158. if (paiza == null) {
  159. paiza = new Paiza();
  160. }
  161. return paiza;
  162. }
  163.  
  164. public void resolve(Resolver resolver) {
  165. init(resolver);
  166. for (Widget widget : widgets) {
  167. answer(resolver.resolve(widget));
  168. }
  169. flush();
  170. }
  171.  
  172.  
  173. public final class Home
  174. {
  175. private final int H;
  176. private final int W;
  177. private int[][] display;
  178. private Home(int H, int W) {
  179. this.H = H;
  180. this.W = W;
  181. display = new int[H][W];
  182. }
  183.  
  184. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  185. if (x < 0 || x >= W) {
  186. throw new ArrayIndexOutOfBoundsException("x : " + x);
  187. }
  188. if (y < 0 || y >= H) {
  189. throw new ArrayIndexOutOfBoundsException("y : " + y);
  190. }
  191. if (e != 0 && e != 1) {
  192. throw new IllegalArgumentException("e");
  193. }
  194. display[y][x] = e;
  195. }
  196.  
  197. public int getH() {
  198. return H;
  199. }
  200.  
  201. public int getW() {
  202. return W;
  203. }
  204.  
  205. public boolean isSpace(int x, int y) {
  206. if (x < 0 || y < 0 || x >= W || y >= H) {
  207. return false;
  208. }
  209. return display[y][x] == 0;
  210. }
  211. }
  212.  
  213. public final class Widget
  214. {
  215. private final int s;
  216. private final int t;
  217. private Widget(int s, int t) {
  218. this.s = s;
  219. this.t = t;
  220. }
  221.  
  222. public int getS() {
  223. return s;
  224. }
  225.  
  226. public int getT() {
  227. return t;
  228. }
  229. }
  230.  
  231. private static Paiza paiza = null;
  232.  
  233. private Home home;
  234. private ArrayList<Widget> widgets;
  235.  
  236. private Paiza() throws java.lang.Exception {
  237. String[] hw = in.readLine().split(" ");
  238. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  239. for (int y = 0; y < home.getH(); y++) {
  240. String line = in.readLine();
  241. for (int x = 0; x < home.getW(); x++) {
  242. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  243. }
  244. }
  245. int N = Integer.parseInt(in.readLine());
  246. widgets = new ArrayList<Widget>(N);
  247. for (int i = 0; i< N; i++) {
  248. String[] st = in.readLine().split(" ");
  249. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  250. }
  251. }
  252.  
  253. private StringBuilder output = null;
  254. private static final String NEWLINE = System.getProperty("line.separator");
  255.  
  256. private void init(Resolver resolver) {
  257. resolver.setHome(home);
  258. output = new StringBuilder(widgets.size() * 6);
  259. }
  260.  
  261. private void answer(int count) {
  262. output.append(count);
  263. output.append(NEWLINE);
  264. }
  265.  
  266. private void flush() {
  267. System.out.print(output);
  268. }
  269. }
  270.  
Success #stdin #stdout 0.08s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2