fork download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/3b78c0b31fec7b1a3172b8404476a150
  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.  
  33. for (int y = 0; y < home.getH(); y++) {
  34. int count = 0;
  35. for (int x = home.getW() - 1; x >= 0; x--) {
  36. if (home.isSpace(x, y)) {
  37. count++;
  38. } else {
  39.  
  40. count = 0;
  41. }
  42. space2right[y][x] = count;
  43. }
  44. }
  45.  
  46. for (int y = 0; y < home.getH(); y++) {
  47. for (int x = 0; x < home.getW(); x++) {
  48. if (space2right[y][x] == 0) {
  49. continue;
  50. }
  51. int min = space2right[y][x];
  52. int d = 1;
  53. for (int ty = y; ty < home.getH() && space2right[ty][x] > 0; ty++) {
  54. if (space2right[ty][x] < min) {
  55. min = space2right[ty][x];
  56. }
  57. table[d][min]++;
  58. d++;
  59. }
  60. }
  61. }
  62.  
  63. } // setHome()
  64.  
  65. @Override
  66. public int resolve(Paiza.Widget widget) {
  67. int s = widget.getS();
  68. int t = widget.getT();
  69. if (s > home.getH() || t > home.getW()) {
  70. return 0;
  71. }
  72. if (cache[s][t] > 0) {
  73. return cache[s][t] - 1;
  74. }
  75. int count = 0;
  76. for (int i = t; i <= home.getW(); i++) {
  77. count += table[s][i];
  78. }
  79. cache[s][t] = count + 1;
  80. return count;
  81. } // resolve()
  82.  
  83. }
  84.  
  85. final class Paiza
  86. {
  87. public static abstract class Resolver
  88. {
  89. protected Paiza.Home home;
  90.  
  91. public void setHome(Paiza.Home home) {
  92. this.home = home;
  93. }
  94.  
  95. public abstract int resolve(Paiza.Widget widget);
  96. }
  97.  
  98. public static Paiza getInstance() throws java.lang.Exception {
  99. if (paiza == null) {
  100. paiza = new Paiza();
  101. }
  102. return paiza;
  103. }
  104.  
  105. public void resolve(Resolver resolver) {
  106. init(resolver);
  107. for (Widget widget : widgets) {
  108. answer(resolver.resolve(widget));
  109. }
  110. flush();
  111. }
  112.  
  113.  
  114. public final class Home
  115. {
  116. private final int H;
  117. private final int W;
  118. private int[][] display;
  119. private Home(int H, int W) {
  120. this.H = H;
  121. this.W = W;
  122. display = new int[H][W];
  123. }
  124.  
  125. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  126. display[y][x] = e;
  127. }
  128.  
  129. public int getH() {
  130. return H;
  131. }
  132.  
  133. public int getW() {
  134. return W;
  135. }
  136.  
  137. public boolean isSpace(int x, int y) {
  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.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2