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:
from bisect import bisect_left

abq = list(map(int, input().split()))
s = sorted([int(input()) for i in range(abq[0])])
t = sorted([int(input()) for i in range(abq[1])])
x = [int(input()) for i in range(abq[2])]

for i in range(abq[2]):
    
    s_r_i = bisect_left(s, x[i])
    if s_r_i < len(s):  
        s_r = s[s_r_i]
    else:
        s_r = None
    
    t_r_i = bisect_left(t, x[i])
    if t_r_i < len(t):
        t_r = t[t_r_i]
    else:
        t_r = None
    
    s_l_i = bisect_left(s, x[i]) - 1
    if 0 <= s_l_i <len(s):
        s_l = s[s_l_i]
    else:
        s_l = None
    
    t_l_i = bisect_left(t, x[i]) - 1
    if 0 <= t_l_i < len(t):
        t_l = t[t_l_i]
    else:
        t_l = None
    
    if s_r != None and t_r != None:
        if (x[i] - s_r)*(x[i] - t_r) > 0:
            route1 = max(abs(x[i] - s_r), abs(x[i] - t_r))
        else:
            route1 = min(abs(x[i] - s_r), abs(x[i] - t_r))*2 + max(abs(x[i] - s_r), abs(x[i] - t_r))
    else:
        route1 = 10**10
    
    if s_r != None and t_l != None:
        if (x[i] - s_r)*(x[i] - t_l) >0:
            route2 = max(abs(x[i] - s_r), abs(x[i] - t_l))
        else:
            route2 = min(abs(x[i] - s_r), abs(x[i] - t_l))*2 + max(abs(x[i] - s_r), abs(x[i] - t_l))
    else:
        route2 = 10**10
    
    if s_l != None and t_r != None:
        if (x[i] - s_l)*(x[i] - t_r) > 0:
            route3 = max(abs(x[i] - s_l), abs(x[i] - t_r))
        else:
            route3 = min(abs(x[i] - s_l), abs(x[i] - t_r))*2 + max(abs(x[i] - s_l), abs(x[i] - t_r))
    else:
        route3 = 10**10 
        
    if s_l != None and t_l != None:
        if (x[i] - s_l)*(x[i] - t_l) >0:
            route4 = max(abs(x[i] - s_l), abs(x[i] - t_l))
        else:
            route4 = min(abs(x[i] - s_l), abs(x[i] - t_l))*2 + max(abs(x[i] - s_l), abs(x[i] - t_l))
    else:
        route4 = 10**10
        
    print(min(route1,route2,route3,route4))

Submission status: Wrong Answer

Solution code:
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


results = ""
for _ in range(q):
    x = int(input())
    si = near(x, s)
    spos = [si, si + 1] if s[si] < x else [si, si - 1]
    ti = near(x, t)
    tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]

    result = float("inf")
    for sindex in spos:
        for tindex in tpos:
            v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
            v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
#             result = min(
#                 float("inf") if math.isnan(v1) else v1,
#                 float("inf") if math.isnan(v2) else v2,
#                 result
#             )
# 
    results += str(result) + "\n"
print(results)

Submission status: Wrong Answer

Solution code:
import bisect

INF = 10**18
A = 0
B = 0
A, B, Q = [int(s) for s in input().split()]

s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]


# def bisect_right(a, x):
#     l, r = 0, len(a)
#     while r - l > 1:
#         m = (l + r) // 2
#         if a[m] <= x:
#             l = m
#         else:
#             r = m
#     return r


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

    sr = bisect.bisect_right(s, q)
    sl = sr - 1
    if sr >= A:
        sr = A - 1
    if sl < 0:
        sl = 0

    tr = bisect.bisect_right(t, q)
    tl = tr - 1
    if tr >= B:
        tr = B - 1
    if tl < 0:
        tl = 0

    res = INF
    for _s in [s[sl], s[sr]]:
        for _t in [t[tl], t[tr]]:
            res = min(res, abs(q-_s) + abs(_t-_s), abs(q-_t)+abs(_t-_s))

    print(res)

Submission status: Wrong Answer

Solution code:
INF = 10**18
A = 0
B = 0
A, B, Q = [int(s) for s in input().split()]

s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]


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


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

    sr = bisect_right(s, q)
    sl = sr - 1
    if sr >= A:
        sr = A - 1
    if sl < 0:
        sl = 0

    tr = bisect_right(t, q)
    tl = tr - 1
    if tr >= B:
        tr = B - 1
    if tl < 0:
        tl = 0

    res = INF
    for _s in [s[sl], s[sr]]:
        for _t in [t[tl], t[tr]]:
            res = min(res, abs(q-_s) + abs(_t-_s), abs(q-_t)+abs(_t-_s))

    print(res)

Submission status: Wrong Answer

Solution code:
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


for _ in range(q):
    x = int(input())
    si = near(x, s)
    spos = [si, si + 1] if s[si] < x else [si, si - 1]
    ti = near(x, t)
    tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]

    result = float("inf")
    for sindex in spos:
        for tindex in tpos:
            v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
            v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
#             result = min(
#                 float("inf") if math.isnan(v1) else v1,
#                 float("inf") if math.isnan(v2) else v2,
#                 result
#             )
# 
#     print(result)

Submission status: Wrong Answer

Solution code:
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


for _ in range(q):
    x = int(input())
    si = near(x, s)
    spos = [si, si + 1] if s[si] < x else [si, si - 1]
    ti = near(x, t)
    tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]

#     result = float("inf")
#     for sindex in spos:
#         for tindex in tpos:
#             v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
#             v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
#             result = min(
#                 float("inf") if math.isnan(v1) else v1,
#                 float("inf") if math.isnan(v2) else v2,
#                 result
#             )
# 
#     print(result)

Submission status: Wrong Answer

Solution code:
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


for _ in range(q):
    x = int(input())
    near(x, s)
    near(x, t)
# 
#     si = near(x, s)
#     spos = [si, si + 1] if s[si] < x else [si, si - 1]
#     ti = near(x, t)
#     tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]
# 
#     result = float("inf")
#     for sindex in spos:
#         for tindex in tpos:
#             v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
#             v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
#             result = min(
#                 float("inf") if math.isnan(v1) else v1,
#                 float("inf") if math.isnan(v2) else v2,
#                 result
#             )
# 
#     print(result)

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = list(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)]

for x in X:
    s_min = bisect.bisect_left(s, x) - 1
    t_min = bisect.bisect_left(t, x) - 1


    if s_min == -1:
        if t_min == -1:
            print(max(s[0]-x, t[0]-x))
        elif t_min == B-1:
            print(s[0]-t[t_min]+min(s[0]-x, t[t_min]-x))
        else:
            print(min(t[t_min+1]-x, s[0]-t[t_min]+min(s[0]-x, t[t_min]-x)))
    elif s_min == A-1:
        if t_min == -1:
            print(t[0]-s[s_min] + min(t[0]-x, x-s[s_min]))
        elif t_min == B-1:
            print(max(x-s[s_min], x-t[t_min]))
        else:
            print(min(x-t[t_min], t[t_min+1]-s[s_min] + min(t[t_min+1]-x, x-s[s_min])))
    else:
        if t_min == -1:
            if t[0] < s[s_min+1]:
                print(min(s[s_min + 1] - x, t[0] - s[s_min] + min(t[0] - x, x-s[s_min])))
            else:
                print(t[0]-x)
        elif t_min == B-1:
            print(min(x - s[s_min], s[s_min + 1] - t[t_min] + min(s[s_min + 1] - x, x - t[t_min])))
        else:
            if s[s_min] < t[t_min]:
                if s[s_min+1] < t[t_min+1]:
                    print(min(x-s[s_min], t[t_min+1] - x, s[s_min+1] - t[t_min] + min(x-t[t_min], s[s_min+1]-x)))
                else:
                    print(min(x-s[s_min], s[s_min+1]-x))
            else:
                if s[s_min+1] < t[t_min+1]:
                    print(min(t[t_min+1]-x, x-t[t_min]))
                else:
                    print(min(x-t[t_min], s[s_min+1] - x, t[t_min+1] - s[s_min] + min(x-s[s_min], t[t_min+1]-x)))

Submission status: Wrong Answer

Solution code:
def inpl():
    return list(map(int, input().split()))


def binary_search(Array, x):
    le = 0
    ri = len(Array)
    if ri == 1 or x < Array[0]:
        return x - Array[0], Array[0] - x
    elif x > Array[-1]:
        return x - Array[-1], Array[-1] - x
    while ri - le > 0:
        i = (le + ri) // 2
        if (Array[i] == x):
            return 0, 0
        elif (Array[i] < x):
            le = i + 1
            zou = True
        else:
            ri = i
            zou = False
    if zou is True:
        return x - Array[ri - 1], Array[ri] - x
    else:
        return x - Array[ri], Array[ri + 1] - x


A, B, Q = inpl()
shrine = [int(input()) for _ in range(A)]
temple = [int(input()) for _ in range(B)]
query = [int(input()) for _ in range(Q)]

# shrine.sort()
# temple.sort()

for q in query:
    s_left, s_right = binary_search(shrine, q)
    t_left, t_right = binary_search(temple, q)

    ans = 10**12
    if s_left < 0:
        if t_left < 0:
            ans = max(s_right, t_right)
        elif t_right < 0:
            ans = t_left + s_right + min(t_left, s_right)
        else:
            ans = max(s_right, t_right)
            ans = min(ans, t_left + s_right + min(t_left, s_right))
    elif s_right < 0:
        if t_left < 0:
            ans = s_left + t_right + min(s_left, t_right)
        elif t_right < 0:
            ans = max(s_left, t_left)
        else:
            ans = max(s_left, t_left)
            ans = min(ans, t_right + s_left + min(s_left, t_right))
    else:
        if t_left < 0:
            ans = max(s_right, t_right)
            ans = min(ans, t_right + s_left + min(t_right, s_left))
        elif t_right < 0:
            ans = max(s_left, t_left)
            ans = min(ans, t_left + s_right + min(t_left, s_right))
        else:
            ans = min(max(s_left, t_left), max(s_right, t_right))
            ans = min(ans, s_left + t_right + min(s_left, t_right))
            ans = min(ans, t_left + s_right + min(t_left, s_right))
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
# coding: utf-8
#
# node.py : 二分木の操作関数 (非再帰版)
#
#           Copyright (C) 2007 Makoto Hiroi
#

# 節の定義
class Node:
    def __init__(self, x):
        self.data  = x
        self.left  = None
        self.right = None

# 右回転
def rotate_right(node):
    lnode = node.left
    node.left = lnode.right
    lnode.right = node
    return lnode

# 左回転
def rotate_left(node):
    rnode = node.right
    node.right = rnode.left
    rnode.left = node
    return rnode

# データの探索
def search(node, x):
    while node is not None:
        if node.data == x: return True
        if x < node.data:
            node = node.left
        else:
            node = node.right
    return False

# データの挿入
def insert(node, x):
    if node is None: return Node(x)
    p = node
    while True:
        if p.data == x: return node
        elif x < p.data:
            if p.left is None:
                p.left = Node(x)
                break
            p = p.left
        else:
            if p.right is None:
                p.right = Node(x)
                break
            p = p.right
    return node

#
# データの削除
#

# データを探す
def _search(node, x):
    pnode = None
    while node is not None:
        if node.data == x: return node, pnode
        pnode = node
        if x < node.data:
            node = node.left
        else:
            node = node.right
    return None, None

# 最小値を探す
def _search_min(node):
    pnode = None
    while node.left is not None:
        pnode = node
        node = node.left
    return node, pnode

# 削除
def delete(root, x):
    if root is None: return None    # 空の木
    node, pnode = _search(root, x)  # 探索
    if node is None: return root    # 削除データなし
    if node.left is not None and node.right is not None:
        # 子が二つある場合
        # 右部分木の最小値を探して置き換える
        min_node, min_pnode = _search_min(node.right)
        node.data = min_node.data
        if min_pnode is not None:
            pnode = min_pnode
            node = min_node
        else:
            pnode = node
            node = min_node
    # 節を削除する
    if node.left is None:
        cnode = node.right
    else:
        cnode = node.left
    if pnode is None:
        return cnode            # root の削除
    elif pnode.left == node:
        pnode.left = cnode
    else:
        pnode.right = cnode
    return root

# 巡回
def traverse(node):
    if node is not None:
        for x in traverse(node.left):
            yield x
        yield node.data
        for x in traverse(node.right):
            yield x
 
input = sys.stdin.readline
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)]
lens, lent = len(s), len(t)
treeA= Node(s[0])
for i in range(1,lens): 
    insert(treeA,s[i]) # 木を作る
treeB= Node(t[0])
for i in range(lent):
    insert(treeB,t[i])
print(1)

Submission status: Wrong Answer

Solution code:
#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
"""
n, a, b, c = map(int, input().split())
abc = [a,b,c]
x = [int(input()) for i in range(n)]
x.sort()
ans = []
flg = 0
for i in x:
    if i in abc:
        ans.append(i)
        flg = 1
if flg:
    for i in ans:
        abc.remove(i)
        x.remove(i)

"""
#D

a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    print(s_l, s_r, t_l, t_r)
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = INF
for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
        d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
        res = min(res, d1, d2)
        print(res)

Submission status: Wrong Answer

Solution code:
def diff_leng(s_list, t_list, x):
    s2 = [s - x for s in s_list]
    t2 = [t - x for t in t_list]
    return s2, t2


A, B, Q = map(int, input().split())
s_list, t_list, x_list = [], [], []
ans = 0

s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for i in range(Q)]

for i in x_list:
    ans_list = []
    s2, t2 = diff_leng(s_list, t_list, i)
    for j in s2:
        for k in t2:
            ans_list.append(abs(j) + abs(k-j))
    print(min(ans_list))

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


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

    si = near(x, s)
    spos = [si, si + 1] if s[si] < x else [si, si - 1]
    ti = near(x, t)
    tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]

    result = float("inf")
    for sindex in spos:
        for tindex in tpos:
            result = min(
                abs(x - s[sindex]) + abs(s[sindex] - t[tindex]),
                abs(x - t[tindex]) + abs(t[tindex] - s[sindex]),
                result
            )

    print(result)

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 j in range(b) ]
res = []
for i in range(q):
  start = int(input())
  near_s_in = min(zip([ abs(j - start) for j in s], range(len(s))))[1]
  near_t_in = min(zip([ abs(k - start) for k in t], range(len(t))))[1]

  print(near_s_in)
  print(near_t_in)
  if start <= s[near_s_in] and start <= t[near_t_in]:
    res.append(min(s[near_s_in], t[near_t_in]) - start + abs(t[near_t_in] - s[near_s_in]))
  elif start >= s[near_s_in] and start >= t[near_t_in]:
    res.append(start - max(s[near_s_in], t[near_t_in]) + abs(t[near_t_in] - s[near_s_in]))
  else:
    res.append(min(abs(s[near_s_in] - start), abs(t[near_t_in] - start)) + abs(t[near_t_in] - s[near_s_in]))

for h in res:
  print(h)

Submission status: Wrong Answer

Solution code:
import bisect
import sys

class Avltree:
    def __init__(self,key=None):
        self.key = key
        self.left = None
        self.right = None 
        self.balance = "Even"
    def search(self,key):
        if self.key == None:
            return None
        if self.key > key:
            if self.left == None:
                return None
            else:
                return self.left.search(key)
        if self.key < key:
            if self.right == None:
                return None
            else:
                return self.right.search(key)
        return self # self.key == keyの場合
        
    def search_lower(self,key,key_lower):
        if self.key == None:
            return key_lower
        if self.key > key:
            if self.left == None:
                return key_lower
            else:
                return self.left.search_lower(key,key_lower)
        if self.key < key:
            key_lower = self.key
            if self.right == None:
                return key_lower
            else:
                return self.right.search_lower(key,key_lower)
        #self.key == keyの場合
        if self.left == None:
            return key_lower
        else:
            if key_lower == None:
                return self.left.end_higher(self.left.key)
            else:
                return max(key_lower,self.left.end_higher(self.left.key))
    
    def search_higher(self,key,key_higher):
        if self.key == None:
            return key_higher
        if self.key > key:
            key_higher = self.key
            if self.left == None:
                return key_higher
            else:
                return self.left.search_higher(key,key_higher)
        if self.key < key:
            if self.right == None:
                return key_higher
            else:
                return self.right.search_higher(key,key_higher)
        #self.key == keyの場合
        if self.right == None:
            return key_higher
        else:
            if key_higher == None:
                return self.right.end_lower(self.right.key)
            else:
                return min(key_higher,self.right.end_lower(self.right.key))
        
    
    def end_lower(self,end_lower_key):
        if self.left == None:
            return end_lower_key
        else:
            end_lower_key = self.left.key
            return self.left.end_lower(end_lower_key)
    def end_higher(self,end_higher_key):
        if self.right == None:
            return end_higher_key
        else:
            end_higher_key = self.right.key
            return self.right.end_higher(end_higher_key)
    
            
    def DoubleRightRotation(self):
        tl = self.left
        self.left = tl.right.right
        tl.right.right = self # selfはそのノード
        tlr = tl.right
        tl.right = tlr.left
        tlr.left = tl
        if tlr.balance == "Left":
            tlr.right.balance = "Right"
            tlr.left.balance = "Even"
        elif tlr.balance == "Right":
            tlr.right.balance = "Even"
            tlr.left.balance = "Left"
        elif tlr.balance == "Even":
            tlr.right.balance = "Even"
            tlr.left.balance = "Even"
        tlr.balance = "Even"
        return tlr
    def DoubleLeftRotation(self):
        tr = self.right
        self.right = tr.left.left
        tr.left.left = self
        trl = tr.left
        tr.left = trl.right
        trl.right = tr
        if trl.balance == "Right":
            trl.left.balance = "Left"
            trl.right.balance = "Even"
        elif trl.balance == "Left":
            trl.left.balance = "Even"
            trl.right.balance = "Right"
        elif trl.balance == "Even":
            trl.left.balance = "Even"
            trl.right.balance = "Even"
        trl.balance = "Even"
        return trl
    def SingleLeftRotation(self):
        tr = self.right
        tr.balance = "Even"
        self.balance = "Even"
        self.right = tr.left
        tr.left = self
        return tr
    def SingleRightRotation(self):
        tl = self.left
        tl.balance = "Even"
        self.balance = "Even"
        self.left = tl.right
        tl.right = self
        return tl
    def replace(self,p,v): # 親ノードpの下にある自分(self)をvに置き換える。
        if p.left == self:
            p.left = v
        else :
            p.right = v 
    def insert(self,key): # rootでのみ呼ばれる挿入
        if self.key == None: # rootを含むrotationはしないことにする。
            self.key = key
            return self
        if key < self.key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                self.left.insertx(self,key)
        elif key > self.key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                self.right.insertx(self,key)
        else: # key == self.key:
            pass # do not overwrite
    def insertx(self,p,key): # replaceを呼ぶために一つ上の親を持っているinsert
        if self.key > key:
            if self.left == None:
                self.left = Avltree(key)
            else:
                if not self.left.insertx(self, key): # 左の木が生長しなければ、
                    return False # 成長しない
            if self.balance == "Right":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Left"
                return True # 成長した
            elif self.balance == "Left":
                if self.left.balance == "Right":
                    self.replace(p,self.DoubleRightRotation())
                elif self.left.balance == "Left":
                    self.replace(p,self.SingleRightRotation())
                return False # rotationを行うと成長しない
        if self.key < key:
            if self.right == None:
                self.right = Avltree(key)
            else:
                if not self.right.insertx(self, key):
                    return False 
            if self.balance == "Left":
                self.balance = "Even"
                return False
            elif self.balance == "Even":
                self.balance = "Right"
                return True
            elif self.balance == "Right":
                if self.right.balance == "Left": 
                    self.replace(p,self.DoubleLeftRotation())
                elif self.right.balance == "Right":
                    self.replace(p,self.SingleLeftRotation())
                return False
        return False # self.key == keyの時は何もしないので成長もしない 

    def to_s(self):
        return self.key
    def left_s(self):
        if self.left == None:
            return None
        return (self.left).key
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).key
        
input = sys.stdin.readline
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)]
lens, lent = len(s), len(t)
treeA, treeB = Avltree(), Avltree()
for i in s: treeA.insert(i) # 木を作る
for i in t: treeB.insert(i)
for i in x:
    # atob
    p1,p2 = 0, 0
    alow = treeA.search_lower(i,10**15)
    ahigh = treeA.search_higher(i,10**15)
    p1 += abs(alow-i)
    p2 += abs(ahigh-i)
    p1 += min(abs(treeB.search_lower(alow,10**15)-alow),abs(treeB.search_higher(alow,10**15)-alow))
    p2 += min(abs(treeB.search_lower(ahigh,10**15)-ahigh),abs(treeB.search_higher(ahigh,10**15)-ahigh))
    ans = min(p1,p2)
    # print(p1,p2)
    # btoa
    p3,p4 = 0, 0
    blow = treeB.search_lower(i,10**15)
    bhigh = treeB.search_higher(i,10**15)
    p3 += abs(blow-i)
    p4 += abs(bhigh-i)
    p3 += min(abs(treeA.search_lower(blow,10**15)-blow),abs(treeA.search_higher(blow,10**15)-blow))
    p4 += min(abs(treeA.search_lower(bhigh,10**15)-bhigh),abs(treeA.search_higher(bhigh,10**15)-bhigh))
    ans = min(p3,p4,ans)
    # print(p3,p4)
    print(1)