fork(1) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/1d1a1406bbcad854db3a4d4bcc25f5ae
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12. static byte[] buf = new byte[900000];
  13. static int ptr = 0;
  14.  
  15. static int getInt() throws java.lang.Exception
  16. {
  17. int v = 0;
  18. char ch;
  19. do
  20. {
  21. ch = (char)buf[ptr++];
  22. } while (ch < '0' || ch > '9');
  23. do
  24. {
  25. v = 10 * v + (int)(ch - '0');
  26. ch = (char)buf[ptr++];
  27. } while (ch >= '0' && ch <= '9');
  28. return v;
  29. }
  30.  
  31. static char getChar()
  32. {
  33. char ch;
  34. do
  35. {
  36. ch = (char)buf[ptr++];
  37. } while (ch < '0' || ch > '9');
  38. return ch;
  39. }
  40.  
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. System.in.read(buf, 0, buf.length);
  44.  
  45. final int H = getInt();
  46. final int W = getInt();
  47. final String NEWLINE = System.getProperty("line.separator");
  48.  
  49. int[][] space2left = new int[H][W];
  50. int[][] space2top = new int[H][W];
  51. int allSpaceCount = 0;
  52.  
  53. int[] startPointX = new int[H * (1 + W / 3)];
  54. int[] startPointY = new int[H * (1 + W / 3)];
  55. int startPointCount = 0;
  56.  
  57. int[] space2leftCount = new int[W + 1];
  58. int[] space2topCount = new int[H + 1];
  59.  
  60. for (int y = 0; y < H; y++)
  61. {
  62. int spaceCount = 0;
  63. for (int x = 0; x < W; x++)
  64. {
  65. char ch = getChar();
  66. if (ch == '0') // 空
  67. {
  68. spaceCount++;
  69. allSpaceCount++;
  70. if (y > 0)
  71. {
  72. space2top[y][x] = space2top[y - 1][x] + 1;
  73. if (y == H - 1 && space2top[y][x] > 1)
  74. {
  75. space2topCount[space2top[y][x]]++;
  76. }
  77. }
  78. else // if (y == 0)
  79. {
  80. space2top[y][x] = 1;
  81. }
  82. }
  83. else // if (ch == '1') // 埋
  84. {
  85. if (spaceCount > 1)
  86. {
  87. startPointX[startPointCount] = x - 1;
  88. startPointY[startPointCount] = y;
  89. startPointCount++;
  90. space2leftCount[spaceCount]++;
  91. }
  92. spaceCount = 0;
  93. // space2top[y][x] = 0; // 初期値のまんま
  94. if (y > 0) {
  95. space2topCount[space2top[y - 1][x]]++;
  96. }
  97. }
  98. space2left[y][x] = spaceCount;
  99. } // for(x)
  100. if (spaceCount > 1)
  101. {
  102. startPointX[startPointCount] = W - 1;
  103. startPointY[startPointCount] = y;
  104. startPointCount++;
  105. space2leftCount[spaceCount]++;
  106. }
  107. } // for(y)
  108.  
  109. final int N = getInt();
  110. int[][] result = new int[301][301];
  111.  
  112. StringBuilder output = new StringBuilder(N * (1 + NEWLINE.length()));
  113.  
  114. for (int i = 0; i < N; i++)
  115. {
  116. int s = getInt();
  117. int t = getInt();
  118.  
  119. int count = 0;
  120.  
  121. if (s <= H && t <= W)
  122. {
  123. if (result[s][t] > 0)
  124. {
  125. count = result[s][t] - 1;
  126. }
  127. else if (s != 1 && t != 1)
  128. {
  129. for (int j = 0; j < startPointCount; j++)
  130. {
  131. int x = startPointX[j];
  132. int y = startPointY[j];
  133. int seekLength = space2left[y][x] - t + 1;
  134.  
  135. for (int dx = 0; dx < seekLength; dx++)
  136. {
  137. int tx = x - dx;
  138. if (space2top[y][tx] < s)
  139. {
  140. continue;
  141. }
  142. int dy;
  143. for (dy = 0; dy < s; dy++)
  144. {
  145. if (space2left[y - dy][tx] < t)
  146. {
  147. break;
  148. }
  149. } // for(dy)
  150. if (dy == s)
  151. {
  152. count++;
  153. }
  154. } // for(dx)
  155. } // for(j)
  156. }
  157. else if (s != 1)
  158. {
  159. for (int j = s; j <= H; j++)
  160. {
  161. count += space2topCount[j] * (j - s + 1);
  162. }
  163. }
  164. else if (t != 1)
  165. {
  166. for (int j = t; j <= W; j++)
  167. {
  168. count += space2leftCount[j] * (j - t + 1);
  169. }
  170. }
  171. else // if (s == 1 && t == 1)
  172. {
  173. count = allSpaceCount;
  174. }
  175. }
  176. output.append(count);
  177. output.append(NEWLINE);
  178. result[s][t] = count + 1;
  179. } // for(i)
  180.  
  181. System.out.print(output);
  182.  
  183. } // void main([String)
  184.  
  185. } // class Main
Success #stdin #stdout 0.06s 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