fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/887841fd8edbf1307d9e64576fb2ed96
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12. static int[][] sp = new int[301][301];
  13. static int[][] tb = new int[301][301];
  14. static int[][] ca = new int[301][301];
  15.  
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18.  
  19. String[] hw = in.readLine().split(" ");
  20. int H = Integer.parseInt(hw[0]); // ホーム画面縦の区画数
  21. int W = Integer.parseInt(hw[1]); // ホーム画面横の区画数
  22.  
  23. int[][] space2right = sp;
  24. int[][] table = tb;
  25. int[][] cache = ca;
  26.  
  27. int y, x, j ,i, s, t;
  28. String line;
  29. int count;
  30.  
  31. for (y = 0; y < H; y++)
  32. {
  33. line = in.readLine();
  34. count = 0;
  35. for (x = W - 1; x >= 0; x--)
  36. {
  37. if (line.charAt(x) == '0')
  38. {
  39. count++;
  40. }
  41. else
  42. {
  43. count = 0;
  44. }
  45. space2right[y][x] = count;
  46. }
  47. }
  48.  
  49. for (y = 0; y < H; y++)
  50. {
  51. for (x = 0; x < W; x++)
  52. {
  53. if (space2right[y][x] == 0)
  54. {
  55. continue;
  56. }
  57. s = 1;
  58. t = space2right[y][x];
  59. for (i = y; i < H && space2right[i][x] > 0; i++)
  60. {
  61. if (space2right[i][x] < t)
  62. {
  63. t = space2right[i][x];
  64. }
  65. table[s][t]++;
  66. s++;
  67. }
  68. }
  69. }
  70.  
  71. int N = Integer.parseInt(in.readLine()); // ウィジェット数
  72. StringBuilder output = new StringBuilder(N * 6);
  73.  
  74. for (i = 0; i < N; i++)
  75. {
  76. hw = in.readLine().split(" ");
  77. s = Integer.parseInt(hw[0]); // ウィジェットの縦サイズ
  78. t = Integer.parseInt(hw[1]); // ウィジェットの横サイズ
  79.  
  80. count = 0;
  81. if (s <= H && t <= W)
  82. {
  83. if (cache[s][t] > 0)
  84. {
  85. count = cache[s][t] - 1;
  86. }
  87. else
  88. {
  89. for (j = t; j <= W; j++)
  90. {
  91. count += table[s][j];
  92. }
  93. cache[s][t] = count + 1;
  94. }
  95. }
  96.  
  97. output.append(count);
  98. output.append('\n');
  99. }
  100.  
  101. System.out.print(output);
  102.  
  103. } // end of main(String[])
  104.  
  105. }
  106.  
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