Problem:


Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.



Sample input:
2 3 4
100
600
400
900
1000
150
2000
899
799


Sample output:
350
1400
301
399

Submission status: Wrong Answer

Solution code:
a,b,q = map(int, input().split(" "))
A,B,Q = [],[],[]
for i in range(a+b+q):
  if i<a:
    A.append(int(input()))
  elif i<a+b:
    B.append(int(input()))
  else:
    Q.append(int(input()))  
    
for j in Q:
  mina,minb=0,0
  pos=0
  posa,posb=0,0
  needa=9999999999999999999999999999999999999
  for k in A:
    if needa>abs(k-j):
      needa=abs(k-j)
      posa=k
  needb=999999999999999999999999999999999999999
  for l in B:
    if needb>abs(l-j):
      needb=abs(l-j)
      posb=l    
  needa2=99999999999999999999999999999999999999
  minb+=needb
  pos=posb
  for m in A:
    if needa2>abs(pos-m):
      needa2=abs(pos-m)   
  minb+=needa2 
  needb2=99999999999999999999999999999999999
  mina+=needa
  pos=posa
  for n in B:
    if needb2>abs(pos-n):
      needb2=abs(pos-n)
      print(needb2,posa)    
  mina+=needb2
  print(min(mina,minb))

Submission status: Wrong Answer

Solution code:
(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]


def find_left_and_right(st, start, end, pos):
    if pos < st[start]:
        return (-1, start)
    elif pos > st[end-1]:
        return (end-1, -1)
    i = int((end - start) / 2)
    if st[i] > pos and st[i-1] < pos:
        return (i-1, i)
    l_result = find_left_and_right(st, start, i, pos)
    if l_result:
        return l_result
    r_result = find_left_and_right(st, start, i, pos)
    if r_result:
        return r_result
    return None


def find_optimum(ss, ts, pos):
    (sl_index, sr_index) = find_left_and_right(ss, 0, len(ss), pos)
    (tl_index, tr_index) = find_left_and_right(ts, 0, len(ts), pos)

    penalty = 9999999999999
    s_left = s[sl_index] if 0 <= sl_index else penalty
    s_right = s[sr_index] if 0 <= sr_index else penalty
    t_left = t[tl_index] if 0 <= tl_index else penalty
    t_right = t[tr_index] if 0 <= tr_index else penalty

    dist0 = abs(s_left - pos) + min(abs(t_left - s_left), abs(t_right - s_left))
    dist1 = abs(s_right - pos) + min(abs(t_left - s_right), abs(t_right - s_right))
    dist2 = abs(t_left - pos) + min(abs(s_left - t_left), abs(s_right - t_left))
    dist3 = abs(t_right - pos) + min(abs(s_left - t_right), abs(s_right - t_right))
    return min([dist0, dist1, dist2, dist3])


for x_i in x:
    print(find_optimum(s, t, x_i))

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())

INF = 1<<60

s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]

bisect.insort_left(s, -INF)
bisect.insort_left(s, INF)
bisect.insort_left(t, -INF)
bisect.insort_left(t, INF)

def cost(sl, sr, tl, tr):
    tmp = INF
    tmp = min(tmp, sl * 2 + tr)
    tmp = min(tmp, tl * 2 + sr)
    tmp = min(tmp, sr * 2 + tl)
    tmp = min(tmp, tr * 2 + sl)
    tmp = min(tmp, max(sl, tl))
    tmp = min(tmp, max(sr, tr))
    return tmp

# print(s)
# print(t)

def lower_bound(v, x):
    l = 0
    r = len(v) - 1
    while r - l > 1:
        mid = (l + r) // 2
        if v[mid] < x:
            l = mid
        else:
            r = mid
    return r

print(s)

for _ in range(Q):
    x = int(input())
    # print(x)
    sl = x - s[lower_bound(s, x) - 1]
    sr = s[lower_bound(s, x)] - x
    tl = x - t[lower_bound(t, x) - 1]
    tr = t[lower_bound(t, x)] - x
    print(cost(sl, sr, tl, tr))

Submission status: Wrong Answer

Solution code:
(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]


def find_left_and_right(st, start, end, pos):
    if pos < st[start]:
        return (-1, 0)
    elif pos > st[end-1]:
        return (end-1, -1)
    i = int((end - start) / 2)
    if st[i] > pos and st[i-1] < pos:
        return (i-1, i)
    l_result = find_left_and_right(st, start, i, pos)
    if l_result:
        return l_result
    r_result = find_left_and_right(st, start, i, pos)
    if r_result:
        return r_result
    return None


def find_optimum(ss, ts, pos):
    (sl_index, sr_index) = find_left_and_right(ss, 0, len(ss), pos)
    (tl_index, tr_index) = find_left_and_right(ts, 0, len(ts), pos)

    penalty = 9999999999999
    s_left = s[sl_index] if 0 <= sl_index else penalty
    s_right = s[sr_index] if 0 <= sr_index else penalty
    t_left = t[tl_index] if 0 <= tl_index else penalty
    t_right = t[tr_index] if 0 <= tr_index else penalty

    dist0 = abs(s_left - pos) + min(abs(t_left - s_left), abs(t_right - s_left))
    dist1 = abs(s_right - pos) + min(abs(t_left - s_right), abs(t_right - s_right))
    dist2 = abs(t_left - pos) + min(abs(s_left - t_left), abs(s_right - t_left))
    dist3 = abs(t_right - pos) + min(abs(s_left - t_right), abs(s_right - t_right))
    return min([dist0, dist1, dist2, dist3])


for x_i in x:
    print(find_optimum(s, t, x_i))

Submission status: Wrong Answer

Solution code:
a,b,q = map(int,input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
x = [int(input()) for i in range(q)]
ans = []
for x_ in x:
    s_ = []
    t_ = []
    for i in range(len(s)):
        s_.append(s[i]-x_)
        if s[i]-x_>0:
            break
    for i in range(len(t)):
        t_.append(t[i]-x_)
        if t_[i]-x_:
            break
    s_l=-float('inf')
    s_r=float('inf')
    t_l=-float('inf')
    t_r=float('inf')
    for k in s_:
        if k<0:
            s_l=k
        else:
            s_r=k
            break
    for k in t_:
        if k<0:
            t_l=k
        else:
            t_r=k
            break
    ans.append(min(max(abs(s_l),abs(t_l)),abs(s_l)*2+t_r,t_r*2+abs(s_l),max(s_r,t_r),s_r*2+abs(t_l),abs(t_l)*2+s_r))
for ans_ in ans:
    print(ans_)

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue

    # S1, T1
    if s_i != 0 and t_i != 0:
        tmp = min(tmp, abs(min(s_list[s_i - 1], t_list[t_i - 1]) - x))
    # S2, T2
    tmp = min(tmp, abs(max(s_list[s_i], t_list[t_i]) - x))

    # S1, T2
    if s_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i - 1] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i - 1] - x))))
    # S2, T1
    if t_i != 0:
        tmp = min(tmp, abs(min(abs(s_list[s_i] - x), abs(t_list[t_i - 1] - x)) * 2 + max(abs(s_list[s_i] - x),
                                                                                         abs(t_list[t_i - 1] - x))))
    ans.append(tmp)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import product
from bisect import bisect, bisect_left
def inpl(): return list(map(int, input().split()))
A, B, Q = inpl()
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]

for x in X:
    ans = 1e12
    ls, lt = S[max(0, bisect(S, x)-1)], T[max(0, bisect(T, x)-1)]
    rs, rt = S[min(A-1, bisect_left(S, x))], T[min(B-1, bisect_left(T, x))]
    for s in [ls, rs]:
        for t in [lt, rt]:
            if s < t:
                a, b = s, t
            else:
                a, b = t, s

            if x < s:
                ans = min(ans, b-x)
            elif t < x:
                ans = min(ans, x-a)
            else:
                ans = min(ans, 2*(x-a) + (b-x), (x-a) + 2*(b-x))
    print(ans)

Submission status: Wrong Answer

Solution code:
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
Q = [int(input()) for i in range(Q)]

#print(S, T, Q)

def calc_m(xs):
    return [(a + b) / 2 for a, b in zip(xs, xs[1:])]

sm = calc_m(S)
tm = calc_m(T)

#print(sm)
#print(tm)

import bisect

def calc_nearest_0(xs, y, ym):
    result = []
    i = 0
    for x in xs:
        while i < len(ym) and x >= ym[i]:
            i += 1
        result.append(y[i])
    return result

def calc_nearest(xs, y, ym):
    result = []
    for x in xs:
        i = bisect.bisect(ym, x)
        result.append(y[i])
    return result

sn = calc_nearest(S, T, tm)
tn = calc_nearest(T, S, sm)

#print(sn)
#print(tn)

def solve(q, S, T, sn, tn):
    nsp = bisect.bisect(sm, q)
    ntp = bisect.bisect(tm, q)
    r1 = abs(S[nsp] - q) + abs(S[nsp] - sn[nsp])
    r2 = abs(T[ntp] - q) + abs(T[ntp] - tn[ntp])
    # print("Q", q, S, T, sn, tn, r1, r2)
    return min(r1, r2)

for q in Q:
    print(solve(q, S, T, sn, tn))

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue

    # S1, T1
    if s_i != 0 and t_i != 0:
        tmp = min(tmp, abs(min(s_list[s_i - 1], t_list[-1]) - x))
    # S2, T2
    tmp = min(abs(max(s_list[s_i], t_list[t_i]) - x), tmp)

    # S1, T2
    if s_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i - 1] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i - 1] - x))))
    # S2, T1
    if t_i != 0:
        tmp = min(tmp, abs(min(abs(s_list[s_i] - x), abs(t_list[t_i-1] - x)) * 2 + max(abs(s_list[s_i] - x),
                                                                                         abs(t_list[t_i - 1] - x))))
    ans.append(tmp)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import numpy as np
A, B, Q = list(map(int,input().split()))
s, t, x = [[], [], []]
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x.append(int(input()))
def get(list, x):
    index = np.abs(np.asarray(list) - x).argmin()
    return list[index]
def find(s, t, x):
    dis = np.int64(0)
    near = np.int64(get(s + t, x))
    dis += np.int64(abs(near - x))
    x = near
    if near in s:
        near_2 = np.int64(get(t, x))
        dis += np.int64(abs(near_2 - x))
    else:
        near_2 = get(s, x)
        dis += np.int64(abs(near_2 - x))
    return dis
for q in range(Q):
    print(find(s, t, x[q]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python2
"""
This file is part of https://github.com/cheran-senthil/PyRival
Copyright 2019 Cheran Senthilkumar <hello@cheran.io>

"""
from __future__ import division, print_function

import itertools
import os
import sys
from atexit import register
from io import BytesIO


class dict(dict):
    """dict() -> new empty dictionary"""

    def items(self):
        """D.items() -> a set-like object providing a view on D's items"""
        return dict.iteritems(self)

    def keys(self):
        """D.keys() -> a set-like object providing a view on D's keys"""
        return dict.iterkeys(self)

    def values(self):
        """D.values() -> an object providing a view on D's values"""
        return dict.itervalues(self)


def gcd(x, y):
    """greatest common divisor of x and y"""
    while y:
        x, y = y, x % y
    return x


range = xrange

filter = itertools.ifilter
map = itertools.imap
zip = itertools.izip

sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))

input = lambda: sys.stdin.readline().rstrip('\r\n')


def main():
    a, b, q = map(int, input().split())

    s = sorted(int(input()) for _ in range(a))
    t = sorted(int(input()) for _ in range(b))

    for _ in range(q):
        x = int(input())

        lo, hi = 0, a - 1
        while lo < hi:
            mi = lo + (hi - lo) // 2
            if s[mi] >= x:
                hi = mi
            else:
                lo = mi + 1

        if lo == 0:
            p1, _p1 = s[0] - x, s[0] - x
        else:
            p1, _p1 = s[lo - 1] - x, s[lo] - x

        lo, hi = 0, b - 1
        while lo < hi:
            mi = lo + (hi - lo) // 2
            if t[mi] >= x:
                hi = mi
            else:
                lo = mi + 1

        if lo == 0:
            p2, _p2 = t[0] - x, t[0] - x
        else:
            p2, _p2 = t[lo - 1] - x, t[lo] - x

        res = float('inf')

        if _p1 <= 0 and _p2 <= 0:
            res = min(res, abs(min(_p1, _p2)))

        if p1 <= 0 and p2 <= 0:
            res = min(res, abs(min(p1, p2)))

        if _p1 <= 0 and p2 <= 0:
            res = min(res, abs(min(_p1, p2)))

        if p1 <= 0 and _p2 <= 0:
            res = min(res, abs(min(p1, _p2)))

        if p1 >= 0 and p2 >= 0:
            res = min(res, min(p1, p2))

        if p1 >= 0 and _p2 >= 0:
            res = min(res, max(p1, _p2))

        if p2 >= 0 and _p1 >= 0:
            res = min(res, max(p2, _p1))

        if _p2 >= 0 and _p1 >= 0:
            res = min(res, max(_p1, _p2))

        res = min(res, 2 * abs(_p1) + abs(p2))
        res = min(res, 2 * abs(_p1) + abs(_p2))
        res = min(res, 2 * abs(p1) + abs(p2))
        res = min(res, 2 * abs(p1) + abs(_p2))

        res = min(res, 2 * abs(_p2) + abs(p1))
        res = min(res, 2 * abs(_p2) + abs(_p1))
        res = min(res, 2 * abs(p2) + abs(_p1))
        res = min(res, 2 * abs(p2) + abs(p1))

        print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
Q = [int(input()) for i in range(Q)]

#print(S, T, Q)

def calc_m(xs):
    return [(a + b) / 2 for a, b in zip(xs, xs[1:])]

sm = calc_m(S)
tm = calc_m(T)

#print(sm)
#print(tm)

def calc_nearest(xs, y, ym):
    result = []
    i = 0
    for x in xs:
        while i < len(ym) and x >= ym[i]:
            i += 1
        result.append(y[i])
    return result

sn = calc_nearest(S, T, tm)
tn = calc_nearest(T, S, sm)

#print(sn)
#print(tn)

import bisect

def solve(q, S, T, sn, tn):
    nsp = bisect.bisect(sm, q)
    ntp = bisect.bisect(tm, q)
    r1 = abs(S[nsp] - q) + abs(S[nsp] - sn[nsp])
    r2 = abs(T[ntp] - q) + abs(T[ntp] - tn[ntp])
    # print("Q", q, S, T, sn, tn, r1, r2)
    return min(r1, r2)

for q in Q:
    print(solve(q, S, T, sn, tn))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import bisect

MAX_VAL = 2e+10


def main():
  a, b, s = map(int, input().split())
  places = [(int(input()), 's') for _ in range(a)]
  places += [(int(input()), 't') for _ in range(b)]
  starts = [int(input()) for _ in range(s)]

  places.sort(key=lambda x: x[0])

  for i in range(len(places)):
    p, t = places[i]
    v = MAX_VAL

    if i > 0 and places[i - 1][1] != t:
      v = p - places[i - 1][0]

    if i < len(places) - 1 and places[i + 1][1] != t:
      v = min(places[i + 1][0] - p, v)

    places[i] = (p, t, v)

  places = [(p, t, v) for p, t, v in places if v != MAX_VAL]
  points = [p for p, _, _ in places]

  for start in starts:
    idx = bisect.bisect(points, start)
    left = MAX_VAL
    right = MAX_VAL

    if idx != 0:
      left = (start - points[idx - 1]) + places[idx - 1][2]

    if idx < len(points):
      right = (points[idx] - start) + places[idx][2]

    print(min(left, right), flush=True)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8
A,B,Q = map(int,input().split())
S = []
T = []
X = []
for a in range(A):
    S.append(int(input()))
for b in range(B):
    T.append(int(input()))
for q in range(Q):
    X.append(int(input()))

print (S,T,X)

def find_nearest(array, point):
    #print ("p",point)
    abs_diff = 10 ** 11
    for place in array:

        diff = place - point
        #print ("d",diff)
        if abs_diff > abs(diff):
            min_diff = diff
            abs_diff = abs(diff)
        
    return min_diff

def calc(first, second,point):
    score = 0
    md = find_nearest(first,point)
    #print("m",md)
    score += abs(md)
    point += md
    md = find_nearest(second,point)
    score += abs(md)
    #print ("m",md)
    #print (score)
    return score

for x in X:
    print (min(calc(S,T,x),calc(T,S,x)))
    #break

Submission status: Wrong Answer

Solution code:
INF = float('inf')

from sys import stdin, stdout
from bisect import bisect_left as bl, bisect_right as br

def f(x, s, t):
    a = abs(x - s) + abs(s - t)
    a = min(a, abs(x - t) + abs(t - s))
    return a

a, b, q = map(int, raw_input().strip().split())
inp = stdin.readlines()
out = []

s = [0 for i in xrange(a)]
t = [0 for i in xrange(b)]

ctr = 0
for i in xrange(a):
    s[i] = int(inp[i + ctr].strip())

ctr = a
for i in xrange(b):
    t[i] = int(inp[i + ctr].strip())

ctr += b
for i in xrange(q):
    x = int(inp[i + ctr].strip())
    idx_s = bl(s, x)

    s1 = -INF
    s2 = INF
    if idx_s:
        s1 = s[idx_s - 1]
    try: s2 = s[idx_s]
    except: pass

    idx_t = bl(t, x)
    t1 = -INF
    t2 = INF
    if idx_t:
        t1 = t[idx_t - 1]
    try: t2 = t[idx_t]
    except: pass
    
    ans = f(x, s1, t1)
    ans = min(ans, f(x, s1, t2))
    ans = min(ans, f(x, s2, t2))
    ans = min(ans, f(x, s2, t1))

    out.append(str(ans))

stdout.write("\n".join(out))

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]

for i in range(Q):
    x = X[i]
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)

    if s_index == 0:
        s_left = -float("inf")
    else:
        s_left = s[s_index - 1]
    
    if s_index == A:
        s_right = float("inf")
    else:
        s_right = s[s_index]
        
    if t_index == 0:
        t_left = -float("inf")
    else:
        t_left = t[t_index - 1]

    if t_index == B:
        t_right = float("inf")
    else:
        t_right = t[t_index]

    ans = float("inf")

    if s_index != 0 and t_index != 0:
        ans = x - min(s_left, t_left)

    if s_index != 0 and t_index != B:
        temp = min(x - s_left, t_right - x)
        ans = min(ans, (x - s_left) + (t_right - x) + temp)

    if s_index != A and t_index != 0:
        temp = min(x - t_left, s_right - x)
        ans = min(ans, (x - t_left) + (s_right - x) + temp)

    if t_index != B and s_index != A:
        ans = min(ans, max(s_right, t_left) - x)

    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
A, B, Q = list(map(int,input().split()))
s, t, x = [[], [], []]
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x.append(int(input()))
def get(list, x):
    index = np.abs(np.asarray(list) - x).argmin()
    return list[index]
def find(s, t, x):
    dis = 0
    near = get(s + t, x)
    dis += abs(near - x)
    x = near
    if near in s:
        near_2 = get(t, x)
        dis += abs(near_2 - x)
    else:
        near_2 = get(s, x)
        dis += abs(near_2 - x)
    return dis
for q in range(Q):
    print(find(s, t, x[q]))

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue
    tmp = min(tmp, max(s_list[s_i], t_list[t_i]) - x)
    if s_i != 0:
        tmp = min(tmp, s_list[s_i - 1] + t_list[t_i] - x)
    if t_i != 0:
        tmp = min(tmp, s_list[s_i] + t_list[t_i - 1] - x)
    ans.append(tmp)

for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
# D
from bisect import bisect_left
A, B, Q = map(int, input().split())

s_list = [0]*A
t_list = [0]*B
q_list = [0]*Q

for i in range(A):
    s_list[i] = int(input())
for i in range(B):
    t_list[i] = int(input())
for i in range(Q):
    q_list[i] = int(input())
    
def query_dist(q):
    res = 10**11
    # s -> t
    ind_s = bisect_left(s_list, q)
    if ind_s != len(s_list):
        s = s_list[ind_s]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
    if ind_s != 0:
        s = s_list[ind_s-1]
        ind_t = bisect_left(t_list, s)
        if ind_t != len(t_list):
            t = t_list[ind_t]
            res = min(res, abs(s-q)+abs(t-s))
        if ind_t != 0:
            t = t_list[ind_t-1]
            res = min(res, abs(s-q)+abs(t-s))
            
    ind_t = bisect_left(t_list, q)
    if ind_t != len(t_list):
        t = t_list[ind_t]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    if ind_t != len(t_list):
        t = t_list[ind_t-1]
        ind_s = bisect_left(s_list, t)
        if ind_s != len(s_list):
            s = s_list[ind_s]
            res = min(res, abs(t-q)+abs(s-t))
        if ind_s != 0:
            s = s_list[ind_s-1]
            res = min(res, abs(t-q)+abs(s-t))
    return res

for q in q_list:
    print(query_dist(q))

Submission status: Wrong Answer

Solution code:
import bisect

a,b,n=map(int,input().split())
l1=[int(input()) for i in range(a)]
l2=[int(input()) for i in range(b)]
x=[int(input()) for i in range(n)]
l1.sort()
l2.sort()
for i in range(n):
    idx1=bisect.bisect_left(l1,x[i])
    if idx1<a:
        p=abs(l1[idx1-1]-x[i])
        q=abs(l1[idx1]-x[i])
        if p>=q:
            po=l1[idx1]
            dis1=q
        else:
            po=l1[idx1-1]
            dis1=p
    else:
        dis1=x[i]-l1[-1]
        po=l1[-1]
    idx3=bisect.bisect_left(l2,po)
    if idx3<b:
        dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
    else:
        dis=po-l2[-1]
    dis1+=dis
    idx2=bisect.bisect_left(l2,x[i])
    if idx2<b:
        p=abs(l2[idx2-1]-x[i])
        q=abs(l2[idx2]-x[i])
        if p>=q:
            po=l2[idx2]
            dis2=q
        else:
            po=l2[idx2-1]
            dis2=p
    else:
        dis2=x[i]-l2[-1]
        po=l2[-1]
    idx3=bisect.bisect_left(l1,po)
    if idx3<a:
        dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
    else:
        dis=po-l1[-1]
    dis2+=dis
    print(min(dis1,dis2))

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = [int(_) for _ in input().split()]
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
for x in X:
    i_s_l = bisect.bisect_left(S, x)
    i_s_r = bisect.bisect_right(S, x)
    i_t_l = bisect.bisect_left(T, x)
    i_t_r = bisect.bisect_right(T, x)

    D_l = D_r = D_m1 = D_m2 = float('inf')
    if 0 <= i_s_l < A and 0 <= i_t_l < B:
        D_l = max(S[i_s_l] - x, T[i_t_l] - x)
    if 1 <= i_s_r <= A and 1 <= i_t_r <= B:
        D_r = max(x - S[i_s_r - 1], x - T[i_t_r - 1])
    if 0 <= i_s_l < A and 1 <= i_t_r <= B:
        D_m1 = min(S[i_s_l] - x, x - T[i_t_r - 1]) + S[i_s_l] - T[i_t_r - 1]
    if 0 <= i_t_l < A and 1 <= i_s_r <= B:
        D_m2 = min(T[i_t_l] - x, x - S[i_s_r - 1]) + T[i_t_l] - S[i_s_r - 1]
    print(min([D_l, D_r, D_m1, D_m2]))

Submission status: Wrong Answer

Solution code:
#119-D
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
import bisect
a,b,q=IL()
s=[]
t=[]
for i in range(a):
    tmp=I()
    s.append(tmp)
for i in range(b):
    tmp=I()
    t.append(tmp)
inf=10**10
for i in range(q):
    rs,ls,rt,lt=inf,inf,inf,inf
    x=I()
    ts=bisect.bisect(s,x)
    tt=bisect.bisect(t,x)
    if ts!=0:
        ls=abs(x-s[ts-1])
    if tt!=0:
        lt=abs(x-t[tt-1])
    if ts!=a:
        rs=abs(s[ts]-x)
    if tt!=b:
        rt=abs(t[tt]-x)
    print(min(min(rs,lt)*2+max(rs,lt),min(rt,ls)*2+max(rt,ls),max(lt,ls),max(rt,rs)))

Submission status: Wrong Answer

Solution code:
a,b,q=map(int,input().split())
S=[int(input()) for i in range(a)]
T=[int(input()) for i in range(b)]
Q=[int(input()) for i in range(q)]

QLIST=[[10**15,10**15,10**15,10**15] for i in range(q)]

QI=[[Q[i],i] for i in range(q)]
QI.sort()

i=0
j=0

while j<q and i<a:
    if S[i]>QI[j][0]:
        if i>0:
            QLIST[QI[j][1]][0]=QI[j][0]-S[i-1]
        QLIST[QI[j][1]][1]=S[i]-QI[j][0]

        j+=1
    else:
        i+=1

    if i==a:
        for h in range(j,q):
            QLIST[QI[h][1]][0]=QI[h][0]-S[i-1]

i=0
j=0

while j<q and i<b:
    if T[i]>QI[j][0]:
        if i>0:
            QLIST[QI[j][1]][2]=QI[j][0]-T[i-1]
        QLIST[QI[j][1]][3]=T[i]-QI[j][0]

        j+=1
    else:
        i+=1

    if i==a:
        for h in range(j,q):
            QLIST[QI[h][1]][2]=QI[h][0]-T[i-1]
        
        
for L1,R1,L2,R2 in QLIST:
    A=max(L1,L2)
    B=max(R1,R2)
    C=min(L1,R1)*2+min(L2,R2)
    D=min(L1,R1)+min(L2,R2)*2

    print(min(A,B,C,D))