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