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