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