Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Last active May 28, 2017 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/9548ba3d048304b5488113818e430b13 to your computer and use it in GitHub Desktop.
Save whatalnk/9548ba3d048304b5488113818e430b13 to your computer and use it in GitHub Desktop.
AtCoder AGC #015
N, A, B = gets.chomp.split(" ").map(&:to_i)
if N == 1 then
if A == B then
puts 1
else
puts 0
end
else
if A == B then
puts 1
elsif A > B then
puts 0
else
puts (B - A) * (N - 2) + 1
end
end
S = gets.chomp.split("")
n = S.size
ret = 0
(1..n).each do |i|
if i == 1 || i == n then
ret += (n - 1)
else
if S[i - 1] == "U" then
ret += ((n - i) + (i - 1) * 2)
else
ret += ((n - i) * 2 + (i - 1))
end
end
end
puts ret
# http://agc015.contest.atcoder.jp/submissions/1314849
# TLE
N, M, Q = gets.chomp.split(" ").map(&:to_i)
node = []
N.times do
node << gets.chomp.split("").map(&:to_i)
end
edge_h = Array.new(N){Array.new(M, 0)}
edge_v = Array.new(N){Array.new(M, 0)}
N.times do |i|
M.times do |j|
if j + 1 < M then
edge_h[i][j] = 1 if node[i][j] == 1 && node[i][j+1] == 1
end
if i > 0
edge_v[i][j] = 1 if node[i][j] == 1 && node[i-1][j] == 1
end
end
end
cumsum = Array.new(N+1){Array.new(M+1, 0)}
cumsum_h = Array.new(N+1){Array.new(M+1, 0)}
cumsum_v = Array.new(N+1){Array.new(M+1, 0)}
N.times do |i|
M.times do |j|
cumsum[i+1][j+1] = node[i][j] + cumsum[i][j+1] + cumsum[i+1][j] - cumsum[i][j]
cumsum_h[i+1][j+1] = edge_h[i][j] + cumsum_h[i][j+1] + cumsum_h[i+1][j] - cumsum_h[i][j]
cumsum_v[i+1][j+1] = edge_v[i][j] + cumsum_v[i][j+1] + cumsum_v[i+1][j] - cumsum_v[i][j]
end
end
Q.times do |v|
y1, x1, y2, x2 = gets.chomp.split(" ").map{|c| c.to_i}
sum_node = cumsum[y2][x2] - cumsum[y1-1][x2] - cumsum[y2][x1-1] + cumsum[y1-1][x1-1]
sum_edge_h = cumsum_h[y2][x2-1] - cumsum_h[y1-1][x2-1] - cumsum_h[y2][x1-1] + cumsum_h[y1-1][x1-1]
sum_edge_v = cumsum_v[y2][x2] - cumsum_v[y1-1+1][x2] - cumsum_v[y2][x1-1] + cumsum_v[y1-1+1][x1-1]
puts sum_node - sum_edge_h - sum_edge_v
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment