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: Time Limit Exceeded

Solution code:
def halfsplitsearch(list,start,end,minmax):
    if minmax=='min':
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                end=test
            else:
                start=test
        if condition(list[start][0]):
            return start
        else:
            return end
    else:
        while end-start>1:
            test=(end+start)//2
            if condition(list[test][0]):
                start=test
            else:
                end=test
        if condition(list[end][0]):
            return end
        else:
            return start

import sys

input=sys.stdin.readline

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

list=s+t
list.sort()
s.sort()
t.sort()

for i in range(0,Q):
    a=0
    b=0
    test1=10**100
    test2=10**100
    def condition(num):
        return num<x[i]
    a=halfsplitsearch(list,0,len(list)-1,'max')
    if list[a][1]=='s':
        def condition(num):
            return num<list[a][0]
        b=halfsplitsearch(t,0,len(t)-1,'max')
        if b!=len(t)-1:
            test1=abs(x[i]-list[a][0])+min(abs(t[b][0]-list[a][0]),abs(t[b+1][0]-list[a][0]))
        else:
            test1=abs(x[i]-list[a][0])+abs(t[b][0]-list[a][0])
    else:
        def condition(num):
            return num<list[a][0]
        b=halfsplitsearch(s,0,len(s)-1,'max')
        if b!=len(s)-1:
            test1=abs(x[i]-list[a][0])+min(abs(s[b][0]-list[a][0]),abs(s[b+1][0]-list[a][0]))
        else:
            test1=abs(x[i]-list[a][0])+abs(s[b][0]-list[a][0])
    if a!=len(list)-1:
        if list[a+1][1]=='s':
            def condition(num):
                return num<list[a+1][0]
            b=halfsplitsearch(t,0,len(t)-1,'max')
            if b!=len(t)-1:
                test2=abs(x[i]-list[a+1][0])+min(abs(t[b][0]-list[a+1][0]),abs(t[b+1][0]-list[a+1][0]))
            else:
                test2=abs(x[i]-list[a+1][0])+abs(t[b][0]-list[a+1][0])
        else:
            def condition(num):
                return num<list[a+1][0]
            b=halfsplitsearch(s,0,len(s)-1,'max')
            if b!=len(s)-1:
                test2=abs(x[i]-list[a+1][0])+min(abs(s[b][0]-list[a+1][0]),abs(s[b+1][0]-list[a+1][0]))
            else:
                test2=abs(x[i]-list[a+1][0])+abs(s[b][0]-list[a+1][0])
    print(min(test1,test2))

Submission status: Time Limit Exceeded

Solution code:
def bs(x,li):
    left,right=0,len(li)
    while left+1<right:
        mid = (left+right)//2
        if li[mid]<=x:
            left=mid
        else:
            right=mid
    return left

A,B,Q=map(int,input().split())
INF=10**13
S=[-INF]+[int(input()) for _ in range(A)]+[INF]
T=[-INF]+[int(input()) for _ in range(B)]+[INF]
for _ in range(Q):
    x=int(input())
    sn,tn=bs(x,S),bs(x,T)
    ans=INF
    for s in [S[sn],S[sn+1]]:
        for t in [T[tn],T[tn+1]]:
            d=abs(s-x)+abs(t-s)
            e=abs(t-x)+abs(s-t)
            ans=min(ans,d,e)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 各クエリに対して,二分探索で解を求める

def bs(x,li):
    # 値xがliの2連続した値a,b達が作る[a,b)のどこに入るか
    # aを返す
    # liはソート済みとする
    left,right=0,len(li)
    while left+1<right:
        mid = (left+right)//2
        if li[mid]<=x:
            left=mid
        else:
            right=mid
    return left

def get(L):
    ret=[-10**13]
    for _ in range(L):
        ret.append(int(input()))
    ret.append(10**13)
    return ret

A,B,Q=map(int,input().split())
S=get(A)
T=get(B)
for _ in range(Q):
    x=int(input())

    # sn,sp,tn,tpを二分探索で出す
    sn,tn=bs(x,S),bs(x,T)

    # これらの経路の組み合わせの中に最小値が含まれている
    # 全部で8パターンあるので全探索してminをansとして保持
    ans=10**13
    for s in [S[sn],S[sn+1]]:
        for t in [T[tn],T[tn+1]]:
            d=abs(s-x)+abs(t-s)
            e=abs(t-x)+abs(s-t)
            ans=min([ans,d,e])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 各クエリに対して,二分探索で解を求める

def bs(x,li):
    # 値xがliの2連続した値a,b達が作る[a,b)のどこに入るか
    # aを返す
    # liはソート済みとする
    left,right=0,len(li)
    while left+1<right:
        mid = (left+right)//2
        if li[mid]<=x:
            left=mid
        else:
            right=mid
    return left

def get(L):
    ret=[-10**13]
    for _ in range(L):
        ret.append(int(input()))
    ret.append(10**13)
    return ret

A,B,Q=map(int,input().split())
S=get(A)
T=get(B)
for _ in range(Q):
    x=int(input())

    # sn,sp,tn,tpを二分探索で出す
    sn,tn=bs(x,S),bs(x,T)
    sp,tp=sn+1,tn+1

    # これらからさらに二分探索で最寄りのs,tを出して,minをansとして保持
    ans=10**13
    # print(sn,sp,tn,tp)
    for y in [S[sn],S[sp]]:
        l=bs(y,T)
        d=abs(y-x)+abs(T[l]-y)
        ans=min(ans,d)
        if l+1<len(T):
            e=abs(y-x)+abs(T[l+1]-y)
            ans=min(ans,e)
    for y in [T[tn],T[tp]]:
        l=bs(y,S)
        d=abs(y-x)+abs(S[l]-y)
        ans=min(ans,d)
        if l+1<len(S):
            e=abs(y-x)+abs(S[l+1]-y)
            ans=min(ans,e)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]
S.sort()
T.sort()
S = [-float('inf')] + S + [float('inf')]
T = [-float('inf')] + T + [float('inf')]

import bisect
for _ in range(q):
    x = int(input())
    i = bisect.bisect_right(S, x)
    j = bisect.bisect_right(T, x)
    ans = float('inf')
    for s in [S[i-1], S[i]]:
        for t in [T[j-1], T[j]]:
            d1 = abs(s-x) + abs(t-s)
            d2 = abs(t-x) + abs(s-t)
            ans = min([ans, d1, d2])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
from itertools import product
A,B,Q = map(int,input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
stlist = [s,t]
ab = [A,B]
for i in range(Q):
    x = int(input())
    cand = []
    for i in range(2):
        st = stlist[i]
        cand1 = set()
        l1 = bisect.bisect_left(st,x)
        r1 = bisect.bisect_right(st,x)
        if 0 <= l1 <= ab[i]-1:
            cand1.add(st[l1])
        if 0 <= l1-1 <= ab[i]-1:
            cand1.add(st[l1-1])  
        if 0 <= r1 <= ab[i]-1:
            cand1.add(st[r1])
        if 0 <= r1+1 <= ab[i]-1:
            cand1.add(st[r1+1])
        cand.append(cand1)
        
    ans = float('inf')
    for a,b in product(cand[0],cand[1]):
        c = min(abs(x-a)+abs(b-a),abs(x-b)+abs(b-a))
        ans = min(ans,c)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]+[10**15]
t = [int(input()) for _ in range(B)]+[10**15]

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

    # x -> s -> t
    dist1, dist2 = 0, 0
    p1 = bisect_left(s, x)
    x1, x2 = s[p1-1], s[p1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(t, x1)
    x3, x4 = t[p3-1], t[p3]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(t, x2)
    x3, x4 = t[p3-1], t[p3]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_st = min(dist1, dist2)

    # x -> t -> s

    dist1, dist2 = 0, 0
    p1 = bisect_left(t, x)
    x1, x2 = t[p1-1], t[p1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(s, x1)
    x3, x4 = s[p3-1], s[p3]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(s, x2)
    x3, x4 = s[p3-1], s[p3]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_ts = min(dist1, dist2)

    print(min(dist_st, dist_ts))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]

u = [-s[A-i-1] for i in range(A)]
v = [-t[B-i-1] for i in range(B)]

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

    # x -> s -> t
    dist1, dist2 = 0, 0
    p1 = bisect_left(s, x)
    p2 = bisect_left(u, -x)
    x1, x2 = s[p1-1], -u[p2-1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(t, x1)
    p4 = bisect_left(v, -x1)
    x3, x4 = t[p3-1], -v[p4-1]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(t, x2)
    p4 = bisect_left(v, -x2)
    x3, x4 = t[p3-1], -v[p4-1]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_st = min(dist1, dist2)

    # x -> t -> s

    dist1, dist2 = 0, 0
    p1 = bisect_left(t, x)
    p2 = bisect_left(v, -x)
    x1, x2 = t[p1-1], -v[p2-1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(s, x1)
    p4 = bisect_left(u, -x1)
    x3, x4 = s[p3-1], -u[p4-1]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(s, x2)
    p4 = bisect_left(u, -x2)
    x3, x4 = s[p3-1], -u[p4-1]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_ts = min(dist1, dist2)

    print(min(dist_st, dist_ts))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]

u = [-i for i in s[::-1]]
v = [-i for i in t[::-1]]

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

    # x -> s -> t
    dist1, dist2 = 0, 0
    p1 = bisect_left(s, x)
    p2 = bisect_left(u, -x)
    x1, x2 = s[p1-1], -u[p2-1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(t, x1)
    p4 = bisect_left(v, -x1)
    x3, x4 = t[p3-1], -v[p4-1]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(t, x2)
    p4 = bisect_left(v, -x2)
    x3, x4 = t[p3-1], -v[p4-1]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_st = min(dist1, dist2)

    # x -> t -> s

    dist1, dist2 = 0, 0
    p1 = bisect_left(t, x)
    p2 = bisect_left(v, -x)
    x1, x2 = t[p1-1], -v[p2-1]
    dist1 += abs(x-x1)
    dist2 += abs(x-x2)

    p3 = bisect_left(s, x1)
    p4 = bisect_left(u, -x1)
    x3, x4 = s[p3-1], -u[p4-1]
    dist1 += min(abs(x1-x3),abs(x1-x4))

    p3 = bisect_left(s, x2)
    p4 = bisect_left(u, -x2)
    x3, x4 = s[p3-1], -u[p4-1]
    dist2 += min(abs(x2-x3),abs(x2-x4))

    dist_ts = min(dist1, dist2)

    print(min(dist_st, dist_ts))

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

a,b,q = map(int,readline().split())

jinja = [-(2*10**10+10)]

for i in range(a):
    jinja.append(int(input()))

tera = [-(2*10**10+10)]

for i in range(b):
    tera.append(int(input()))

#add big number
jinja.append(2*10**10+10)
tera.append(2*10**10+10)

for query in range(q):
    x = int(input())
    j_r = bisect.bisect_left(jinja,x)
    j_l = jinja[j_r-1]
    t_r = bisect.bisect_left(tera,x)
    t_l = tera[t_r-1]
    j_r = jinja[j_r]
    t_r = tera[t_r]
    ans = min((x-j_l)*2+(t_r-x),(x-t_l)*2+(j_r-x),(x-j_l)+(t_r-x)*2,(x-t_l)+(j_r-x)*2)
    ans = min (ans,x - min(j_l,t_l), max(j_r,t_r)-x)
    print(ans)

Submission status: Time Limit Exceeded

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

import bisect
def solve(x):
    sr=bisect.bisect_right(S,x)
    tr=bisect.bisect_right(T,x)
    sl=sr-1
    tl=tr-1
    ret=10**12
    if sl>=0 and tl>=0:
        ret=min(ret,x-min(S[sl],T[tl]))
    if sr<A and tl>=0:
        ret=min(ret,S[sr]-T[tl]+min(S[sr]-x,x-T[tl]))
    if sl>=0 and tr<B:
        ret=min(ret,T[tr]-S[sl]+min(T[tr]-x,x-S[sl]))
    if sr<A and tr<B:
        ret=min(ret,max(S[sr],T[tr])-x)
    return ret

for x in X:
    print(solve(x))

Submission status: Time Limit Exceeded

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

import bisect
def solve(x):
    sl=bisect.bisect_left(S,x)
    sr=bisect.bisect_right(S,x)
    tl=bisect.bisect_left(T,x)
    tr=bisect.bisect_right(T,x)
    sl-=1
    tl-=1
    ret=10**12
    if sl>=0 and tl>=0:
        ret=min(ret,x-min(S[sl],T[tl]))
    if sr<A and tl>=0:
        ret=min(ret,S[sr]-T[tl]+min(S[sr]-x,x-T[tl]))
    if sl>=0 and tr<B:
        ret=min(ret,T[tr]-S[sl]+min(T[tr]-x,x-S[sl]))
    if sr<A and tr<B:
        ret=min(ret,max(S[sr],T[tr])-x)
    return ret

for x in X:
    print(solve(x))

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
value1=2*(10**10)
for zzz in range(Q):
    x=int(input())
    value=value1
    s1=bisect_left(S,x)
    for s in [s1-1,s1]:
        if -1< s < A:
            t1=bisect_left(T,S[s])
            for t in [t1-1,t1]:
                a=0
                if -1< t < B:
                    if x>S[s]:
                        a+=x-S[s]
                    else:
                        a+=S[s]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a
    t1=bisect_left(T,x)
    for t in [t1-1,t1]:
        if -1< t < B:
            s1=bisect_left(S,T[t])
            for s in [s1-1,s1]:
                a=0
                if -1< s < A:
                    if x>T[t]:
                        a+=x-T[t]
                    else:
                        a+=T[t]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
value1=2*(10**10)
for zzz in range(Q):
    x=int(input())
    value=value1
    s1=bisect_left(S,x)
    for s in range(s1-1,s1+1):
        if -1< s < A:
            t1=bisect_left(T,S[s])
            for t in range(t1-1,t1+1):
                a=0
                if -1< t < B:
                    if x>S[s]:
                        a+=x-S[s]
                    else:
                        a+=S[s]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a
    t1=bisect_left(T,x)
    for t in range(t1-1,t1+1):
        if -1< t < B:
            s1=bisect_left(S,T[t])
            for s in range(s1-1,s1+1):
                a=0
                if -1< s < A:
                    if x>T[t]:
                        a+=x-T[t]
                    else:
                        a+=T[t]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
value1=2*(10**10)
for zzz in range(Q):
    x=int(input())
    value=value1

    s1=bisect_left(S,x)
    for s in [s1-1,s1]:
        if 0<= s < A:
            t1=bisect_left(T,S[s])
            for t in [t1-1,t1]:
                a=0
                if 0<= t < B:
                    if x>S[s]:
                        a+=x-S[s]
                    else:
                        a+=S[s]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a
    t1=bisect_left(T,x)
    for t in [t1-1,t1]:
        if 0<= t < B:
            s1=bisect_left(S,T[t])
            for s in [s1-1,s1]:
                a=0
                if 0<= s < A:
                    if x>T[t]:
                        a+=x-T[t]
                    else:
                        a+=T[t]-x
                    if S[s]>T[t]:
                        a+=S[s]-T[t]
                    else:
                        a+=T[t]-S[s]
                    if value>a:
                        value=a

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
value1=2*(10**10)
for zzz in range(Q):
    x=int(input())
    value=value1
    s1=bisect_left(S,x)
    for s in [s1-1,s1]:
        if 0<= s < A:
            t1=bisect_left(T,S[s])
            for t in [t1-1,t1]:
                if 0<= t < B:
                    value=min(max(x,S[s])-min(x,S[s]) + max(S[s],T[t])-min(S[s],T[t]),value)
    t1=bisect_left(T,x)
    for t in [t1-1,t1]:
        if 0<= t < B:
            s1=bisect_left(S,T[t])
            for s in [s1-1,s1]:
                if 0<= s < A:
                    value=min(max(x,T[t])-min(x,T[t]) + max(S[s],T[t])-min(S[s],T[t]),value)

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
for zzz in range(Q):
    x=int(input())
    value=2*(10**10)
    s1=bisect_left(S,x)
    for s in [s1-1,s1]:
        if 0<= s < A:
            t1=bisect_left(T,S[s])
            for t in [t1-1,t1]:
                if 0<= t < B:
                    value=min(max(x,S[s])-min(x,S[s]) + max(S[s],T[t])-min(S[s],T[t]),value)
    t1=bisect_left(T,x)
    for t in [t1-1,t1]:
        if 0<= t < B:
            s1=bisect_left(S,T[t])
            for s in [s1-1,s1]:
                if 0<= s < A:
                    value=min(max(x,T[t])-min(x,T[t]) + max(S[s],T[t])-min(S[s],T[t]),value)

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from bisect import *
A,B,Q=map(int,input().split())
S=[int(input()) for i in range(A)]
T=[int(input()) for i in range(B)]
for zzz in range(Q):
    x=int(input())
    value=2*10**10
    s1=bisect_left(S,x)
    for s in [s1-1,s1]:
        if 0<= s < A:
            t1=bisect_left(T,S[s])
            for t in [t1-1,t1]:
                if 0<= t < B:
                    value=min(max(x,S[s])-min(x,S[s]) + max(S[s],T[t])-min(S[s],T[t]),value)
    t1=bisect_left(T,x)
    for t in [t1-1,t1]:
        if 0<= t < B:
            s1=bisect_left(S,T[t])
            for s in [s1-1,s1]:
                if 0<= s < A:
                    value=min(max(x,T[t])-min(x,T[t]) + max(S[s],T[t])-min(S[s],T[t]),value)

    print(value)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999999
R = 10**9 + 7
import bisect

def ddprint(x):
  if DBG:
    print(x)

a,b,q = inm()
s = []
t = []
for i in range(a):
    s.append(inn())
for i in range(b):
    t.append(inn())
for qq in range(q):
    x = inn()
    mn = BIG

    i = bisect.bisect_left(s,x)
    ss = [0] if i==0 else [a-1] if i==a else [i-1,i]
    for y in ss:
        d = abs(s[y]-x)
        j = bisect.bisect_left(t,s[y])
        tt = [0] if j==0 else [b-1] if j==b else [j-1,j]
        for z in tt:
            mn = min(mn, d+abs(s[y]-t[z]))

    i = bisect.bisect_left(t,x)
    tt = [0] if i==0 else [b-1] if i==b else [i-1,i]
    for y in tt:
        d = abs(t[y]-x)
        j = bisect.bisect_left(s,t[y])
        ss = [0] if j==0 else [a-1] if j==a else [j-1,j]
        for z in ss:
            mn = min(mn, d+abs(t[y]-s[z]))

    print(mn)

Submission status: Time Limit Exceeded

Solution code:
# 大方の予想通りにぶたんを都度やる+全探索
# （最初はクエリが飛んだときにO(1)なる方法があるのかと思ってたけどな）
# にぶたんでそこから右側と左側の直近にある寺と神社を同定して探索

a, b, q = map(int, input().split())
INF = 10 ** 18
s, t = (
    [-INF] + [int(input()) for _ in range(a)] + [INF],
    [-INF] + [int(input()) for _ in range(b)] + [INF],
)


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


for _ in range(q):
    target = int(input())
    # print("target = {}".format(target))
    y = lower_bound(target, s)
    if y == a + 1:
        y -= 1
    elif y == 1:
        y += 1
    x = y - 1
    w = lower_bound(target, t)
    if w == b + 1:
        w -= 1
    elif w == 1:
        w += 1
    z = w - 1
    # print("based s: x, y={}, {}".format(x, y))
    # print("based t: z, w={}, {}".format(w, z))
    m = min(
        abs(target - s[x]) + abs(s[x] - t[w]),
        abs(target - s[x]) + abs(s[x] - t[z]),
        abs(target - s[y]) + abs(s[y] - t[w]),
        abs(target - s[y]) + abs(s[y] - t[z]),
        abs(target - t[z]) + abs(t[z] - s[x]),
        abs(target - t[z]) + abs(t[z] - s[y]),
        abs(target - t[w]) + abs(t[w] - s[x]),
        abs(target - t[w]) + abs(t[w] - s[y]),
    )
    print(m)

Submission status: Time Limit Exceeded

Solution code:
# 大方の予想通りにぶたんを都度やる+全探索
# （最初はクエリが飛んだときにO(1)なる方法があるのかと思ってたけどな）
# にぶたんでそこから右側と左側の直近にある寺と神社を同定して探索

a, b, q = map(int, input().split())
s, t = [-(10 ** 18)], [-(10 ** 18)]
for _ in range(a):
    s.append(int(input()))
s.append(10 ** 18)
for _ in range(b):
    t.append(int(input()))
t.append(10 ** 18)


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


for _ in range(q):
    target = int(input())
    # print("target = {}".format(target))
    y = lower_bound(target, s)
    if y == a + 1:
        y -= 1
    elif y == 1:
        y += 1
    x = y - 1
    w = lower_bound(target, t)
    if w == b + 1:
        w -= 1
    elif w == 1:
        w += 1
    z = w - 1
    # print("based s: x, y={}, {}".format(x, y))
    # print("based t: z, w={}, {}".format(w, z))
    m = min(
        abs(target - s[x]) + abs(s[x] - t[w]),
        abs(target - s[x]) + abs(s[x] - t[z]),
        abs(target - s[y]) + abs(s[y] - t[w]),
        abs(target - s[y]) + abs(s[y] - t[z]),
        abs(target - t[z]) + abs(t[z] - s[x]),
        abs(target - t[z]) + abs(t[z] - s[y]),
        abs(target - t[w]) + abs(t[w] - s[x]),
        abs(target - t[w]) + abs(t[w] - s[y]),
    )
    print(m)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def f(li, x):
    n = len(li)
    ok = -1
    ng = n
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if li[mid] < x:
            ok = mid
        else:
            ng = mid
    return ok

def main():
    a,b,q = inpm()
    s = inplm(a)
    t = inplm(b)
    x = inplm(q)
    for i in range(q):
        start = x[i]
        index_s = f(s,start)
        index_t = f(t,start)
        a1 = 10**15
        b1 = 10**15
        c1 = 10**15
        d1 = 10**15
        if index_t+1 < b and index_s+1 < a:
            d1 = abs(max(s[index_s+1],t[index_t+1]) - start)
        if index_s >= 0 and index_t+1 < b:
            a1 = min(abs(start - s[index_s]) + abs(s[index_s] - t[index_t+1]), abs(start - t[index_t+1]) + abs(s[index_s] - t[index_t+1]) )  
        if index_t >= 0 and index_s+1 < a:
            b1 = min(abs(start - t[index_t]) + abs(s[index_s+1] - t[index_t]),abs(start - s[index_s+1]) + abs(s[index_s+1] - t[index_t]))
        if index_s >= 0 and index_t >= 0:
            c1 = abs(start - min(s[index_s],t[index_t]))
        print(min(a1,b1,c1,d1))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
#inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
#s_append = s.append
for i in range(a):
    si = int(input())
    s.append(si)
s.append(INF)
t = [-INF]
#t_append = t.append
for i in range(b):
    ti = int(input())
    t.append(ti)
t.append(INF)
x = []
#x_append = x.append
for i in range(q):
    xi = int(input())
    x.append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = xi - s[index_s - 1]
    s2 = s[index_s] - xi
    index_t = bisect_left(t, xi)
    t1 = xi - t[index_t - 1]
    t2 = t[index_t] -xi
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
#inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
s_append = s.append
for i in range(a):
    si = int(input())
    s_append(si)
s_append(INF)
t = [-INF]
#t_append = t.append
for i in range(b):
    ti = int(input())
    t.append(ti)
t.append(INF)
x = []
#x_append = x.append
for i in range(q):
    xi = int(input())
    x.append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = xi - s[index_s - 1]
    s2 = s[index_s] - xi
    index_t = bisect_left(t, xi)
    t1 = xi - t[index_t - 1]
    t2 = t[index_t] -xi
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
x = [int(input()) for _ in range(q)]

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = xi - s[index_s - 1]
    s2 = s[index_s] - xi
    index_t = bisect_left(t, xi)
    t1 = xi - t[index_t - 1]
    t2 = t[index_t] -xi
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
s_append = s.append
for i in range(a):
    si = int(input())
    s_append(si)
s_append(INF)
t = [-INF]
t_append = t.append
for i in range(b):
    ti = int(input())
    t_append(ti)
t_append(INF)
x = []
x_append = x.append
for i in range(q):
    xi = int(input())
    x_append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = xi - s[index_s - 1]
    s2 = s[index_s] - xi
    index_t = bisect_left(t, xi)
    t1 = xi - t[index_t - 1]
    t2 = t[index_t] -xi
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
s_append = s.append
for i in range(a):
    si = int(input())
    s_append(si)
s_append(INF)
t = [-INF]
t_append = t.append
for i in range(b):
    ti = int(input())
    t_append(ti)
t_append(INF)
x = []
x_append = x.append
for i in range(q):
    xi = int(input())
    x_append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = abs(xi - s[index_s - 1])
    s2 = abs(xi - s[index_s])
    index_t = bisect_left(t, xi)
    t1 = abs(xi - t[index_t - 1])
    t2 = abs(xi - t[index_t])
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
inpurt = sys.stdin.readline
a, b, q = map(int, input().split())
INF = 10 ** 11
s = [-INF]
for i in range(a):
    si = int(input())
    s.append(si)
s.append(INF)
t = [-INF]
for i in range(b):
    ti = int(input())
    t.append(ti)
t.append(INF)
x = []
for i in range(q):
    xi = int(input())
    x.append(xi)

for xi in x:
    index_s = bisect_left(s, xi)
    s1 = abs(xi - s[index_s - 1])
    s2 = abs(xi - s[index_s])
    index_t = bisect_left(t, xi)
    t1 = abs(xi - t[index_t - 1])
    t2 = abs(xi - t[index_t])
    print(min(max(s1, t1), max(s2, t2), min(s1, t2) * 2 + max(s1, t2), min(s2, t1) * 2 + max(s2, t1)))