fork download
  1. <?php
  2. // paiza POH! vol.2
  3. // result:
  4. // http://p...content-available-to-author-only...a.jp/poh/paizen/result/8afab308477ce5e7f61b8a83b9d383ca
  5. // author: Leonardone @ NEETSDKASU
  6.  
  7. fscanf(STDIN, "%d %d", $h, $w);
  8.  
  9. $table = array();
  10. $space2top = array_fill(0, $w, 0);
  11. for ($y = 0; $y <= $h; $y++) {
  12. $table[$y] = array_fill(0, $w + 1, 0);
  13. }
  14.  
  15. for ($y = 0; $y < $h; $y++) {
  16. fscanf(STDIN, "%s", $str);
  17. for ($x = 0; $x < $w; $x++) {
  18. if ($str[$x] == '0') {
  19. $space2top[$x]++;
  20. $s = $space2top[$x];
  21. $t = 1;
  22. for ($i = $x; $i >= 0 && $space2top[$i] > 0; $i--) {
  23. if ($space2top[$i] < $s) {
  24. $s = $space2top[$i];
  25. }
  26. $table[$s][$t]++;
  27. $t++;
  28. }
  29. } else {
  30. $space2top[$x] = 0;
  31. }
  32. }
  33. }
  34.  
  35. for ($x = 1; $x <= $w; $x++) {
  36. for ($y = $h - 1; $y > 0; $y--) {
  37. $table[$y][$x] += $table[$y + 1][$x];
  38. }
  39. }
  40.  
  41. fscanf(STDIN, "%d", $n);
  42.  
  43. for ($i = 0; $i < $n; $i++) {
  44. fscanf(STDIN, "%d %d", $s, $t);
  45. if ($s <= $h && $t <= $w) {
  46. echo $table[$s][$t], PHP_EOL;
  47. } else {
  48. echo '0', PHP_EOL;
  49. }
  50. }
  51.  
Success #stdin #stdout 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