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