fork(1) download
  1. <?php
  2. // paiza POH! vol.2
  3. // result:
  4. // http://p...content-available-to-author-only...a.jp/poh/paizen/result/d548b3ab8816ce66e9d6803dd491e75f
  5. // author: Leonardone @ NEETSDKASU
  6.  
  7. fscanf(STDIN, "%d %d", $h, $w);
  8.  
  9. $table = array();
  10. $cache = array();
  11. $space2right = array();
  12.  
  13. for ($y = 0; $y < $h; $y++) {
  14. fscanf(STDIN, "%s", $str);
  15. $count = 0;
  16. $table[$y] = array_fill(0, $w, 0);
  17. $cache[$y] = array_fill(0, $w, 0);
  18. $space2right[$y] = array_fill(0, $w, 0);
  19. for ($x = $w - 1; $x >= 0; $x--) {
  20. if ($str[$x] == '0') {
  21. $count++;
  22. } else {
  23. $count = 0;
  24. }
  25. $space2right[$y][$x] = $count;
  26. }
  27. }
  28.  
  29. for ($y = 0; $y < $h; $y++) {
  30. for ($x = 0; $x < $w; $x++) {
  31. if ($space2right[$y][$x] == 0) {
  32. continue;
  33. }
  34. $s = 1;
  35. $t = $space2right[$y][$x];
  36. for ($i = $y; $i < $h && $space2right[$i][$x] > 0; $i++) {
  37. if ($space2right[$i][$x] < $t) {
  38. $t = $space2right[$i][$x];
  39. }
  40. $table[$s][$t]++;
  41. $s++;
  42. }
  43. }
  44. }
  45.  
  46. fscanf(STDIN, "%d", $n);
  47.  
  48. for ($i = 0; $i < $n; $i++) {
  49. fscanf(STDIN, "%d %d", $s, $t);
  50. if ($s > $h || $t > $w) {
  51. echo '0', PHP_EOL;
  52. } else {
  53. if ($cache[$s][$t] > 0) {
  54. echo ($cache[$s][$t] - 1), PHP_EOL;
  55. } else {
  56. $count = 0;
  57. for ($j = $t; $j <= $w; $j++) {
  58. $count += $table[$s][$j];
  59. }
  60. $cache[$s][$t] = $count + 1;
  61. echo $count, PHP_EOL;
  62. }
  63. }
  64. }
  65.  
Success #stdin #stdout #stderr 0.01s 20568KB
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
stderr
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 40
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 40
PHP Notice:  Undefined offset: 1 in /home/fb8ivT/prog.php on line 40
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 58
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 58
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 58
PHP Notice:  Undefined offset: 5 in /home/fb8ivT/prog.php on line 58