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