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:
import sys
input = sys.stdin.readline

A,B,Q = map(int,input().split())
jinjya = [-float('inf')]+[int(input()) for _ in range(A)]+[float('inf')]#範囲を絞るために両サイドに0と無限を挿入
tera =[-float('inf')]+[int(input()) for _ in range(B)]+[float('inf')]

for i in range(Q):
  x=int(input())
#出発地点の前後の神社と寺の場所を知りたい。つまり出発点より前にある対象のインデックス
  L=0
  R=A+2
  while R-L>1:
    P=(L+R)//2
    if x<jinjya[P]:
      R=P
    else:
      L=P
  jinjya_L=L
  
  L=0
  R=B+2
  while R-L>1:
    P=(L+R)//2
    if x<tera[P]:
      R=P
    else:
      L=P
  tera_L=L
#xの左側の神社と寺のインデックスがわかったのでそれらに対して距離を測って最小を探す。
  a=jinjya[jinjya_L]
  b=jinjya[jinjya_L+1]
  c=tera[tera_L]
  d=tera[tera_L+1]
  answer=min(abs(x-a)+abs(a-c),abs(x-c)+abs(c-a),abs(x-b)+abs(b-c),abs(x-c)+abs(c-b),abs(x-a)+abs(a-d),abs(x-d)+abs(d-a),abs(x-b)+abs(b-d),abs(x-d)+abs(d-b))
  print(answer)

Submission status: Wrong Answer

Solution code:
import sys


sys.setrecursionlimit(10000)


def my_search(l, low, high, t):
    n = (low+high)//2
    if t > l[n]:
        low = n
    else:
        high = n
    if high == low + 1:
        return low
    else:
        return my_search(l, low, high, t)


a,b,q = [int(_) for _ in input().split()]
s_l = [-float('inf')]
t_l = [-float('inf')]
x_l = []
for i in range(a):
    s_l.append(int(input()))
s_l.append(float('inf'))
for i in range(b):
    t_l.append(int(input()))
t_l.append(float('inf'))
for i in range(q):
    x_l.append(int(input()))
for i in range(q):
    x = x_l[i]
    s1_id = my_search(s_l, 0, len(s_l)-1, x)
    s2_id = s1_id + 1
    s1 = s_l[s1_id]
    s2 = s_l[s2_id]
    t1_id = my_search(t_l, 0, len(t_l)-1, x)
    t2_id = t1_id + 1
    t1 = t_l[t1_id]
    t2 = t_l[t2_id]
    ans = min(
        abs(t1-x)+abs(s1-t1),
        abs(t1-x)+abs(s2-t1),
        abs(t2-x)+abs(s1-t2),
        abs(t2-x)+abs(s2-t2),
        abs(s1-x)+abs(t1-s1),
        abs(s1-x)+abs(t2-s1),
        abs(s2-x)+abs(t1-s2),
        abs(s2-x)+abs(t2-s2)
    )
    print(ans)

Submission status: Wrong Answer

Solution code:
def my_search(l, low, high, t):
    n = (low+high)//2
    if t > l[n]:
        low = n
    else:
        high = n
    if high == low + 1:
        return low
    else:
        return my_search(l, low, high, t)


a,b,q = [int(_) for _ in input().split()]
s_l = [-float('inf')]
t_l = [-float('inf')]
x_l = []
for i in range(a):
    s_l.append(int(input()))
s_l.append(float('inf'))
for i in range(b):
    t_l.append(int(input()))
t_l.append(float('inf'))
for i in range(q):
    x_l.append(int(input()))
for i in range(q):
    x = x_l[i]
    s1_id = my_search(s_l, 0, len(s_l)-1, x)
    s2_id = s1_id + 1
    s1 = s_l[s1_id]
    s2 = s_l[s2_id]
    t1_id = my_search(t_l, 0, len(t_l)-1, x)
    t2_id = t1_id + 1
    t1 = t_l[t1_id]
    t2 = t_l[t2_id]
    ans = min(
        abs(t1-x)+abs(s1-t1),
        abs(t1-x)+abs(s2-t1),
        abs(t2-x)+abs(s1-t2),
        abs(t2-x)+abs(s2-t2),
        abs(s1-x)+abs(t1-s1),
        abs(s1-x)+abs(t2-s1),
        abs(s2-x)+abs(t1-s2),
        abs(s2-x)+abs(t2-s2)
    )
    print(ans)

Submission status: Wrong Answer

Solution code:
a,b,q = list(map(int, input().split()))

s = [0]*a
t = [0]*b
x = [0]*q
for i in range(a):
    s[i] = int(input())
for i in range(b):
    t[i] = int(input())
for i in range(q):
    x[i] = int(input())
s.sort()
t.sort()

INT_MAX = 10**10 + 10

#print(s)
#print(t)
import bisect
for xi in x:
    i = bisect.bisect_left(s,xi)
    s_right = INT_MAX
    if i < len(s):
        s_right = s[i] - xi
    s_left = INT_MAX
    if i != 0:
        s_left = xi - s[i-1]
    j = bisect.bisect_left(t,xi)
    t_right = INT_MAX
    if j < len(t):
        t_right = t[j] - xi
    t_left = INT_MAX
    if j != 0:
        t_left = xi - t[j-1]
    #print(i,j,xi)
    #print(s_left,s_right,t_left,t_right)
    ans = min(max(s_right,t_right), max(s_left,t_left), min(s_left,t_right)*2+max(s_left,t_right), min(s_right,t_left)*2+max(s_right,t_left))
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect as bi

def dL(x,li):
    ind=bi.bisect_left(li,x)
    if ind==0:
        return 10**10
    else:
        return x-li[ind-1]

def dR(x,li):
    ind=bi.bisect_left(li,x)
    if ind==len(li):
        return 10**10
    else:
        return li[ind]-x

def ans(x,s,t):
    # 西の神社から訪ねる(d1)
    d1=dL(x,s)
    if d1<dL(x,t):
        d1+=min(toLt[x-d1],toRt[x-d1])
    d2=dR(x,s)
    if d2<dR(x,t):
        d2+=min(toRt[x+d2],toLt[x+d2])
    d3=dL(x,t)
    if d3<dL(x,s):
        d3+=min(toLs[x-d3],toRs[x-d3])
    d4=dR(x,t)
    if d4<dR(x,s):
        d4+=min(toLs[x+d4],toRs[x+d4])
    
    return min(d1,d2,d3,d4)

A,B,Q=map(int,input().split())
s=[]
for i in range(A):
    s.append(int(input()))
t=[]
for i in range(B):
    t.append(int(input()))

# 各神社の座標につき，東西の最寄りの寺までの距離を計算しておく
toLt={};toRt={}
for i in range(A):
    toLt[s[i]]=dL(s[i],t)
    toRt[s[i]]=dR(s[i],t)
# 同上
toLs={};toRs={}
for i in range(B):
    toLs[t[i]]=dL(t[i],s)
    toRs[t[i]]=dR(t[i],s)

for i in range(Q):
    print(ans(int(input()),s,t))

Submission status: Wrong Answer

Solution code:
import bisect as bi

INF=10**10
A,B,Q=map(int,input().split())
s=[]
for i in range(A):
    s.append(int(input()))

t=[]
for i in range(B):
    t.append(int(input()))

for i in range(Q):
    x=int(input())
    sl=bi.bisect_left(s,x)

    if sl==0:
        dsl=INF;dsr=s[0]-x
    elif sl==A:
        dsl=x-s[A-1];dsr=INF
    else:
        dsl=x-s[sl-1];dsr=s[sl]-x

    tl=bi.bisect_left(t,x)
    
    if tl==0:
        dtl=INF;dtr=t[0]-x
    elif tl==B:
        dtl=x-t[B-1];dtr=INF
    else:
        dtl=x-t[tl-1];dtr=t[tl]-x
    
 #   print(dsl,dsr)
 #   print(dtl,dtr)

    print(min(max(dsl,dtl),max(dsr,dtr),2*dsl+dtr,2*dtl+dsr,dtl+2*dsr,dsl+2*dtr))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
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)]

INF = 10 ** 11
for x in X:
  ids = bisect.bisect_left(S,x)
  if ids == 0:
    sl = -INF
    sr = S[0]
  elif ids == A:
    sl = S[A-1]
    sr = INF
  else:
    sl = S[ids-1]
    sr = S[ids]
  idt = bisect.bisect_left(T,x)
  if idt == 0:
    tl = -INF
    tr = T[0]
  elif idt == B:
    tl = T[B-1]
    tr = INF
  else:
    tl = T[idt-1]
    tr = T[idt]
  print(min(max(x-sl,x-tl),(x-sl)+(tr-sl),(tr-x)+(tr-sl),max(sr-x,tr-x)))

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())
S = [-float("inf")]
T = [-float("inf")]
X = []

for i in range(A):
    S.append(int(input()))
S.append(float("inf"))

for i in range(B):
    T.append(int(input()))
T.append(float("inf"))


for i in range(Q):
    X.append(int(input()))

for x in X:
    si = bisect.bisect_right(S, x)
    ti = bisect.bisect_right(T, x)
    
    s01 = [S[si-1], S[si]]
    t01 = [T[ti-1], T[ti]]
    
    ll = []
    for ss in s01:
        for tt in t01:
            l = abs(ss - tt) + min(abs(ss - x), abs(tt - x))
            ll.append(l)
    print(min(ll))

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())
S = [-float("inf")]
T = [-float("inf")]
X = []

for i in range(A):
    S.append(int(input()))
S.append(float("inf"))

for i in range(B):
    T.append(int(input()))
T.append(float("inf"))


for i in range(Q):
    X.append(int(input()))

for x in X:
    si = bisect.bisect_left(S, x)
    ti = bisect.bisect_left(T, x)
    
    s01 = [S[si-1], S[si]]
    t01 = [T[ti-1], T[ti]]
    
    ll = []
    for ss in s01:
        for tt in t01:
            l = abs(ss - tt) + min(abs(ss - x), abs(tt - x))
            ll.append(l)
    print(min(ll))

Submission status: Wrong Answer

Solution code:
import bisect

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)] 

def __index(i, _list):
    _index = bisect.bisect_left(_list, i)
    if _index==0:
        near=_list[_index]
    elif _index==len(_list):
        near=_list[_index-1]
    else:
        if abs(i-_list[_index-1])>abs(i-_list[_index]):
            near=_list[_index]
        else:
            near=_list[_index-1]
    return near

for i in x:
    ans=0
    s_near = __index(i, s)
    t_near = __index(s_near, t)
    ans=abs(i-s_near)+abs(t_near-s_near)
    t_near = __index(i, t)
    s_near = __index(t_near, s)
    ans=min(abs(i-t_near)+abs(t_near-s_near), ans)
    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 _ in range(a)]+[inf]
T = [-inf]+[int(input()) for _ in range(b)]+[inf]
ans = inf
for _ in range(q):
  x = int(input())
  b,d = bisect.bisect_right(S,x),bisect.bisect_right(T,x)
  for s in [S[b-1],S[b]]:
    for t in [T[d-1],T[d]]:
      tmp1,tmp2 = abs(s-x)+abs(s-t) ,abs(t-x)+abs(t-s)
      ans = min(ans ,tmp1,tmp2)
  print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

a,b,q=map(int,input().split())
tem=[-10**18]+[int(input()) for _ in range(a)]+[10**18]
shr=[-10**18]+[int(input()) for _ in range(b)]+[10**18]
ans=[]

for _ in range(q):
    x=int(input())
    t=bisect.bisect_right(tem, x)
    s=bisect.bisect_right(shr, x)
    dt=[x-tem[t-1], tem[t]-x]
    ds=[x-shr[s-1], shr[s]-x]

    m=min(dt+ds)
    if m in dt:
        m_ind=dt.index(m)
        m2=min(ds)
        m2_ind=ds.index(m2)
    else:
        m_ind=ds.index(m)
        m2=min(dt)
        m2_ind=dt.index(m2)

    if m_ind==m2_ind:
        ans.append(m2)
    else:
        ans.append(m2 + m*2)

print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left, bisect_right
from operator import itemgetter
from fractions import gcd
from math import ceil, floor, sqrt
from copy import deepcopy
from collections import Counter, deque
import heapq
from functools import reduce
from random import shuffle
sys.setrecursionlimit(200000)
# local only
# if not __debug__:
#     fin = open('text.txt', 'r')
#     sys.stdin = fin
# local only
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None
def exit(*arg): print(*arg); sys.exit()
# template

class RBNode:
    def __init__(self, val, is_red=True, parent=None, left=None, right=None):
        self.val = val
        self.parent = parent
        self.left = left
        self.right = right
        self.color = is_red


class RBTree:
    def __init__(self):
        self.root = None

    def left_rotate(self, node):
        # set the node as the left child node of the current node's right node
        right_node = node.right
        if right_node is None:
            return
        else:
            # right node's left node become the right node of current node
            node.right = right_node.left
            if right_node.left is not None:
                right_node.left.parent = node
            right_node.parent = node.parent
            # check the parent case
            if node.parent is None:
                self.root = right_node
            elif node is node.parent.left:
                node.parent.left = right_node
            else:
                node.parent.right = right_node
            right_node.left = node
            node.parent = right_node

    def right_rotate(self, node):
        # set the node as the right child node of the current node's left node
        left_node = node.left
        if left_node is None:
            return
        else:
            # left node's right  node become the left node of current node
            node.left = left_node.right
            if left_node.right is not None:
                left_node.right.parent = node
            left_node.parent = node.parent
            # check the parent case
            if node.parent is None:
                self.root = left_node
            elif node is node.parent.left:
                node.parent.left = left_node
            else:
                node.parent.right = left_node
            left_node.right = node
            node.parent = left_node

    def insert(self, node):
        # the inserted node's color is default is red
        root = self.root
        insert_node_parent = None
        # find the position of inserted node
        while root is not None:
            insert_node_parent = root
            if insert_node_parent.val < node.val:
                root = root.right
            else:
                root = root.left
        # set the n ode's parent node
        node.parent = insert_node_parent
        if insert_node_parent is None:
            # case 1  inserted tree is null
            self.root = node
        elif insert_node_parent.val > node.val:
            # case 2 not null and find left or right
            insert_node_parent.left = node
        else:
            insert_node_parent.right = node
        node.left = None
        node.right = None
        node.color = 1
        # fix the tree to
        self.fix_insert(node)

    def fix_insert(self, node):
        # case 1 the parent is null, then set the inserted node as root and color = 0
        if node.parent is None:
            node.color = 0
            self.root = node
            return
            # case 2 the parent color is black, do nothing
        # case 3 the parent color is red
        while node.parent and node.parent.color == 1:
            if node.parent is node.parent.parent.left:
                uncle_node = node.parent.parent.right
                if uncle_node and uncle_node.color == 1:
                    # case 3.1 the uncle node is red
                    # then set parent and uncle color is black and grandparent is red
                    # then node => node.parent
                    node.parent.color = 0
                    node.parent.parent.right.color = 0
                    node.parent.parent.color = 1
                    node = node.parent.parent
                    continue
                elif node is node.parent.right:
                    # case 3.2 the uncle node is black or null, and the node is right of parent
                    # then set his parent node is current node
                    # left rotate the node and continue the next
                    node = node.parent
                    self.left_rotate(node)
                # case 3.3 the uncle node is black and parent node is left
                # then parent node set black and grandparent set red
                node.parent.color = 0
                node.parent.parent.color = 1
                self.right_rotate(node.parent.parent)
            else:
                uncle_node = node.parent.parent.left
                if uncle_node and uncle_node.color == 1:
                    # case 3.1 the uncle node is red
                    # then set parent and uncle color is black and grandparent is red
                    # then node => node.parent
                    node.parent.color = 0
                    node.parent.parent.left.color = 0
                    node.parent.parent.color = 1
                    node = node.parent.parent
                    continue
                elif node is node.parent.left:
                    # case 3.2 the uncle node is black or null, and the node is right of parent
                    # then set his parent node is current node
                    # left rotate the node and continue the next
                    node = node.parent
                    self.right_rotate(node)
                # case 3.3 the uncle node is black and parent node is left
                # then parent node set black and grandparent set red
                node.parent.color = 0
                node.parent.parent.color = 1
                self.left_rotate(node.parent.parent)
        self.root.color = 0

    def transplant(self, node_u, node_v):
        """
        replace u with v
        :param node_u: replaced node
        :param node_v:
        :return: None
        """
        if node_u.parent is None:
            self.root = node_v
        elif node_u is node_u.parent.left:
            node_u.parent.left = node_v
        elif node_u is node_u.parent.right:
            node_u.parent.right = node_v
        # check is node_v is None
        if node_v:
            node_v.parent = node_u.parent

    def find(self, key):
        node = self.root
        while node is not None:
            if node.val < key:
                node = node.right
            elif node.val > key:
                node = node.left
            else:
                return node

    def maximum(self, node):
        """
        find the max node when node regard as a root node
        :param node:
        :return: max node
        """
        temp_node = node
        while temp_node.right:
            temp_node = temp_node.right
        return temp_node

    def minimum(self, node):
        """
        find the minimum node when node regard as a root node
        :param node:
        :return: minimum node
        """
        temp_node = node
        while temp_node.left:
            temp_node = temp_node.left
        return temp_node

    def search_higher(self, key, key_higher=None):
        node = self.root
        if node.val is None:
            return key_higher
        ans = key_higher
        while node is not None:
            if node.val > key:
                ans = min(ans, node.val)
                node = node.left
            elif node.val <= key:
                node = node.right
        return ans

    def search_lower(self, key, key_lower=None):
        node = self.root
        if node.val is None:
            return key_lower
        ans = key_lower
        while node is not None:
            if node.val < key:
                ans = max(ans, node.val)
                node = node.right
            elif node.val >= key:
                node = node.left
        return ans

    def delete(self, node):
        # find the node position
        node_color = node.color
        if node.left is None:
            temp_node = node.right
            self.transplant(node, node.right)
        elif node.right is None:
            temp_node = node.left
            self.transplant(node, node.left)
        else:
            # both child exits ,and find minimum child of right child
            node_min = self.minimum(node.right)
            node_color = node_min.color
            temp_node = node_min.right
            ##
            if node_min.parent != node:
                self.transplant(node_min, node_min.right)
                node_min.right = node.right
                node_min.right.parent = node_min
            self.transplant(node, node_min)
            node_min.left = node.left
            node_min.left.parent = node_min
            node_min.color = node.color
        # when node is black, then need to fix it with 4 cases
        if node_color == 0:
            self.delete_fixup(temp_node)

    def delete_fixup(self, node):
        # 4 cases
        while node != self.root and node.color == 0:
            # node is not root and color is black
            if node == node.parent.left:
                # node is left node
                node_brother = node.parent.right

                # case 1: node's red, can not get black node
                # set brother is black and parent is red
                if node_brother.color == 1:
                    node_brother.color = 0
                    node.parent.color = 1
                    self.left_rotate(node.parent)
                    node_brother = node.parent.right

                # case 2: brother node is black, and its children node is both black
                if (node_brother.left is None or node_brother.left.color == 0) and (
                        node_brother.right is None or node_brother.right.color == 0):
                    node_brother.color = 1
                    node = node.parent
                else:

                    # case 3: brother node is black , and its left child node is red and right is black
                    if node_brother.right is None or node_brother.right.color == 0:
                        node_brother.color = 1
                        node_brother.left.color = 0
                        self.right_rotate(node_brother)
                        node_brother = node.parent.right

                    # case 4: brother node is black, and right is red, and left is any color
                    node_brother.color = node.parent.color
                    node.parent.color = 0
                    node_brother.right.color = 0
                    self.left_rotate(node.parent)
                node = self.root
                break
            else:
                node_brother = node.parent.left
                if node_brother.color == 1:
                    node_brother.color = 0
                    node.parent.color = 1
                    self.left_rotate(node.parent)
                    node_brother = node.parent.right
                if (node_brother.left is None or node_brother.left.color == 0) and (
                        node_brother.right is None or node_brother.right.color == 0):
                    node_brother.color = 1
                    node = node.parent
                else:
                    if node_brother.left is None or node_brother.left.color == 0:
                        node_brother.color = 1
                        node_brother.right.color = 0
                        self.left_rotate(node_brother)
                        node_brother = node.parent.left
                    node_brother.color = node.parent.color
                    node.parent.color = 0
                    node_brother.left.color = 0
                    self.right_rotate(node.parent)
                node = self.root
                break
        node.color = 0

    def output(self):
        res = []
        if not self.root:
            print()
        que = deque()
        root = self.root
        que.append(root)
        while que:
            root = que.popleft()
            if root.parent:
                res.append({'parent': root.parent.val, 'val': root.val, 'color': root.color})
            else:
                res.append({'val': root.val, 'color': root.color})
            if root.left is not None:
                que.append(root.left)
            if root.right is not None:
                que.append(root.right)
        print(*res, sep="\n")
        return



def main():
    # N = ii()
    # P = lmi()
    # L = [0 for i in range(N)]
    # for i in range(N):
    #     L[P[i] - 1] = i
    # L.reverse()
    # ans = 0
    # rb = RBTree()
    # for num, i in enumerate(L):
    #     rb.insert(RBNode(i, 1))
    #     l1 = rb.search_lower(i, -1)
    #     l2 = rb.search_lower(l1, -1)
    #     r1 = rb.search_higher(i, N)
    #     r2 = rb.search_higher(r1, N)
    #     # debug(l2, l1, r1, r2)
    #     ans += (abs(i - l1) * abs(r2 - r1) + abs(l1 - l2) * abs(r1 - i)) * (N - num)
    # print(ans)
    # rb.output()
    rb1 = RBTree()
    a, b, q = map(int, input().split())
    s = int(input())

    rb2 = RBTree()
    for i in range(1, a):
        s = int(input())
        rb1.insert(RBNode(s))
    t = int(input())
    for i in range(1, b):
        t = int(input())
        rb2.insert(RBNode(t))
    print(1)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect

INF = 10**24

A, B, Q = map(int, input().split())
S, T, X = [[int(input()) for _ in range(m)] for m in [A, B, Q]]

S = [-INF] + S + [INF]
T = [-INF] + T + [INF]

for x in X:
    s_i = bisect(S, x)
    t_i = bisect(T, x)
    points = [S[s_i-1], S[s_i], T[t_i-1], T[t_i]]
    min_cost = INF
    for j, p_j in enumerate(points):
        for k, p_k in enumerate(points):
            if j == k:
                continue
            cost = abs(p_j-x) + abs(p_k-p_j)
            min_cost = min(min_cost, cost)
    print(min_cost)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import math

INF = 1e+11
A,B,Q=map(int, input().split())
s = [[0] for _ in range(A+2)]
t = [[0] for _ in range(B+2)]
s[0]=-INF
t[0]=-INF
for i in range(A):
    s[i+1]=int(input())
for i in range(B):
    t[i+1]=int(input())
s[A+1]=INF
t[B+1]=INF
x = [int(input()) for _ in range(Q)]

def BySearch(a,x):
    right = len(a)-1
    left = 0
    while right - left > 1:
        tmp = (right+left)//2
        if a[tmp] <= x:
            left = tmp
        else:
            right = tmp
    if abs(a[right]-x) < abs(a[left]-x): return right
    else: return left
        

for i in range(Q):
    ans = INF
    for j in range(2):
        if j == 0:
            a = s
            b = t
        else:
            a = t
            b = s
        ax = a[BySearch(a,x[i])]
        bx = b[BySearch(b,ax)]
        ans = min(abs(ax-x[i])+abs(bx-ax),ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right

A, B, Q = map(int, input().split())
shrines = [int(input()) for _ in range(A)]
temples = [int(input()) for _ in range(B)]
for _ in range(Q):
  query = int(input())
  sh_pos = bisect_left(shrines, query)
  te_pos = bisect_left(temples, query)
  dis_a = max(abs(shrines[sh_pos-1]-query), abs(temples[te_pos-1]-query))
  dis_b = 10**6
  if te_pos <= B-1:
    dis_b = min(abs(temples[te_pos]-shrines[sh_pos-1])+abs(temples[te_pos]-query), \
              abs(temples[te_pos]-shrines[sh_pos-1])+abs(shrines[sh_pos-1]-query))
  dis_c = 10**6
  if sh_pos <= A-1:
    dis_c = min(abs(shrines[sh_pos]-temples[te_pos-1])+abs(temples[te_pos-1]-query), \
              abs(shrines[sh_pos]-temples[te_pos-1])+abs(shrines[sh_pos]-query))
  dis_d = 10**6
  if sh_pos <= A-1 and te_pos <= B-1:
    dis_d = max(abs(shrines[sh_pos]-query), abs(temples[te_pos]-query))
  print(min(dis_a, dis_b, dis_c, dis_d))

Submission status: Wrong Answer

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

for i in range(Q):
  ans = []
  sin = bisect.bisect_left(S, X[i])
  if sin == 0:
    s1,s2 = S[sin], -(10**9)
  elif sin == A:
    s1,s2 = (10**9),S[sin-1]
  else:
    s1,s2 = S[sin],S[sin-1]
  tin = bisect.bisect_left(T, X[i])
  if tin == 0:
    t1,t2 = T[tin], -(10**9)
  elif tin == B:
    t1,t2 = (10**9),T[tin-1]
  else:
    t1,t2 = T[tin],T[tin-1]
  for j in [s1,s2]:
    for k in [t1,t2]:
      for l in [0,1]:
        if l ==0:
          ans.append(abs(j-X[i])+abs(k-j))
        else:
          ans.append(abs(k-X[i])+abs(j-k)) 
          
  print(min(ans))

Submission status: Wrong Answer

Solution code:
import bisect
I = lambda n: [int(input()) for _ in range(n)]
def f(S, x):
    p = bisect.bisect_left(S, x)
    return min([S[p-1], S[p]], key = lambda s: abs(x-s))
g = lambda x, s, t: abs(x-s)+abs(s-t)
INF = 10**18
a, b, q = map(int, input().split())
S, T, X = [-INF]+I(a)+[INF], [-INF]+I(b)+[INF], I(q)
for x in X:
    s, t = f(S, x), f(T, x)
    print(min(g(x, s, f(T, s)), g(x, t, f(S, t))))

Submission status: Wrong Answer

Solution code:
import bisect
I = lambda n: [int(input()) for _ in range(n)]
def f(S, x):
    p = bisect.bisect_left(S, x)
    return min([S[p-1], S[p]], key = lambda s: abs(x-s))
g = lambda x, s, t: abs(x-s)+abs(s-t)
INF = 10**11
a, b, q = map(int, input().split())
S, T, X = [-INF]+I(a)+[INF], [-INF]+I(b)+[INF], I(q)
for x in X:
    s, t = f(S, x), f(T, x)
    print(min(g(x, s, f(T, s)), g(x, t, f(S, t))))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n = int(input())
#n,a = [int(i) for i in readline().split()]
a,b,q = [int(i) for i in readline().split()]
s = [-10**12]+[int(input()) for i in range(a)]+[10**12]
t = [-10**12]+[int(input()) for i in range(b)]+[10**12]

from bisect import *
for _ in range(q):
    x = int(readline())
    ans = 10**11

    i = bisect_left(s,x)
    j = bisect_left(t,x)
    d1 = max(s[i],t[j])-x
    d2 = x- min(s[i-1],t[j-1])
    d3 = x- max(s[i-1],t[j-1])
    d4 = min(s[i],t[j])-x
#    print(ans,d1,d2,d3,d4)
    ans = min(ans,d1,d2,d3*2+d4,d4*2+d3)
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n = int(input())
#n,a = [int(i) for i in readline().split()]
a,b,q = [int(i) for i in readline().split()]
s = [-10**12]+[int(input()) for i in range(a)]+[10**12]
t = [-10**12]+[int(input()) for i in range(b)]+[10**12]

from bisect import *
for _ in range(q):
    x = int(readline())
    ans = 10**11

    i = bisect_left(s,x)
    j = bisect_left(t,x)
    d1 = max(s[i],t[j])-x
    d2 = x- min(s[i-1],t[j-1])
    d3 = 2*(x-s[i-1]) + t[j]-x
    d4 = 2*(t[j]-x) + x-s[i-1]
#    print(ans,d1,d2,d3,d4)
    ans = min(ans,d1,d2,d3,d4)
    print(ans)