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