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: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import math
import sys
import bisect

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


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


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

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


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

Submission status: Runtime Error

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 = min(bisect.bisect_right(s, x), A-1), min(bisect.bisect_right(t, x), B-1)
  a, c = max(b-1, 0), max(d-1, 0)
  
  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: Runtime Error

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: Runtime Error

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: Runtime Error

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: Runtime Error

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):
    near(x, s)
    near(x, t)
#     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: Runtime Error

Solution code:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[a]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[a-1]),abs(cc - bb[0])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1],2*bb[bbb] - cc - aa[aaa-1],bb[bbb] + cc - 2 * aa[aaa-1])
		print(aaaa)

Submission status: Runtime Error

Solution code:
import sys
class Bintree:
    def __init__(self,data=None): # デフォルト値を使うのはrootのみ
        self.data = data
        self.left = None
        self.right = None
    def search(self,data):
        if self.data == None:
            return None
        if data < self.data:
            if self.left == None:
                return None # 左の子がなければない
            else:
                return self.left.search(data) # 左の子があれば左の子(の下を)再帰的に検索
        elif data > self.data:
            if self.right == None:
                return None
            else:
                return self.right.search(data)
        else: # data == self.dataなら発見
            return self
    
    def insert(self,data):
        if self.data == None:
            self.data = data
            return self
        if data < self.data:
            if self.left == None:
                self.left = Bintree(data) # 左の子がなければ、そこに頂点を作って登録
                return True
            else:
                return self.left.insert(data)
        elif data > self.data:
            if self.right == None:
                self.right = Bintree(data)
                return True
            else:
                return self.right.insert(data)
        else: # data == self.data:
            return False
    def to_s(self):
        return self.data
    def left_s(self):
        if self.left==None:
            return None
        return (self.left).data
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).data
 
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 = Bintree(), Bintree()
for i in s: treeA.insert(i) # 木を作る
for i in t: treeB.insert(i)
print(1)

Submission status: Runtime Error

Solution code:
A,B,Q=map(int,input().split())
s=[]
t=[]
for i in range(A):
  s.append(int(input()))
for i in range(B):
  t.append(int(input()))
for i in range(Q):
  x=int(input())
  s1=s[:]
  s1.append(x)
  s1.sort()
  j=s1.index(x)
  if j==0:
    kouhoA=[s[j]]
  else:
    kouhoA=[s[j],s[j-1]]
  t1=t[:]
  t1.append(x)
  t1.sort()
  k=t1.index(x)
  if k==0:
    kouhoB=[t[k]]
  else:
    kouhoB=[t[k],t[k-1]]
  if j==0:
    
    if k==0:
      a=abs(kouhoA[0]-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,abs(x-kouhoB[0])+a])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      ans=min([u+a,abs(x-kouhoB[0])+a,u+b,abs(x-kouhoB[1])+b])
  else:
    if k==0:
      a=abs(kouhoA[0]-kouhoB[0])
      c=abs(kouhoB[0]-kouhoA[1])
      v=abs(x-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,v+a,abs(x-kouhoA[1])+c,v+c])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      c=abs(kouhoB[0]-kouhoA[1])
      d=abs(kouhoA[1]-kouhoB[1])
      v=abs(x-kouhoB[0])
      w=abs(x-kouhoA[1])
      y=abs(x-kouhoB[1])
      ans=min([u+a,v+a,w+d,y+d,w+c,v+c,u+b,y+b])
  print(ans)

Submission status: Runtime Error

Solution code:
A,B,Q=[int(x) for x in input().split()]
LA=[]#神社
LB=[]#寺

for i in range(A):
    LA.append(int(input()))
for i in range(B):
    LB.append(int(input()))

mx=200000000001

for i in range(Q):
    x=int(input())
    if LA[-1]<x:
        sa=mx
        sb=x-LA[-1]
    else:
        if x<LA[int(len(LA)/2)]:        
            for js,s in enumerate(LA):
                if x<s:
                    if js==0:
                        sb=mx
                    else:
                        sb=x-LA[js-1]
                    sa=s-x
                    break
        else:
            LA.reverse()
            for js,s in enumerate(LA):
                if s<x:
                    sa=LA[js-1]-x
                    sb=x-s
                    break
    if LB[-1]<x:
        ta=mx
        tb=x-LB[-1]
    else:
        if x<LB[int(len(LB)/2)]:
            for jt,t in enumerate(LB):
                if x<t:
                    if jt==0:
                        tb=mx
                    else:
                        tb=x-LB[jt-1]
                    ta=t-x
                    break
        else:
            LB.reverse()
            for jt,t in enumerate(LB):
                if t<x:
                    sa=LA[jt-1]-x
                    tb=x-t
                    break
    
    print(min(max(ta,sa),max(tb,sb),tb+sa+min(tb,sa),ta+sb+min(ta,sb)))

Submission status: Runtime Error

Solution code:
import bisect
import sys
class Bintree:
    def __init__(self,data=None): # デフォルト値を使うのはrootのみ
        self.data = data
        self.left = None
        self.right = None
    def search(self,data):
        if self.data == None:
            return None
        if data < self.data:
            if self.left == None:
                return None # 左の子がなければない
            else:
                return self.left.search(data) # 左の子があれば左の子(の下を)再帰的に検索
        elif data > self.data:
            if self.right == None:
                return None
            else:
                return self.right.search(data)
        else: # data == self.dataなら発見
            return self
    
    def insert(self,data):
        if self.data == None:
            self.data = data
            return self
        if data < self.data:
            if self.left == None:
                self.left = Bintree(data) # 左の子がなければ、そこに頂点を作って登録
                return True
            else:
                return self.left.insert(data)
        elif data > self.data:
            if self.right == None:
                self.right = Bintree(data)
                return True
            else:
                return self.right.insert(data)
        else: # data == self.data:
            return False
    def to_s(self):
        return self.data
    def left_s(self):
        if self.left==None:
            return None
        return (self.left).data
    def right_s(self):
        if self.right == None:
            return None
        return (self.right).data

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 = Bintree(), Bintree()
for i in s: treeA.insert(i) # 木を作る
for i in t: treeB.insert(i)

Submission status: Runtime Error

Solution code:
# -*- coding utf-8 -*-

A, B, Q = map(int, input().split())
si = [int(input()) for _ in range(A)]
ti = [int(input()) for _ in range(B)]
xi = [[int(input()), i] for i in range(Q)]

si.sort()
ti.sort()
xi.sort()

ai = [0] * A
j = 0
for i in range(A):
    while j != B and ti[j] < si[i]:
        j += 1 
    if j == B:
        j -= 1
    ai[i] = min(abs(si[i] - ti[j]), abs(si[i] - ti[j - 1]))

bi = [0] * B
j = 0
for i in range(B):
    while j != A and si[j] < ti[i]:
        j += 1
    if j == A:
        j -= 1
    bi[i] = min(abs(ti[i] - si[j]), abs(ti[i] - si[j - 1]))

xai = [0] * Q
xai_ind = [0] * Q
j = 0
for i in range(Q):
    while j != A and si[j] < xi[i][0]:
        j += 1
    if j == A:
        j -= 1
    # xai[i] = min(abs(xi[i] - si[j]), abs(xi[i] - si[j - 1]))
    if abs(xi[i][0] - si[j]) < abs(xi[i][0] - si[j - 1]):
        xai[i][0] = abs(xi[i] - si[j])
        xai_ind[i] = j
    else:
        xai[i] = abs(xi[i][0] - si[j - 1])
        xai_ind[i] = j - 1

xbi = [0] * Q
xbi_ind = [0] * Q
j = 0
for i in range(Q):
    while j != B and ti[j] < xi[i][0]:
        j += 1
    if j == B:
        j -= 1
    # xbi[i] = min(abs(xi[i] - ti[j]), abs(xi[i] - ti[j - 1]))
    if abs(xi[i][0] - ti[j]) < abs(xi[i][0] - ti[j - 1]):
        xbi[i] = abs(xi[i][0] - ti[j])
        xbi_ind[i] = j
    else:
        xbi[i] = abs(xi[i][0] - ti[j - 1])
        xbi_ind[i] = j - 1


ans = [[0] * 2 for _ in range(Q)]
for i in range(Q):
    ans[xi[i][1]][1] = min(xai[i] + ai[xai_ind[i]], xbi[i] + bi[xbi_ind[i]])
    ans[0][0] = i
for i in range(Q):
    print(ans[i][1])

Submission status: Runtime Error

Solution code:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x)   for (__typeof(x) i = 0; i < x; i++)
#define mp             make_pair
#define pb             push_back
#define fi             first
#define se             second
#define sz(x)          (int)((x).size())
#define all(x)         (x).begin(), (x).end()
#define sci(x)         int x;       scanf("%d", &x);
#define scii(x, y)     int x, y;    scanf("%d %d", &x, &y);
#define sciii(x, y, z) int x, y, z; scanf("%d %d %d", &x, &y, &z);
#define TC(x)          sci(x); while(x --)
#define eprintf(...)   fprintf(stderr, __VA_ARGS__)
#define debug(x)       { cerr << #x << " = " << x << endl; }
#define repi(i, x)  for (__typeof(x) i = x-1; i >= 0; i --)
#define fore(itr, x)   for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr ++)
#define forei(itr, x)  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr --)
typedef long long      ll;
typedef pair<int, int> ii;
typedef pair<ii, int>  iii;
typedef vector<int>    vi;
typedef vector<ii>     vii;
typedef vector<iii>    viii;
typedef vector<vector<int> > vvi;
const   int            infi = numeric_limits<int>::max();
const   double         eps = 0;
const   int            ms  = 0;



int lowerbound(vector<ll>& a, ll x, int N) {
    int low = -1;/////
    int high = N;//////array.size /// not N-1
    
    while (abs(high - low) > 1){
        int mid = (high + low)/ 2;
        if(a[mid] > x){///////////
            high = mid;
        }
        else{
            low = mid;
        }
    }
    
    return high;
    
}


int main()
{
    ios::sync_with_stdio(false);//endl->"\n"
    cin.tie(0);

    ll A, B, Q;
    cin >> A >> B >> Q;
    vector<ll> S_(A), T_(B), X(Q);
    rep(i, A)cin >> S_[i];
    rep(i, B)cin >> T_[i];
    rep(i, Q)cin >> X[i];
    //sort(all(S));
    //sort(all(T));

    vector<ll> S, T;
    S.pb(-(numeric_limits<ll>::max()/10));
    rep(i, A)S.pb(S_[i]);
    S.pb(numeric_limits<ll>::max()/10);

    T.pb(-(numeric_limits<ll>::max()/10));
    rep(i, B)T.pb(T_[i]);
    T.pb(numeric_limits<ll>::max()/10);

    rep(q, Q){
        ll s0, s1;
        ll x = X[q];
        int si = lowerbound(S, X[q], S.size());
        s0 = S[si-1];
        s1 = S[si];
        ll t0;
        ll t1;
        int ti = lowerbound(T, X[q], T.size());
        t0 = T[ti-1];
        t1 = T[ti];
        ll d = min(t1-s0+min(x-s0, t1-x), s1-t0 + min(x-t0, s1-x));
        debug(s0)debug(s1)debug(t0)debug(t1)debug(d)
        d = min(d, min(x-min(t0, s0), max(t1, s1)-x));
        cout << d << "\n";
    }

    return 0;
}

Submission status: Runtime Error

Solution code:
import sys

# スタート地点を与えると、最寄りの建物と、そこまでの移動距離を返す
# [(ind, dist)]
def search(start, lst):
    if start <= lst[0]:
        return [(0, lst[0] - start)]
    if lst[-1] <= start:
        return [(len(lst)-1, start - lst[-1])]

    # 間にある場合は二分探索
    return search_rec(start, lst, 0, len(lst) - 1)

def search_rec(start, lst, min_ind, max_ind):
    pivot_ind = (min_ind + max_ind) // 2
    if lst[pivot_ind] <= start and start <= lst[pivot_ind+1]:
        return [
            (pivot_ind, start - lst[pivot_ind]),
            (pivot_ind+1, lst[pivot_ind+1] - start),
        ]

    if start <= lst[pivot_ind]:
        return search_rec(start, lst, min_ind, pivot_ind)

    if lst[pivot_ind+1] <= start:
        return search_rec(start, lst, pivot_ind+1, max_ind)

    # ないと思う
    raise Exception('Not expected')


def solve(s_lst, t_lst, x):
    ans = []

    s_routes = search(x, s_lst)
    t_routes = search(x, t_lst)

    a = max([dist for ind, dist in s_routes if s_lst[ind] >= x])
    b = max([dist for ind, dist in t_routes if t_lst[ind] >= x])
    t1 = max(a,b)

    a = min([dist for ind, dist in s_routes if s_lst[ind] =< x])
    b = min([dist for ind, dist in t_routes if t_lst[ind] =< x])
    t2 = max(a, b)

    return min(t1, t2)

def main():
    line = sys.stdin.readline()
    line = line.rstrip()
    lst = line.split()

    a = int(lst[0])
    b = int(lst[1])
    q = int(lst[2])

    s_lst = []
    t_lst = []

    for i in range(a):
        line = sys.stdin.readline()
        s = int(line.rstrip())
        s_lst.append(s)

    for i in range(b):
        line = sys.stdin.readline()
        t = int(line.rstrip())
        t_lst.append(t)

    for i in range(q):
        line = sys.stdin.readline()
        x = int(line.rstrip())
        ans = solve(s_lst, t_lst, x)

        print(ans)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Runtime Error

Solution code:
num=list(map(int,input().split()))
s_list = [int(input()) for i in range(num[0])]
t_list = [int(input()) for i in range(num[1])]
x_list = [int(input()) for i in range(num[2])]

for x_ in x_list:
	min_s=min(list(map(lambda x: abs(x-x_), s_list)))
	min_t=min(list(map(lambda x: abs(x-x_), t_list)))
	
	min_index_t=list(map(lambda x: abs(x-x_), t_list)).index(min_t)
	
	minmin_t=min(list(map(lambda x: abs(x-t_list[min_index_t]), s_list)))+min_t




	min_index_s=list(map(lambda x: abs(x-x_), s_list)).index(min_s)
	minmin_s=min(list(map(lambda x: abs(x-s_list[min_index_s]), t_list)))+min_s
	print(min(minminmin_t,minmin_s))

Submission status: Runtime Error

Solution code:
import bisect

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


for i in range(Q):

    insert_index = bisect.bisect_left(s,x[i])
    if insert_index >= A:
        insert_index = insert_index -1
        tmp1 = s[insert_index]
        tmp2 = s[insert_index]
        
    elif insert_index == 0:
        tmp1 = s[insert_index]
        tmp2 = s[insert_index]
    else:   
        tmp1 = s[insert_index]
        tmp2 = s[insert_index-1]
    
    insert_index = bisect.bisect_left(t,x[i])
    if insert_index >= B or insert_index == 0:
        insert_index = insert_index -1
        tmp3 = t[insert_index]
        tmp4 = t[insert_index]
        
    elif:
        tmp3 = t[insert_index]
        tmp4 = t[insert_index]
        
    else:
        tmp3 = t[insert_index]
        tmp4 = t[insert_index-1]
    
    result = 10 ** 11
    for j, k in ([0,1], [0,0], [1,0], [1,1]):
        tmp = 0
        if j == 0:
            tmp += abs(tmp1 - x[i])
            now = tmp1
        else:
            tmp += abs(tmp2 - x[i])
            now = tmp2
                
        if k == 0:
            tmp += abs(now - tmp3)
        else:
            tmp += abs(now - tmp4)
                
        result = min(tmp, result)
                
    print(result)

Submission status: Runtime Error

Solution code:
import bisect

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


for i in range(Q):

    insert_index = bisect.bisect_left(s,x[i])
    if insert_index >= A:
        insert_index = insert_index -1
        tmp1 = s[insert_index]
        tmp2 = s[insert_index]
        
    elif insert_index == 0:
        tmp1 = s[insert_index]
        tmp2 = s[insert_index]
    else:   
        tmp1 = s[insert_index]
        tmp2 = s[insert_index-1]
    
    insert_index = bisect.bisect_left(t,x[i])
    if insert_index >= B or insert_index == 0:
        insert_index = insert_index -1
        tmp3 = t[insert_index]
        tmp4 = t[insert_index]
        
    elif:
        tmp1 = t[insert_index]
        tmp2 = t[insert_index]
        
    else:
        tmp3 = t[insert_index]
        tmp4 = t[insert_index-1]
    
    result = 10 ** 11
    for j, k in ([0,1], [0,0], [1,0], [1,1]):
        tmp = 0
        if j == 0:
            tmp += abs(tmp1 - x[i])
            now = tmp1
        else:
            tmp += abs(tmp2 - x[i])
            now = tmp2
                
        if k == 0:
            tmp += abs(now - tmp3)
        else:
            tmp += abs(now - tmp4)
                
        result = min(tmp, result)
                
    print(result)

Submission status: Runtime Error

Solution code:
import numpy as np

hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
# print(s)

a = np.asarray(s[:hint[0]])
b = np.asarray(s[hint[0]:-hint[2]])
sum = 0
for t in s[-hint[2]:]:
    a_dis = np.argmin(np.abs(a - t)[a > t-1000 and a < t+1000])
    b_dis = np.argmin(np.abs(b - t))
    print(np.min([np.abs(b[b_dis] - t) + np.min(np.abs(a[np.max([a_dis-1,0]):a_dis+1] - b[b_dis])), np.abs(a[a_dis] - t) + np.min(np.abs(b[np.max([b_dis-1,0]):b_dis+1] - a[a_dis]))]))

Submission status: Runtime Error

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

def get_dist_to_first_cp(x,li):
    dist1 = li -x
    abs_dist1 = abs(dist1)
    dist_to_first_cp = min(abs_dist1)
    first_cp_num = np.argmin(abs_dist1)
    first_cp_pos = x + dist1[first_cp_num]
    return dist_to_first_cp, first_cp_pos
    


u_list = s_list + t_list
for i, x in enumerate(x_list):
    # shrine first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,s_list)
    dist2 = abs(np.array(t_list) - first_cp_pos)
    dist_to_sec_cp = min(dist2)
    ans_s = dist_to_first_cp + dist_to_sec_cp
    # temple first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,t_list)
    dist2 = abs(np.array(s_list) - first_cp_pos)
    dist_to_sec_cp = min(dist2)
    
    ans_t = dist_to_first_cp + dist_to_sec_cp

    print(min(ans_s,ans_t))

Submission status: Runtime Error

Solution code:
A,B,Q=[int(x) for x in input().split()]
LA=[]#神社
LB=[]#寺

for i in range(A):
    LA.append(int(input()))
for i in range(B):
    LB.append(int(input()))

mx=200000000001

for i in range(Q):
    x=int(input())
    if LA[-1]<x:
        sa=mx
        sb=x-LA[-1]
    else:
        if x<LA[int(len(LA)/2)]:        
            for js,s in enumerate(LA):
                if x<s:
                    if js==0:
                        sb=mx
                    else:
                        sb=x-LA[js-1]
                    sa=s-x
                    break
        else:
            LA.reverse()
            for js,s in enumerate(LA):
                if s<x:
                    sa=x-LA[js-1]
                    sb=s-x
                    break
    if LB[-1]<x:
        ta=mx
        tb=x-LB[-1]
    else:
        if x<LB[int(len(LB)/2)]:
            for jt,t in enumerate(LB):
                if x<t:
                    if jt==0:
                        tb=mx
                    else:
                        tb=x-LB[jt-1]
                    ta=t-x
                    break
        else:
            LB.reverse()
            for jt,t in enumerate(LB):
                if t<x:
                    sa=x-LA[jt-1]
                    tb=t-x
                    break
    
    print(min(max(ta,sa),max(tb,sb),tb+sa+min(tb,sa),ta+sb+min(ta,sb)))

Submission status: Runtime Error

Solution code:
import bisect

a,b,q = map(int,input().split())
s = [int(input()) for x in range(a)]
t = [int(input()) for x in range(b)]
x = [int(input()) for x in range(q)]
for i in range(q):
    ss = False;tt = False
    n = x[i]
    s1 = bisect.bisect_left(s,n)
    t1 = bisect.bisect_left(t,n)
    if s1 >= len(s):b = s[s1-1];a = -1
    elif s1 <= 0:a = s[s1];b = -1
    else:a,b = s[s1],s[s1-1]
    if s1 >= len(s):j = t[t1-1];h = -1
    elif s1 <= 0:h = t[t1];j = -1
    else:h,j = t[t1],t[t1-1]
    if a >= 0:aa = bisect.bisect_left(t,a);ar,al = t[aa],t[aa-1]
    else:aa = 0;ar = al = 10**10
    if b >= 0:bb = bisect.bisect_left(t,b);br,bl = t[bb],t[bb-1]
    else:bb = 0;br = bl = 10**10
    if h >= 0:hh = bisect.bisect_left(s,h);hr,hl = s[hh],s[hh-1]
    else:hh = 0;hr = hl = 10**10
    if j >= 0:ii = bisect.bisect_left(s,h);jr,jl = s[ii],s[ii-1]
    else:ii = 0;jr = jl = 10**10

    aaa = [abs(n-a)+abs(a-ar),abs(n-a)+abs(a-al),abs(n-b)+abs(b-br),abs(n-b)+abs(b-bl),abs(n-h)+abs(h-hr),abs(n-h)+abs(h-hl),abs(n-j)+abs(j-jr),abs(n-j)+abs(j-jl)]
    aaaa = min(aaa)
    print(aaaa)

Submission status: Runtime Error

Solution code:
tmp = input()
tmp = tmo.split(" ")
A = tmp[0]
B = tmp[1]
Q = tmo[2]

s_lengthes = []
t_lengthes = []
Qs = []

for _ in range(A):
  s_lengthes.append(int(input()))
for _ in range(B):
  t_lengthes.append(int(input()))
for _ in range(B):
  Qs.append(int(input()))

for Q_position in Qs:
  A_distance = 0
  A_position = 0
  B_distance = 0
  B_position = 0
  ok_A_flag = 0
  ok_B_flag = 0
  
  for i in range(s_lengthes)-1:
    if s_lengthes[i] <= Q_position <=s_lengthes[i+1]:
    	A_distance = min(abs(s_lengthes[i]-Q_position),abs(s_lengthes[i+1]-Q_position))
        ok_A_flag = 1
        A_position = i
                         
  if ok_A_flag != 1:
    A_distance = Q_position - s_lengthes[-1]
    A_position = len(s_lengthes[-1])-1
    
  for i in range(t_lengthes)-1:
    if t_lengthes[i] <= Q_position <=t_lengthes[i+1]:
    	B_distance = min(abs(t_lengthes[i]-Q_position),abs(t_lengthes[i+1]-Q_position))
        ok_B_flag = 1
        B_position = i 
        
  if ok_B_flag != 1:
    B_distance = Q_position - t_lengthes[-1]
    A_position = len(s_lengthes[-1])-1
    
  distance = min(A_distance,B_distance) + s_lengthes(A_position) + t_dixtances(B_position)
  print(distances)

Submission status: Runtime Error

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

def get_dist_to_first_cp(x,li):
    dist1 = li -x
    abs_dist1 = list(map(abs,dist1))
    dist_to_first_cp = min(abs_dist1)
    first_cp_num = np.argmin(abs_dist1)
    first_cp_pos = x + dist1[first_cp_num]
    return dist_to_first_cp, first_cp_pos
    


u_list = s_list + t_list
for i, x in enumerate(x_list):
    # shrine first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,s_list)
    dist2 = abs(np.array(t_list) - first_cp_pos)
    dist_to_sec_cp = min(dist2)
    ans_s = dist_to_first_cp + dist_to_sec_cp
    # temple first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,t_list)
    dist2 = abs(np.array(s_list) - first_cp_pos)
    dist_to_sec_cp = min(dist2)
    
    ans_t = dist_to_first_cp + dist_to_sec_cp

    print(min(ans_s,ans_t))

Submission status: Runtime Error

Solution code:
import numpy as np
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)]

S = np.asarray(s)
T = np.asarray(t)
X = np.asarray(x)

st = np.zeros([a,b])
for i in range(a):
  for j in range(b):
    st[i][j] = abs(s[i]-t[j])

ans = np.zeros(q)
for k in range(q):
  #sを探す
  SX= np.abs(S-X[k])
  min_SX = min(SX)
  i = np.argmin(SX)
  #tを探す
  TX = np.abs(T-X[k])
  j = np.argmin(TX)
  min_TX = min(TX)
  #sが近い->そのsに近いtを探す
  ans[k] = min(min_SX + min(st[i]), min_TX + min(st.T[j]))
  print(int(ans[k]))