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

a,b,n=map(int,input().split())
l1=[int(input()) for i in range(a)]
l2=[int(input()) for i in range(b)]
x=[int(input()) for i in range(n)]
l1.sort()
l2.sort()
for i in range(n):
    idx1=bisect.bisect_left(l1,x[i])
    if idx1<a:
        p=abs(l1[idx1-1]-x[i])
        q=abs(l1[idx1]-x[i])
        if p>=q:
            po=l1[idx1]
            dis1=q
        else:
            po=l1[idx1-1]
            dis1=p
    else:
        dis1=x[i]-l1[-1]
        po=l1[-1]
    idx3=bisect.bisect_left(l2,po)
    if idx3<b:
        dis=min(abs(l2[idx3-1]-po),abs(l2[idx3]-po))
    else:
        dis=po-l2[-1]
    dis1+=dis
    idx2=bisect.bisect_left(l2,x[i])
    if idx2<b:
        p=abs(l2[idx2-1]-x[i])
        q=abs(l2[idx2]-x[i])
        if p>=q:
            po=l2[idx2]
            dis2=q
        else:
            po=l2[idx-1]
            dis2=p
    else:
        dis2=x[i]-l2[-1]
        po=l2[-1]
    idx3=bisect.bisect_left(l1,po)
    if idx3<a:
        dis=min(abs(l1[idx3-1]-po),abs(l1[idx3]-po))
    else:
        dis=po-l1[-1]
    dis2+=dis
    print(min(dis1,dis2))

Submission status: Runtime Error

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)]
for i in range(Q):
    s.append(x[i])
    s.sort()
    sindex=s.index(x[i])
    s.remove(x[i])
    t.append(x[i])
    t.sort()
    tindex=t.index(x[i])
    t.remove(x[i])
    if sindex==0:
        if tindex==0:
            sx=s[0]-x[i]
            tx=t[0]-x[i]
            print(min(sx,tx))
        elif tindex==B-1:
            sx=s[0]-x[i]
            tx=x[i]-t[B-1]
            print(min(2*sx+tx,sx+2*tx))
        else:
            sx=s[0]-x[i]
            tx1=x[i]-t[tindex]
            tx2=t[tindex+1]-x[i]
            print(min(max(tx2,sx),2*sx+tx1))
    elif sindex==A-1:
        if tindex==0:
            sx=x[i]-s[A-1]
            tx=t[0]-x[i]
            print(min(2*sx+tx,sx+2*tx))
        elif tindex==B-1:
            sx=x[i]-s[A-1]
            tx=x[i]-t[B-1]
            print(min(sx,tx))
        else:
            sx=x[i]-s[A-1]
            tx1=x[i]-t[tindex]
            tx2=t[tindex+1]-x[i]
            print(min(max(tx1,sx),2*sx+tx2))
    else:
        if tindex==0:
            tx=t[0]-x[i]
            sx1=x[i]-s[sindex]
            sx2=s[sindex+1]-x[i]
            print(min(max(tx,sx2),2*tx+sx1))
        elif tindex==B-1:
            tx=x[i]-t[B-1]
            sx1=x[i]-s[sindex]
            sx2=s[sindex+1]-x[i]
            print(min(max(tx,sx1),2*tx+sx2))
        else:
            sx1=x[i]-s[sindex]
            sx2=s[sindex+1]-x[i]
            tx1=x[i]-t[tindex]
            tx2=t[tindex+1]-x[i]

Submission status: Runtime Error

Solution code:
import bisect
a,b,q = map(int,input().split())
s = [0]*a
t = [0]*b
for i in range(a):
    s[i] = int(input())
for i in range(b):
    t[i] = int(input())
for i in range(q):
    x = int(input())
    if x in s:
        d = bisect.bisect_left(t,x)
        print(min(t[d-1],t[d]))
    c = bisect.bisect_left(s,x)
    d = bisect.bisect_left(t,s[c])
    e = bisect.bisect_left(t,s[c-1])
    m = min(abs(s[c]-x)+abs(t[d]-s[c]),abs(s[c]-x)+abs(t[d-1]-s[c]),\
            abs(s[c-1]-x)+abs(t[e]-s[c-1]),abs(s[c-1]-x)+abs(t[e]-s[c]))
    print(m)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    long long oo = 0xffffffffffff;

    int A, B, Q;
    cin>>A>>B>>Q;
    vector<long long> s(A+4);
    s[0] = -oo;
    s[1] = -oo+1;
    for (int i=0; i<A; i++)
        cin>>s[i+2];
    s[A+2] = oo-1;
    s[A+3] = oo;
    vector<long long> t(B+4);
    t[0] = -oo;
    t[1] = -oo+1;
    for (int i=0; i<B; i++)
        cin>>t[i+2];
    t[B+2] = oo-1;
    t[B+3] = oo;

    auto index = [&](vector<long long> &v, long long x)
    {
        return (int)(lower_bound(v.begin(), v.end(), x) - v.begin());
    };

    for (int i=0; i<Q; i++)
    {
        long long x;
        cin>>x;

        long long ans = oo;
        for (int b=0; b<8; b++)
        {
            int st = b&1;
            int d1 = b>>1&1;
            int d2 = b>>2&1;

            vector<long long> v1 = st==0 ? s : t;
            vector<long long> v2 = st==0 ? t : s;

            long long x2 = v1[index(v1, x)-d1];
            long long x3 = v2[index(v2, x)-d2];
            ans = min(ans, abs(x-x2)+abs(x2-x3));
        }
        cout<<ans<<endl;
    }
}

Submission status: Runtime Error

Solution code:
import bisect
a,b,q = map(int,input().split())
x = [int(input()) for i in range(a)]
y = [int(input()) for i in range(b)]
x.sort()
y.sort()
for i in range(q):
    z = int(input())
    ans = 1<<61
    n = bisect.bisect_left(x, z)
    if n<a:
        tmp = abs(x[n]-z)
        m = bisect.bisect_left(y, x[n])
        try:
            ans = min(ans,abs(y[m]-x[n])+tmp)
        except:
            pass
        try:
            ans = min(ans,abs(y[m-1]-x[n])+tmp)
        except:
            pass
    if n>0:
        tmp = abs(x[n-1]-z)
        m = bisect.bisect_left(y, x[n-1])
        try:
            ans = min(ans,abs(y[m]-x[n-1])+tmp)
        except:
            pass
        try:
            ans = min(ans,abs(y[m-1]-x[n-1])+tmp)
        except:
            pass

    n = bisect.bisect_left(y, z)
    if n<a:
        tmp = abs(y[n]-z)
        m = bisect.bisect_left(x, y[n])
        try:
            ans = min(ans,abs(y[n]-x[m])+tmp)
        except:
            pass
        try:
            ans = min(ans,abs(x[m-1]-y[n])+tmp)
        except:
            pass
    if n>0:
        tmp = abs(y[n-1]-z)
        m = bisect.bisect_left(x, y[n-1])
        try:
            ans = min(ans,abs(x[m]-y[n-1])+tmp)
        except:
            pass
        try:
            ans = min(ans,abs(x[m-1]-y[n-1])+tmp)
        except:
            pass
    print(ans)