Problem p03112 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[-10**11], [-10**11], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))
s_s.append(10**11)
t_s.append(10**11)


def solve(k):
  s_pos = bisect.bisect_left(s_s, k)
  t_pos = bisect.bisect_left(t_s, k)
  mn = 10**11
  for i in [s_s[s_pos-1], s_s[s_pos]]:
    for j in [t_s[t_pos-1], t_s[t_pos]]:
      mn = min(mn,abs(i-k)+abs(i-j), abs(j-k)+abs(i-j))
  return(mn)

if __name__ == "__main__":
  for x in x_s:
    print(solve(x))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[], [], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))


## solve
def get_otherlen(st_type, k):
  if st_type == 's':
    st_s = s_s
  else:
    st_s = t_s

  idx = bisect.bisect_right(st_s, k)
  if idx == 0:
    mn = abs(st_s[0]-k)
  elif idx == len(s_s):
    mn = abs(st_s[-1]-k)
  else:
    mn = min(abs(st_s[idx]-k), abs(st_s[idx-1]-k))
  return(mn)


def get_len(k):
  # s -> t
  idx = bisect.bisect_right(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen('t', s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen('t', s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen('t', s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen('t', s_s[idx-1])
    mn = min(mn1, mn2)

  # t -> s
  idx = bisect.bisect_right(t_s, k)
  if idx == 0:
    mn = min(mn, abs(t_s[0]-k) + get_otherlen('s', t_s[0]))
  elif idx == len(t_s):
    mn = min(mn, abs(t_s[-1]-k) + get_otherlen('s', t_s[-1]))
  else:
    mn1 = abs(t_s[idx]-k) + get_otherlen('s', t_s[idx])
    mn2 = abs(t_s[idx-1]-k) + get_otherlen('s', t_s[idx-1])
    mn = min(mn, mn1, mn2)
  return(mn)


if __name__ == "__main__":
  for x in x_s:
    print(get_len(x))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[], [], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))


## solve
def get_otherlen(st_type, k):
  if st_type == 's':
    st_s = s_s
  else:
    st_s = t_s

  idx = bisect.bisect_left(st_s, k)
  if idx == 0:
    mn = abs(st_s[0]-k)
  elif idx == len(s_s):
    mn = abs(st_s[-1]-k)
  else:
    mn = min(abs(st_s[idx]-k), abs(st_s[idx-1]-k))
  return(mn)


def get_len(k):
  # s -> t
  idx = bisect.bisect_left(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen('t', s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen('t', s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen('t', s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen('t', s_s[idx-1])
    mn = min(mn1, mn2)

  # t -> s
  idx = bisect.bisect_left(t_s, k)
  if idx == 0:
    mn = min(mn, abs(t_s[0]-k) + get_otherlen('s', t_s[0]))
  elif idx == len(t_s):
    mn = min(mn, abs(t_s[-1]-k) + get_otherlen('s', t_s[-1]))
  else:
    mn1 = abs(t_s[idx]-k) + get_otherlen('s', t_s[idx])
    mn2 = abs(t_s[idx-1]-k) + get_otherlen('s', t_s[idx-1])
    mn = min(mn, mn1, mn2)
  return(mn)


if __name__ == "__main__":
  for x in x_s:
    print(get_len(x))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[], [], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))


## solve
def get_otherlen(st_s, k):
  idx = bisect.bisect_left(st_s, k)
  if idx == 0:
    mn = abs(st_s[0]-k)
  elif idx == len(s_s):
    mn = abs(st_s[-1]-k)
  else:
    mn = min(abs(st_s[idx]-k), abs(st_s[idx-1]-k))
  return(mn)


def get_len(k):
  # s -> t
  idx = bisect.bisect_left(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen(t_s, s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen(t_s, s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen(t_s, s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen(t_s, s_s[idx-1])
    mn = min(mn1, mn2)

  # t -> s
  idx = bisect.bisect_left(t_s, k)
  if idx == 0:
    mn = min(mn, abs(t_s[0]-k) + get_otherlen(s_s, t_s[0]))
  elif idx == len(t_s):
    mn = min(mn, abs(t_s[-1]-k) + get_otherlen(s_s, t_s[-1]))
  else:
    mn1 = abs(t_s[idx]-k) + get_otherlen(s_s, t_s[idx])
    mn2 = abs(t_s[idx-1]-k) + get_otherlen(s_s, t_s[idx-1])
    mn = min(mn, mn1, mn2)
  return(mn)


if __name__ == "__main__":
  for x in x_s:
    print(get_len(x))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math
import sys
import bisect

## input
a,b,q = map(int, sys.stdin.readline().rstrip().split())
s_s, t_s, x_s = [[], [], []]
for _ in range(a):
  s_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(b):
  t_s.append(int(sys.stdin.readline().rstrip()))
for _ in range(q):
  x_s.append(int(sys.stdin.readline().rstrip()))


## solve
def get_otherlen(st_s, k):
  idx = bisect.bisect_left(st_s, k)
  if idx == 0:
    mn = abs(st_s[0]-k)
  elif idx == len(s_s):
    mn = abs(st_s[-1]-k)
  else:
    mn = min(abs(st_s[idx]-k), abs(st_s[idx-1]-k))
  return(mn)


def get_len(k):
  # s -> t
  idx = bisect.bisect_left(s_s, k)
  if idx == 0:
    mn = abs(s_s[0]-k) + get_otherlen(t_s, s_s[0])
  elif idx == len(s_s):
    mn = abs(s_s[-1]-k) + get_otherlen(t_s, s_s[-1])
  else:
    mn1 = abs(s_s[idx]-k) + get_otherlen(t_s, s_s[idx])
    mn2 = abs(s_s[idx-1]-k) + get_otherlen(t_s, s_s[idx-1])
    mn = min(mn1, mn2)

  # t -> s
  idx = bisect.bisect_left(t_s, k)
  if idx == 0:
    mn = min(mn, abs(t_s[0]-k) + get_otherlen(s_s, t_s[0]))
  elif idx == len(t_s):
    mn = min(mn, abs(t_s[-1]-k) + get_otherlen(s_s, t_s[-1]))
  else:
    mn1 = abs(t_s[idx]-k) + get_otherlen(s_s, t_s[idx])
    mn2 = abs(t_s[idx-1]-k) + get_otherlen(s_s, t_s[idx-1])
    mn = min(mn, mn1, mn2)
  return(mn)


if __name__ == "__main__":
  for x in x_s:
    print(x, get_len(x))