fork(1) download
  1. /* paiza POH!vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/fac654d9a690bae98e7944ceeddb309a
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. #include <stdio.h>
  7.  
  8. #define SIZE (2000000)
  9.  
  10. char input[SIZE];
  11. char *ptr = input;
  12.  
  13. int getInt(void) {
  14. int v = 0;
  15. while (*ptr < '0' || *ptr > '9') ++ptr;
  16. while (*ptr >= '0' && *ptr <= '9')
  17. {
  18. v = 10 * v + (int)(*ptr - '0');
  19. ++ptr;
  20. }
  21. return v;
  22. }
  23.  
  24. char getChar(void) {
  25. while (*ptr < '0' || *ptr > '9') ++ptr;
  26. return *ptr++;
  27. }
  28.  
  29. void putInt(int v) {
  30. if (v < 10) {
  31. putchar('0' + (char)v);
  32. } else {
  33. putInt(v / 10);
  34. putchar('0' + (char)(v % 10));
  35. }
  36. }
  37.  
  38. int home[310][310];
  39. int hoge[310][310];
  40.  
  41. int main(void) {
  42. int H, W, N, s, t;
  43. int x, y, c, i;
  44. int hx, hy, hxe, hye;
  45. int count, dy, dx;
  46. char ch;
  47.  
  48. fread(input, sizeof(char), SIZE, stdin);
  49. H = getInt();
  50. W = getInt();
  51.  
  52. for (y = 0; y < H; ++y) {
  53. c = 0;
  54. for (x = 0; x < W; ++x) {
  55. ch = getChar();
  56. if (ch == '0') {
  57. c++;
  58. if (y) {
  59. hoge[y][x] = hoge[y - 1][x] + 1;
  60. } else {
  61. hoge[y][x] = 1;
  62. }
  63. } else {
  64. c = 0;
  65. hoge[y][x] = 0;
  66. }
  67. home[y][x] = c;
  68. }
  69. }
  70.  
  71. N = getInt();
  72.  
  73. for (i = 0; i < N; ++i) {
  74. s = getInt();
  75. t = getInt();
  76.  
  77. if (s > H || t > W) {
  78. putchar('0');
  79. putchar('\n');
  80. continue;
  81. }
  82.  
  83. hye = s - 1;
  84. hxe = t - 1;
  85. count = 0;
  86. for (hy = H - 1; hy >= hye; --hy) {
  87. for (hx = W - 1; hx >= hxe; --hx) {
  88. if (home[hy][hx] < t) {
  89. hx -= home[hy][hx];
  90. continue;
  91. }
  92. if (hoge[hy][hx] < s) {
  93. continue;
  94. }
  95. for (dy = 1; dy < s; ++dy) {
  96. y = hy - dy;
  97. if (y < 0) {
  98. break;
  99. }
  100. if (home[y][hx] < t) {
  101. break;
  102. }
  103. }
  104. if (dy == s) {
  105. count++;
  106. }
  107. }
  108. }
  109. putInt(count);
  110. putchar('\n');
  111. }
  112.  
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 5000KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2