m=9**6 f=[g=z=0]*m*2 gets $<.map{|s|x,r=s.split.map &:to_i;f[m+x-r]+=1;f[m+x+r]-=1;} f.each{|t|g+=t;z