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