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