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
 
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)]
treeA = Avltree()
for i in s: treeA.insert(i)
print(1)

Submission status: Wrong Answer

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

##search(0,x):O(logN)
# xがある場合indexを、ない場合Noneを返す
def search(root,key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return None
        else:
            return search(avl[root][1],key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return None
        else:
            return search(avl[root][2],key)
    return root


##end_lower/higher:search_lower/higherで使用
def end_lower(root):
    if avl[root][1] == None:
        return avl[root][0]
    else:
        return end_lower(avl[root][1])

def end_higher(root):
    if avl[root][2] == None:
        return avl[root][0]
    else:
        return end_higher(avl[root][2])


##search_lower(0,x,None):O(logN)
# xより小さいものの中で最も大きいものを出力。ない場合はNoneを出力
def search_lower(root,key,lower_key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return lower_key
        else:
            return search_lower(avl[root][1],key,lower_key)
    if avl[root][0] < key:
        lower_key = avl[root][0]
        if avl[root][2] == None:
            return lower_key
        else:
            return search_lower(avl[root][2],key,lower_key)
    
    # == 
    if avl[root][1] == None:
        return lower_key
    else:
        if lower_key == None:
            return end_higher(avl[root][1])
        else:
            return max(lower_key,end_higher[root][1])

##search_higher(0,x,None):O(logN)
# xより大きいものの中で最も小さいものを出力。ない場合はNoneを出力
def search_higher(root,key,higher_key):
    if avl[root][0] > key:
        higher_key = avl[root][0]
        if avl[root][1] == None:
            return higher_key
        else:
            return search_higher(avl[root][1],key,higher_key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return higher_key
        else:
            return search_higher(avl[root][2],key,higher_key)
    
    # == 
    if avl[root][1] == None:
        return higher_key
    else:
        if higher_key == None:
            return end_lower(avl[root][1])
        else:
            return min(higher_key,end_lower[root][1])

##search_less(0,x,None):O(logN)
# x以下のものの中で最も大きいものを出力。ない場合はNoneを出力
def search_less(root,key,lower_key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return lower_key
        else:
            return search_less(avl[root][1],key,lower_key)
    if avl[root][0] < key:
        lower_key = avl[root][0]
        if avl[root][2] == None:
            return lower_key
        else:
            return search_less(avl[root][2],key,lower_key)
    
    # == 
    return key

##search_more(0,x,None):O(logN)
# x以上のものの中で最も小さいものを出力。ない場合はNoneを出力
def search_more(root,key,higher_key):
    if avl[root][0] > key:
        higher_key = avl[root][0]
        if avl[root][1] == None:
            return higher_key
        else:
            return search_more(avl[root][1],key,higher_key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return higher_key
        else:
            return search_more(avl[root][2],key,higher_key)
    
    # == 
    return key


##Rotation,replace,insertx:insertで使用
def DoubleRightRotation(x):
    tl = avl[x][1]
    avl[x][1] = avl[avl[tl][2]][2]
    avl[avl[tl][2]][2] = x
    tlr = avl[tl][2]
    avl[tl][2] = avl[tlr][1]
    avl[tlr][1] = tl
    if balance[tlr] == -1:
        balance[avl[tlr][2]] = 1
        balance[avl[tlr][1]] = 0
    elif balance[tlr] == 1:
        balance[avl[tlr][2]] = 0
        balance[avl[tlr][1]] = -1
    else:
        balance[avl[tlr][2]] = 0
        balance[avl[tlr][1]] = 0
    balance[tlr] = 0
    return tlr

def DoubleLeftRotation(x):
    tr = avl[x][2]
    avl[x][2] = avl[avl[tr][1]][1]
    avl[avl[tr][1]][1] = x
    trl = avl[tl][1]
    avl[tr][1] = avl[trl][2]
    avl[trl][2] = tr
    if balance[trl] == 1:
        balance[avl[trl][2]] = 0
        balance[avl[trl][1]] = -1
    elif balance[trl] == -1:
        balance[avl[trl][1]] = 0
        balance[avl[trl][2]] = 1
    else:
        balance[avl[trl][2]] = 0
        balance[avl[trl][1]] = 0
    balance[trl] = 0
    return trl

def SingleLeftRotation(x):
    tr = avl[x][2]
    balance[tr] = 0
    balance[x] = 0
    avl[x][2] = avl[tr][1]
    avl[tr][1] = x
    return tr

def SingleRightRotaion(x):
    tl = avl[x][1]
    balance[tl] = 0
    balance[x] = 0
    avl[x][1] = avl[tl][2]
    avl[tl][2] = x
    return tl

def replace(x,p,v):
    if avl[p][1] == x:
        avl[p][1] = v
    else:
        avl[p][2] = v

def insertx(root,p,key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            avl.append([key,None,None])
            avl[root][1] = len(avl)-1
        else:
            if not insertx(avl[root][1],root,key):
                return False
        if balance[root] == 1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = -1
            return True
        else:
            if balance[avl[root][1]] == 1:
                replace(root,p,DoubleRightRotation(root))
            elif balance[avl[root][1]] == -1:
                replace(root,p,SingleRightRotation(root))
            return False
    if avl[root][0] < key:
        if avl[root][2] == None:
            avl.append([key,None,None])
            avl[root][2] = len(avl)-1
        else:
            if not insertx(avl[root][2],root,key):
                return False
        if balance[root] == -1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = 1
            return True
        else:
            if balance[avl[root][2]] == -1:
                replace(root,p,DoubleLeftRotation(root))
            elif balance[avl[root][2]] == 1:
                replace(root,p,SingleLeftRotation(root))
            return False
    return False


##insert(0,x):O(logN)
#x追加
def insert(root,key):
    if key < avl[root][0]:
        if avl[root][1] == None:
            avl.append([key,None,None])
            avl[root][1] = len(avl)-1
        else:
            insertx(avl[root][1],root,key)
    elif key > avl[root][0]:
        if avl[root][2] == None:
            avl.append([key,None,None])
            avl[root][2] = len(avl)-1
        else:
            insertx(avl[root][2],root,key)
    else:
        pass

########################################################
##初期化（要素は一つ入れておく）
import sys
input = sys.stdin.readline
a,b,q = map(int,input().split())
s = [int(input()) for i in range(a)]
#avl[i]:[key,left_index,right_index]
#balance[i]: {"Even":0,"Left":-1,"Right":1}

#N_MAX = 1000000000
avl = [[s[0],None,None]]
balance = [0]*(a)
#要素数は最大N_MAX個
#first_avl = [x,None,None]
#avl.append(first_avl)

for i in range(1,a):
    insert(0,s[i])
print(1)

Submission status: Wrong Answer

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

##search(0,x):O(logN)
# xがある場合indexを、ない場合Noneを返す
def search(root,key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return None
        else:
            return search(avl[root][1],key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return None
        else:
            return search(avl[root][2],key)
    return root


##end_lower/higher:search_lower/higherで使用
def end_lower(root):
    if avl[root][1] == None:
        return avl[root][0]
    else:
        return end_lower(avl[root][1])

def end_higher(root):
    if avl[root][2] == None:
        return avl[root][0]
    else:
        return end_higher(avl[root][2])


##search_lower(0,x,None):O(logN)
# xより小さいものの中で最も大きいものを出力。ない場合はNoneを出力
def search_lower(root,key,lower_key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return lower_key
        else:
            return search_lower(avl[root][1],key,lower_key)
    if avl[root][0] < key:
        lower_key = avl[root][0]
        if avl[root][2] == None:
            return lower_key
        else:
            return search_lower(avl[root][2],key,lower_key)
    
    # == 
    if avl[root][1] == None:
        return lower_key
    else:
        if lower_key == None:
            return end_higher(avl[root][1])
        else:
            return max(lower_key,end_higher[root][1])

##search_higher(0,x,None):O(logN)
# xより大きいものの中で最も小さいものを出力。ない場合はNoneを出力
def search_higher(root,key,higher_key):
    if avl[root][0] > key:
        higher_key = avl[root][0]
        if avl[root][1] == None:
            return higher_key
        else:
            return search_higher(avl[root][1],key,higher_key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return higher_key
        else:
            return search_higher(avl[root][2],key,higher_key)
    
    # == 
    if avl[root][1] == None:
        return higher_key
    else:
        if higher_key == None:
            return end_lower(avl[root][1])
        else:
            return min(higher_key,end_lower[root][1])

##search_less(0,x,None):O(logN)
# x以下のものの中で最も大きいものを出力。ない場合はNoneを出力
def search_less(root,key,lower_key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            return lower_key
        else:
            return search_less(avl[root][1],key,lower_key)
    if avl[root][0] < key:
        lower_key = avl[root][0]
        if avl[root][2] == None:
            return lower_key
        else:
            return search_less(avl[root][2],key,lower_key)
    
    # == 
    return key

##search_more(0,x,None):O(logN)
# x以上のものの中で最も小さいものを出力。ない場合はNoneを出力
def search_more(root,key,higher_key):
    if avl[root][0] > key:
        higher_key = avl[root][0]
        if avl[root][1] == None:
            return higher_key
        else:
            return search_more(avl[root][1],key,higher_key)
    if avl[root][0] < key:
        if avl[root][2] == None:
            return higher_key
        else:
            return search_more(avl[root][2],key,higher_key)
    
    # == 
    return key


##Rotation,replace,insertx:insertで使用
def DoubleRightRotation(x):
    tl = avl[x][1]
    avl[x][1] = avl[avl[tl][2]][2]
    avl[avl[tl][2]][2] = x
    tlr = avl[tl][2]
    avl[tl][2] = avl[tlr][1]
    avl[tlr][1] = tl
    if balance[tlr] == -1:
        balance[avl[tlr][2]] = 1
        balance[avl[tlr][1]] = 0
    elif balance[tlr] == 1:
        balance[avl[tlr][2]] = 0
        balance[avl[tlr][1]] = -1
    else:
        balance[avl[tlr][2]] = 0
        balance[avl[tlr][1]] = 0
    balance[tlr] = 0
    return tlr

def DoubleLeftRotation(x):
    tr = avl[x][2]
    avl[x][2] = avl[avl[tr][1]][1]
    avl[avl[tr][1]][1] = x
    trl = avl[tl][1]
    avl[tr][1] = avl[trl][2]
    avl[trl][2] = tr
    if balance[trl] == 1:
        balance[avl[trl][2]] = 0
        balance[avl[trl][1]] = -1
    elif balance[trl] == -1:
        balance[avl[trl][1]] = 0
        balance[avl[trl][2]] = 1
    else:
        balance[avl[trl][2]] = 0
        balance[avl[trl][1]] = 0
    balance[trl] = 0
    return trl

def SingleLeftRotation(x):
    tr = avl[x][2]
    balance[tr] = 0
    balance[x] = 0
    avl[x][2] = avl[tr][1]
    avl[tr][1] = x
    return tr

def SingleRightRotaion(x):
    tl = avl[x][1]
    balance[tl] = 0
    balance[x] = 0
    avl[x][1] = avl[tl][2]
    avl[tl][2] = x
    return tl

def replace(x,p,v):
    if avl[p][1] == x:
        avl[p][1] = v
    else:
        avl[p][2] = v

def insertx(root,p,key):
    if avl[root][0] > key:
        if avl[root][1] == None:
            avl.append([key,None,None])
            avl[root][1] = len(avl)-1
        else:
            if not insertx(avl[root][1],root,key):
                return False
        if balance[root] == 1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = -1
            return True
        else:
            if balance[avl[root][1]] == 1:
                replace(root,p,DoubleRightRotation(root))
            elif balance[avl[root][1]] == -1:
                replace(root,p,SingleRightRotation(root))
            return False
    if avl[root][0] < key:
        if avl[root][2] == None:
            avl.append([key,None,None])
            avl[root][2] = len(avl)-1
        else:
            if not insertx(avl[root][2],root,key):
                return False
        if balance[root] == -1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = 1
            return True
        else:
            if balance[avl[root][2]] == -1:
                replace(root,p,DoubleLeftRotation(root))
            elif balance[avl[root][2]] == 1:
                replace(root,p,SingleLeftRotation(root))
            return False
    return False


##insert(0,x):O(logN)
#x追加
def insert(root,key):
    if key < avl[root][0]:
        if avl[root][1] == None:
            avl.append([key,None,None])
            avl[root][1] = len(avl)-1
        else:
            insertx(avl[root][1],root,key)
    elif key > avl[root][0]:
        if avl[root][2] == None:
            avl.append([key,None,None])
            avl[root][2] = len(avl)-1
        else:
            insertx(avl[root][2],root,key)
    else:
        pass

########################################################
##初期化（要素は一つ入れておく）
a,b,q = map(int,input().split())
s = [int(input()) for i in range(a)]
#avl[i]:[key,left_index,right_index]
#balance[i]: {"Even":0,"Left":-1,"Right":1}

#N_MAX = 1000000000
avl = [[s[0],None,None]]
balance = [0]*(a)
#要素数は最大N_MAX個
#first_avl = [x,None,None]
#avl.append(first_avl)

for i in range(1,a):
    insert(0,s[i])
print(1)

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s = [0] + [int(input()) for i in range(A)] + [10**11]
t = [0] + [int(input()) for i in range(B)] + [10**11]

def nibutans(x, l, r):
	if r - l <= 1:
		return r
	nibu = (s[r] + s[l]) // 2
	return nibutans(x, nibu, r) if x > nibu else nibutans(x, l, nibu)

def nibutant(x, l, r):
	if l == r and x >= l:
		return l + 1
	if r - l <= 1:
		return r
	nibu = (t[r] + t[l]) // 2
	return nibutant(x, nibu, r) if x > nibu else nibutant(x, l, nibu)

for i in range(Q):
	x = int(input())
	sr, tr = nibutans(x, 0, -1), nibutant(x, 0, -1)
	ans = 10**11
	for S in [s[sr - 1], s[sr]]:
		for T in [t[tr - 1], t[tr]]:
			d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
			ans = min(ans, d1, d2)
	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)
    print(b,d)
    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:
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)]
IMF=10**12

for i in range(Q):
    if s[-1]<x[i]:
        rs=IMF
        ls=x[i]-s[-1]
    elif s[0]>x[i]:
        rs=s[0]-x[i]
        ls=IMF
    else:
        for j in range(A):
            if s[j]==x[i]:
                rs=0
                ls=0
                break
            elif s[j]>x[i]:
                rs=s[j]-x[i]
                ls=x[i]-s[j-1]
                break

    if t[-1]<x[i]:
        rt=IMF
        lt=x[i]-t[-1]
    elif t[0]>x[i]:
        rt=t[0]-x[i]
        lt=IMF
    else:
        for j in range(B):
            if t[j]==x[i]:
                rt=0
                lt=0
                break
            elif t[j]>x[i]:
                rt=t[j]-x[i]
                lt=x[i]-t[j-1]
                break


    R=max(rs, rt)
    L=max(ls, lt)
    SRs=rs+lt+min(rs, lt)
    SRt=rt+ls+min(rt, ls)

Submission status: Wrong Answer

Solution code:
import bisect
A,B,C =map(int,input().split())
s =[-10**11] + [int(input())for _ in range(A)] +[10**11]
t =[-10**11] + [int(input())for _ in range(B)] +[10**11]

for _ in range(C):
    x =int(input())
    ns = bisect.bisect_left(s,x)
    nt = bisect.bisect_left(t,x)
    lef =sorted([x-s[ns-1],x-t[nt-1]])
    rig =sorted([s[ns]-x,t[nt]-x])
    ans = min(lef[1],rig[1],lef[0]+rig[0]*2,rig[0]+lef[0]*2)
    print(ans)

Submission status: Wrong Answer

Solution code:
def search(lis,x):
    def hantei(m):
        if lis[m] > x:
            return True
        else:
            return False

    num = len(lis)
    left = 0
    right =num -1

    while right -left > 1:
        med = (left+right)//2
        if hantei(med):
            right = med
        else:
            left = med

    return [lis[right]-x,x-lis[left]]

A,B,C =map(int,input().split())
s =[-10**11] + [int(input())for _ in range(A)] +[10**11]
t =[-10**11] + [int(input())for _ in range(B)] +[10**11]

for _ in range(C):
    x = int(input())
    sr,sl = search(s,x)
    tr,tl = search(t,x)
    lmax,lmin = max(sl,tl),min(sl,tl)
    rmax,rmin = max(sr,tr),min(sr,tr)
    ans = min(lmax,rmax,2*lmin+rmin,2*rmin+lmin)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
"""
_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
 
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)
    print(dis)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
"""
_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))

s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
 
_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
"""
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = data[-Q:]
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
"""
_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))

s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = sorted(data[-Q:])
 
for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

_input = sys.stdin.readlines()
A, B, Q = [int(i) for i in _input[0].split()]
data = [int(i) for i in _input[1:]]
"""
A, B, Q = [int(i.strip()) for i in input().split()]
data = list()
for l in range(A+B+Q):
    data.append(int(input()))
"""
s_ls = sorted(data[:A] + [1e30,-1e30])
t_ls = sorted(data[A:A+B] + [1e30,-1e30])
x_ls = sorted(data[-Q:])

for x in x_ls:
    s_pos = bisect.bisect_left(s_ls, x)
    s_left = s_ls[s_pos-1]
    s_right = s_ls[s_pos]
    t_pos = bisect.bisect_left(t_ls, x)
    t_left = t_ls[t_pos-1]
    t_right = t_ls[t_pos]
    a = abs(x - min(s_left, t_left))
    b = abs(x- max(s_right, t_right))
    c = abs(x - max(s_left, t_left))
    d = abs(x - min(s_right, t_right))
    e = min(c,d)
    dis = min(a,
              b,
              c+d+e)