fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/98a4943f50d69fca62009a3e32cd5003
  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[32768];
  13. static int[] tb = new int[32768];
  14. static int[] ca = new int[32768];
  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 * 301 + x] = count;
  46. }
  47. }
  48.  
  49. j = 0;
  50. for (y = 0; y < H; y++)
  51. {
  52. for (x = 0; x < W; x++)
  53. {
  54. if (space2right[j + x] == 0)
  55. {
  56. continue;
  57. }
  58. s = 301;
  59. t = space2right[j + x];
  60. for (i = y; i < H && space2right[i * 301 + x] > 0; i++)
  61. {
  62. if (space2right[i * 301 + x] < t)
  63. {
  64. t = space2right[i * 301 + x];
  65. }
  66. table[s + t]++;
  67. s += 301;
  68. }
  69. }
  70. j += 301;
  71. }
  72.  
  73. int N = Integer.parseInt(in.readLine()); // ウィジェット数
  74. StringBuilder output = new StringBuilder(N * 6);
  75.  
  76. for (i = 0; i < N; i++)
  77. {
  78. hw = in.readLine().split(" ");
  79. s = Integer.parseInt(hw[0]); // ウィジェットの縦サイズ
  80. t = Integer.parseInt(hw[1]); // ウィジェットの横サイズ
  81.  
  82. count = 0;
  83. if (s <= H && t <= W)
  84. {
  85. y = s * 301;
  86. x = y + t;
  87. if (cache[x] > 0)
  88. {
  89. count = cache[x] - 1;
  90. }
  91. else
  92. {
  93. for (j = t; j <= W; j++)
  94. {
  95. count += table[y + j];
  96. }
  97. cache[x] = count + 1;
  98. }
  99. }
  100.  
  101. output.append(count);
  102. output.append('\n');
  103. }
  104.  
  105. System.out.print(output);
  106.  
  107. } // end of main(String[])
  108.  
  109. }
  110.  
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