fork download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/15a9530dcae0cc55c1604f0b645b8f03
  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.  
  15. String[] hw = in.readLine().split(" ");
  16. final int H = Integer.parseInt(hw[0]);
  17. final int W = Integer.parseInt(hw[1]);
  18.  
  19. int[][] space2left = new int[H][W];
  20. int[][] space2top = new int[H][W];
  21. int allSpaceCount = 0;
  22.  
  23. int[] startPointX = new int[H * (1 + W / 3)];
  24. int[] startPointY = new int[H * (1 + W / 3)];
  25. int startPointCount = 0;
  26.  
  27. int[] space2leftCount = new int[W + 1];
  28. int[] space2topCount = new int[H + 1];
  29.  
  30. for (int y = 0; y < H; y++)
  31. {
  32. String line = in.readLine();
  33. int spaceCount = 0;
  34. for (int x = 0; x < W; x++)
  35. {
  36. char ch = line.charAt(x);
  37. if (ch == '0') // 空
  38. {
  39. spaceCount++;
  40. allSpaceCount++;
  41. if (y > 0)
  42. {
  43. space2top[y][x] = space2top[y - 1][x] + 1;
  44. }
  45. else // if (y == 0)
  46. {
  47. space2top[y][x] = 1;
  48. }
  49. }
  50. else // if (ch == '1') // 埋
  51. {
  52. if (spaceCount > 1)
  53. {
  54. startPointX[startPointCount] = x - 1;
  55. startPointY[startPointCount] = y;
  56. startPointCount++;
  57. space2leftCount[spaceCount]++;
  58. }
  59. spaceCount = 0;
  60. space2top[y][x] = 0;
  61. if (y > 0 && space2top[y - 1][x] > 1)
  62. {
  63. space2topCount[space2top[y - 1][x]]++;
  64. }
  65. }
  66. space2left[y][x] = spaceCount;
  67. } // for(x)
  68. if (spaceCount > 1)
  69. {
  70. startPointX[startPointCount] = W - 1;
  71. startPointY[startPointCount] = y;
  72. startPointCount++;
  73. space2leftCount[spaceCount]++;
  74. }
  75. } // for(y)
  76. for (int x = 0, y = H - 1; x < W; x++)
  77. {
  78. if (space2top[y][x] > 1)
  79. {
  80. space2topCount[space2top[y][x]]++;
  81. }
  82. } // for(x)
  83.  
  84. final int N = Integer.parseInt(in.readLine());
  85. int[][] result = new int[H + 1][W + 1];
  86.  
  87. for (int i = 0; i < N; i++)
  88. {
  89. String[] st = in.readLine().split(" ");
  90. int s = Integer.parseInt(st[0]);
  91. int t = Integer.parseInt(st[1]);
  92.  
  93. if (s > H || t > W)
  94. {
  95. System.out.println("0");
  96. continue;
  97. }
  98.  
  99. if (result[s][t] > 0)
  100. {
  101. System.out.println(result[s][t] - 1);
  102. continue;
  103. }
  104.  
  105. int count = 0;
  106.  
  107. if (s != 1 && t != 1)
  108. {
  109. for (int j = 0; j < startPointCount; j++)
  110. {
  111. int x = startPointX[j];
  112. int y = startPointY[j];
  113. int seekLength = space2left[y][x] - t + 1;
  114.  
  115. for (int dx = 0; dx < seekLength; dx++)
  116. {
  117. int tx = x - dx;
  118. if (space2top[y][tx] < s)
  119. {
  120. continue;
  121. }
  122. int dy;
  123. for (dy = 0; dy < s; dy++)
  124. {
  125. if (space2left[y - dy][tx] < t)
  126. {
  127. break;
  128. }
  129. } // for(dy)
  130. if (dy == s)
  131. {
  132. count++;
  133. }
  134. } // for(dx)
  135. } // for(j)
  136. }
  137. else if (s != 1)
  138. {
  139. for (int j = s; j <= H; j++)
  140. {
  141. count += space2topCount[j] * (j - s + 1);
  142. }
  143. }
  144. else if (t != 1)
  145. {
  146. for (int j = t; j <= W; j++)
  147. {
  148. count += space2leftCount[j] * (j - t + 1);
  149. }
  150. }
  151. else // if (s == 1 && t == 1)
  152. {
  153. count = allSpaceCount;
  154. }
  155. System.out.println(count);
  156. result[s][t] = count + 1;
  157. } // for(i)
  158.  
  159. } // void main([String)
  160.  
  161. } // class Main
Success #stdin #stdout 0.08s 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