fork download
  1. # paiza POH! vol.2
  2. # result:
  3. # http://p...content-available-to-author-only...a.jp/poh/paizen/result/0a6719efee92cade010971bc36de29c7
  4. # author: Leonardone @ NEETSDKASU
  5. hw = gets.split(' ')
  6. h = hw[0].to_i
  7. w = hw[1].to_i
  8. sp = Array.new(w, 0)
  9. tb = Array.new(h + 1).map{ Array.new(w + 1, 0) }
  10. h.times do |y|
  11. line = gets.split('')
  12. w.times do |x|
  13. if line[x] == '0' then
  14. sp[x] = sp[x] + 1
  15. s = sp[x]
  16. t = 1
  17. i = x
  18. while i >= 0 && sp[i] > 0 do
  19. if sp[i] < s then
  20. s = sp[i]
  21. end
  22. tb[s][t] = tb[s][t] + 1
  23. t = t + 1
  24. i = i - 1
  25. end
  26. else
  27. sp[x] = 0
  28. end
  29. end
  30. end
  31.  
  32. hh = h - 1
  33. w.times do |x|
  34. xx = x + 1
  35. hh.times do |y|
  36. yy = hh - y
  37. tb[yy][xx] = tb[yy][xx] + tb[yy + 1][xx]
  38. end
  39. end
  40.  
  41. n = gets.to_i
  42.  
  43. n.times do
  44. st = gets.split(' ')
  45. s = st[0].to_i
  46. t = st[1].to_i
  47. if s <= h && t <= w then
  48. puts tb[s][t]
  49. else
  50. puts "0"
  51. end
  52. end
Success #stdin #stdout 0.01s 7420KB
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