fork download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/b58342777dadca0419d0d9af4c7bef64
  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. int all = 0;
  33.  
  34. for (int y = 0; y < home.getH(); y++) {
  35. int count = 0;
  36. for (int x = home.getW() - 1; x >= 0; x--) {
  37. if (home.isSpace(x, y)) {
  38. all++;
  39. count++;
  40. space2right[y][x] = count;
  41. int s = 1;
  42. int t = count;
  43. for (int i = y; i >= 0 && space2right[i][x] > 0; i--) {
  44. if (space2right[i][x] < t) {
  45. t = space2right[i][x];
  46. }
  47. table[s][t]++;
  48. s++;
  49. }
  50. } else {
  51. count = 0;
  52. }
  53. }
  54. }
  55.  
  56. cache[1][1] = all + 1;
  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 = t; i <= home.getW(); i++) {
  71. count += table[s][i];
  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) {
  120. display[y][x] = e;
  121. }
  122.  
  123. public int getH() {
  124. return H;
  125. }
  126.  
  127. public int getW() {
  128. return W;
  129. }
  130.  
  131. public boolean isSpace(int x, int y) {
  132. return display[y][x] == 0;
  133. }
  134. }
  135.  
  136. public final class Widget
  137. {
  138. private final int s;
  139. private final int t;
  140. private Widget(int s, int t) {
  141. this.s = s;
  142. this.t = t;
  143. }
  144.  
  145. public int getS() {
  146. return s;
  147. }
  148.  
  149. public int getT() {
  150. return t;
  151. }
  152. }
  153.  
  154. private static Paiza paiza = null;
  155.  
  156. private Home home;
  157. private ArrayList<Widget> widgets;
  158.  
  159. private Paiza() throws java.lang.Exception {
  160. String[] hw = in.readLine().split(" ");
  161. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  162. for (int y = 0; y < home.getH(); y++) {
  163. String line = in.readLine();
  164. for (int x = 0; x < home.getW(); x++) {
  165. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  166. }
  167. }
  168. int N = Integer.parseInt(in.readLine());
  169. widgets = new ArrayList<Widget>(N);
  170. for (int i = 0; i< N; i++) {
  171. String[] st = in.readLine().split(" ");
  172. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  173. }
  174. }
  175.  
  176. private StringBuilder output = null;
  177. private static final String NEWLINE = System.getProperty("line.separator");
  178.  
  179. private void init(Resolver resolver) {
  180. resolver.setHome(home);
  181. output = new StringBuilder(widgets.size() * 6);
  182. }
  183.  
  184. private void answer(int count) {
  185. output.append(count);
  186. output.append(NEWLINE);
  187. }
  188.  
  189. private void flush() {
  190. System.out.print(output);
  191. }
  192. }
  193.  
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