fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/cefef3fcaa8bfefe9e5c43d2576e49da
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12.  
  13.  
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. }
  17.  
  18. static {
  19. try {
  20.  
  21. String[] hw = in.readLine().split(" ");
  22. int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
  23. int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
  24.  
  25. int[][] result = new int[301][301];
  26.  
  27. int[][] table = new int[W + 1][H + 1];
  28.  
  29. int[] space2top = new int[W];
  30.  
  31. for (int y = 0; y < H; y++) {
  32. String line = in.readLine();
  33. for (int x = 0; x < W; x++) {
  34. if (line.charAt(x) == '0') {
  35. space2top[x]++;
  36. int s = space2top[x];
  37. int t = 1;
  38. for (int i = x; i >= 0 && space2top[i] > 0; i--) {
  39. if (space2top[i] < s) {
  40. s = space2top[i];
  41. }
  42. table[t][s]++;
  43. t++;
  44. }
  45. } else {
  46. space2top[x] = 0;
  47. }
  48. }
  49. }
  50.  
  51. for (int t = 1; t <= W; t++) {
  52. int[] temp1 = result[t];
  53. int[] temp2 = table[t];
  54. temp1[H] = temp2[H];
  55. for (int s = H - 1; s > 0; s--) {
  56. temp1[s] = temp2[s] + temp1[s + 1];
  57. }
  58. }
  59.  
  60. final int N = Integer.parseInt(in.readLine()); // ウィジェット数
  61. StringBuilder output = new StringBuilder(N * 6);
  62.  
  63. for (int i = 0; i < N; i++)
  64. {
  65. String[] st = in.readLine().split(" ");
  66. int s = Integer.parseInt(st[0]); // ウィジェットの縦サイズ
  67. int t = Integer.parseInt(st[1]); // ウィジェットの横サイズ
  68.  
  69. output.append(result[t][s]);
  70. output.append('\n');
  71. }
  72.  
  73. System.out.print(output);
  74.  
  75. } catch (Exception e) {
  76.  
  77. }
  78. } // end of main(String[])
  79. }
  80.  
Success #stdin #stdout 0.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
7
2 2
1 1
3 2
3 2
2 3
3 1
1 3
stdout
6
20
2
2
1
6
7