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