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