#! ruby # yukicoder My Practice # author: Leonardone @ NEETSDKASU def gs() gets.chomp end def gi() gets.to_i end def gss() gets.chomp.split end def gis() gss.map(&:to_i) end def nmapf(n,f) n.times.map{ __send__ f } end def arr2d(h,w,v=0) h.times.map{[v] * w} end def ngs(n) nmapf n,:gs end def ngi(n) nmapf n,:gi end def ngss(n) nmapf n,:gss end def ngis(n) nmapf n,:gis end def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end def nsum(n) n * (n + 1) / 2 end =begin 1) D範囲に完全内包 1.1: 軸に接しない 3 1 10 9 100 1.2: 軸に接する 3 0 10 8 100 1.3: 軸をまたぐ 3 -1 10 7 100 1.4: 原点を含む -3 -4 10 8 100 2) D範囲を完全内包 -10 -12 15 14 9 3) 一部だけD範囲と重なる 3.1: 三角範囲Dに入る 3.1.1: 軸に接しない 2 3 12 11 10 3.1.2: 片軸にだけ接する 0 3 12 11 10 3.1.3: 両軸に接する 0 0 12 11 10 4) Dの範囲の完全外側 10 12 20 17 7 =end def bruteforce(x1, y1, x2, y2, d) count = 0 (y1..y2).each do |y| (x1..x2).each do |x| count += 1 if x.abs + y.abs <= d end end count end def bunkatsu(z1_,z2_) z1, z2 = [z1_, z2_].minmax z1a, z2a = [z1.abs, z2.abs].minmax case when z2 < 0 [[z1a, z2a]] when z2 == 0 [[0, 0], [1, z1a]] when z1 > 0 [[z1a, z2a]] when z1 == 0 [[0, 0], [1, z2a]] when z1 < 0 && 0 < z2 [[0, 0], [1, z1a], [1, z2a]] else sleep 10 [] end end # 4) Dの範囲の完全外側 def perfectly_outside?((x1, x2), (y1, y2), d) d < x1 + y1 end # 三角形 def triangle_count((x1, x2), (y1, y2), d) return 0 if x1 * y1 == 0 return 0 if d < x1 + y1 return 0 if [x1 + y2, x2 + y1].min < d rem = d - (x1 + y1) nsum(rem + 1) end # 直線 def line_count((x1, x2), (y1, y2), d) return 0 if x1 != x2 && y1 != y2 return 0 if d < x1 + y1 if d < x2 + y2 d - (x1 + y1) + 1 else (x2 + y2) - (x1 + y1) + 1 end end X1,Y1,X2,Y2,D = gis #bf = bruteforce(X1, Y1, X2, Y2, D) #puts "bruteforce %d" % [bf] # 1) D範囲に完全内包 if [X1.abs, X2.abs].max + [Y1.abs, Y2.abs].max <= D count = (X2 - X1).abs.succ * (Y2 - Y1).abs.succ puts count exit end # 2) D範囲を完全内包 if [X1..X2, Y1..Y2].all?{|r| r.include?(-D) && r.include?(D)} count = nsum(D) * 4 + 1 puts count exit end xs = bunkatsu(X1, X2) ys = bunkatsu(Y1, Y2) #p xs, ys count = 0 for2p(ys,xs) {|yt,xt| next if perfectly_outside?(xt, yt, D) count += triangle_count(xt, yt, D) count += line_count(xt, yt, D) } puts count