fork(3) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/720571daa885443d3cfd98d72fec1d3b
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. #include <stdio.h>
  7.  
  8. #define OUTPUTSIZE (600000)
  9. char output[OUTPUTSIZE];
  10. char *optr = output;
  11.  
  12. void putInt(int v) {
  13. if (v < 10) {
  14. *optr = '0' + (char)v;
  15. ++optr;
  16. } else {
  17. putInt(v / 10);
  18. *optr = '0' + (char)(v % 10);
  19. ++optr;
  20. }
  21. }
  22.  
  23. void putNewline(void) {
  24. *optr = '\n';
  25. ++optr;
  26. }
  27.  
  28. #define BUFSIZE (900000)
  29. char buf[BUFSIZE];
  30. char *ptr = buf;
  31.  
  32. int getInt(void) {
  33. int v = 0;
  34. while (*ptr < '0' || *ptr > '9') ++ptr;
  35. while (*ptr >= '0' && *ptr <= '9') {
  36. v = 10 * v + (int)(*ptr - '0');
  37. ++ptr;
  38. }
  39. return v;
  40. }
  41.  
  42. char getChar(void) {
  43. while (*ptr < '0' || *ptr > '9') ++ptr;
  44. return *ptr++;
  45. }
  46.  
  47. int space2left[300][300];
  48. int table[301][301];
  49. int cache[301][301];
  50.  
  51. int spaceCount, tempCount, count;
  52.  
  53. int *tempSP2Ty;
  54. int *tempSP2Ty1;
  55.  
  56. int main(void) {
  57. int H, W, N, s, t, i, j, x, y, dx, ty, dy;
  58.  
  59. fread(buf, sizeof(char), BUFSIZE, stdin);
  60.  
  61. H = getInt();
  62. W = getInt();
  63.  
  64. for (y = 0; y < H; ++y) {
  65. spaceCount = 0;
  66. for (x = 0; x < W; ++x) {
  67. if (getChar() == '0') {
  68. ++spaceCount;
  69. } else {
  70. spaceCount = 0;
  71. }
  72. space2left[y][x] = spaceCount;
  73. }
  74. }
  75.  
  76. for (y = 0; y < H; ++y) {
  77. for (x = W - 1; x >= 0; --x) {
  78. if (space2left[y][x] == 0) {
  79. continue;
  80. }
  81. s = 1;
  82. t = space2left[y][x];
  83. for (i = y; i < H && space2left[i][x] > 0; i++) {
  84. if (space2left[i][x] < t) {
  85. t = space2left[i][x];
  86. }
  87. ++table[s][t];
  88. ++s;
  89. }
  90. }
  91. }
  92.  
  93. N = getInt();
  94.  
  95. for (i = 0; i < N; ++i) {
  96. s = getInt();
  97. t = getInt();
  98. count = 0;
  99.  
  100. if (s <= H && t <= W) {
  101. if (cache[s][t]) {
  102. count = cache[s][t] - 1 ;
  103. } else {
  104. for (j = t; j <= W; ++j) {
  105. count += table[s][j];
  106. }
  107. cache[s][t] = count + 1;
  108. }
  109. }
  110.  
  111. putInt(count);
  112. putNewline();
  113. }
  114.  
  115. fwrite(output, sizeof(char), (int)optr - (int)output, stdout);
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0s 4776KB
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