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