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