fork(3) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/4873b83843353ec38dd4283391488ae2
  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.  
  21. int[][] table = null;
  22. int[][] cache = null;
  23.  
  24. @Override
  25. public void setHome(Paiza.Home home) {
  26. super.setHome(home);
  27.  
  28. table = new int[home.getH() + 1][home.getW() + 1];
  29. cache = new int[home.getH() + 1][home.getW() + 1];
  30.  
  31. int[][] space2right = new int[home.getH()][home.getW()];
  32.  
  33. for (int y = 0; y < home.getH(); y++) {
  34. int count = 0;
  35. for (int x = home.getW() - 1; x >= 0; x--) {
  36. if (home.isSpace(x, y)) {
  37. count++;
  38. } else {
  39.  
  40. count = 0;
  41. }
  42. space2right[y][x] = count;
  43. }
  44. }
  45.  
  46. for (int y = 0; y < home.getH(); y++) {
  47. for (int x = 0; x < home.getW(); x++) {
  48. if (space2right[y][x] == 0) {
  49. continue;
  50. }
  51. int min = space2right[y][x];
  52. int d = 1;
  53. for (int ty = y; ty < home.getH() && space2right[ty][x] > 0; ty++) {
  54. if (space2right[ty][x] < min) {
  55. min = space2right[ty][x];
  56. }
  57. table[d][min]++;
  58. d++;
  59. }
  60. }
  61. }
  62.  
  63. } // setHome()
  64.  
  65. @Override
  66. public int resolve(Paiza.Widget widget) {
  67. int s = widget.getS();
  68. int t = widget.getT();
  69. if (s > home.getH() || t > home.getW()) {
  70. return 0;
  71. }
  72. if (cache[s][t] > 0) {
  73. return cache[s][t] - 1;
  74. }
  75. int count = 0;
  76. for (int i = t; i <= home.getW(); i++) {
  77. count += table[s][i];
  78. }
  79. cache[s][t] = count + 1;
  80. return count;
  81. } // resolve()
  82.  
  83. }
  84.  
  85. final class Paiza
  86. {
  87. public static abstract class Resolver
  88. {
  89. protected Paiza.Home home;
  90.  
  91. public void setHome(Paiza.Home home) {
  92. this.home = home;
  93. }
  94.  
  95. public abstract int resolve(Paiza.Widget widget);
  96. }
  97.  
  98. public static Paiza getInstance() throws java.lang.Exception {
  99. if (paiza == null) {
  100. paiza = new Paiza();
  101. }
  102. return paiza;
  103. }
  104.  
  105. public void resolve(Resolver resolver) {
  106. init(resolver);
  107. for (Widget widget : widgets) {
  108. answer(resolver.resolve(widget));
  109. }
  110. flush();
  111. }
  112.  
  113.  
  114. public final class Home
  115. {
  116. private final int H;
  117. private final int W;
  118. private int[][] display;
  119. private Home(int H, int W) {
  120. this.H = H;
  121. this.W = W;
  122. display = new int[H][W];
  123. }
  124.  
  125. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  126. if (x < 0 || x >= W) {
  127. throw new ArrayIndexOutOfBoundsException("x : " + x);
  128. }
  129. if (y < 0 || y >= H) {
  130. throw new ArrayIndexOutOfBoundsException("y : " + y);
  131. }
  132. if (e != 0 && e != 1) {
  133. throw new IllegalArgumentException("e");
  134. }
  135. display[y][x] = e;
  136. }
  137.  
  138. public int getH() {
  139. return H;
  140. }
  141.  
  142. public int getW() {
  143. return W;
  144. }
  145.  
  146. public boolean isSpace(int x, int y) {
  147. if (x < 0 || y < 0 || x >= W || y >= H) {
  148. return false;
  149. }
  150. return display[y][x] == 0;
  151. }
  152. }
  153.  
  154. public final class Widget
  155. {
  156. private final int s;
  157. private final int t;
  158. private Widget(int s, int t) {
  159. this.s = s;
  160. this.t = t;
  161. }
  162.  
  163. public int getS() {
  164. return s;
  165. }
  166.  
  167. public int getT() {
  168. return t;
  169. }
  170. }
  171.  
  172. private static Paiza paiza = null;
  173.  
  174. private Home home;
  175. private ArrayList<Widget> widgets;
  176.  
  177. private Paiza() throws java.lang.Exception {
  178. String[] hw = in.readLine().split(" ");
  179. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  180. for (int y = 0; y < home.getH(); y++) {
  181. String line = in.readLine();
  182. for (int x = 0; x < home.getW(); x++) {
  183. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  184. }
  185. }
  186. int N = Integer.parseInt(in.readLine());
  187. widgets = new ArrayList<Widget>(N);
  188. for (int i = 0; i< N; i++) {
  189. String[] st = in.readLine().split(" ");
  190. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  191. }
  192. }
  193.  
  194. private StringBuilder output = null;
  195. private static final String NEWLINE = System.getProperty("line.separator");
  196.  
  197. private void init(Resolver resolver) {
  198. resolver.setHome(home);
  199. output = new StringBuilder(widgets.size() * 6);
  200. }
  201.  
  202. private void answer(int count) {
  203. output.append(count);
  204. output.append(NEWLINE);
  205. }
  206.  
  207. private void flush() {
  208. System.out.print(output);
  209. }
  210. }
  211.  
Success #stdin #stdout 0.08s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2