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.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    //static List<string> str = new List<string>();
    static void Main(string[] args)
    {
        // 入力
        string[] input = Console.ReadLine().Split(' ');
        int A = int.Parse(input[0]);
        int B = int.Parse(input[1]);
        int Q = int.Parse(input[2]);

        List<Int64> shr = new List<Int64>();
        for (int i = 0; i < A; i++ )
        {
            shr.Add(Int64.Parse(Console.ReadLine()));
        }
        List<Int64> tmp = new List<Int64>();
        for (int i = 0; i < B; i++)
        {
            tmp.Add(Int64.Parse(Console.ReadLine()));
        }

        List<Int64> qst = new List<Int64>();
        for (int i = 0; i < Q; i++)
        {
            qst.Add(Int64.Parse(Console.ReadLine()));
        }
        //qst.Sort((a, b) => a.dist.CompareTo(b.dist));

        // 全てのクエスチョンに対して西端から順番に調べていく
        //Console.WriteLine("aaaaa");
        for (int i = 0; i < Q; i++ )
        {
            List<long> ret = new List<long>();
            // 神社→寺の順番
            Int64 sTot = 0, tTos = 0;
            Int64 x = qst[i];
            //Int64 nearestShr = shr.Nearest(x);
            //Int64 nearestTmp = tmp.Nearest(nearestShr);
            List<long> nearestShr = esearch(x, shr, 0, A);
            List<long> nearestTmp = esearch(nearestShr[0], tmp, 0, B);

            sTot = Math.Abs(x - nearestShr[0]) + Math.Abs(nearestShr[0] - nearestTmp[0]);
            ret.Add(sTot);

            if(nearestTmp.Count > 1)
            {
                sTot = Math.Abs(x - nearestShr[0]) + Math.Abs(nearestShr[0] - nearestTmp[1]);
                ret.Add(sTot);
            }

            if(nearestShr.Count > 1)
            {
                nearestTmp = esearch(nearestShr[1], tmp, 0, B);

                sTot = Math.Abs(x - nearestShr[1]) + Math.Abs(nearestShr[1] - nearestTmp[0]);
                ret.Add(sTot);

                if (nearestTmp.Count > 1)
                {
                    sTot = Math.Abs(x - nearestShr[1]) + Math.Abs(nearestShr[1] - nearestTmp[1]);
                    ret.Add(sTot);
                }
            }
            
            //Console.WriteLine("s->t:" + x + " " + nearestShr + " " + nearestTmp);


            // 寺→神社の順番
            //nearestTmp = tmp.Nearest(x);
            //nearestShr = shr.Nearest(nearestTmp);
            nearestTmp = esearch(x, tmp, 0, B);
            nearestShr = esearch(nearestTmp[0], shr, 0, A);

            //Console.WriteLine("t->s:" + x + " " + nearestShr + " " + nearestTmp);
            tTos = Math.Abs(x - nearestTmp[0]) + Math.Abs(nearestShr[0] - nearestTmp[0]);
            ret.Add(tTos);

            if (nearestShr.Count > 1)
            {
                tTos = Math.Abs(x - nearestTmp[0]) + Math.Abs(nearestShr[1] - nearestTmp[0]);
                ret.Add(tTos);
            }

            if (nearestTmp.Count > 1)
            {
                nearestShr = esearch(nearestTmp[1], shr, 0, A);

                tTos = Math.Abs(x - nearestTmp[1]) + Math.Abs(nearestShr[0] - nearestTmp[1]);
                ret.Add(tTos);

                if (nearestShr.Count > 1)
                {
                    tTos = Math.Abs(x - nearestTmp[1]) + Math.Abs(nearestShr[1] - nearestTmp[1]);
                    ret.Add(tTos);
                }
            }
            ret.Sort();
            Console.WriteLine(ret[0]);
        }
    }   
 
    static List<long> esearch(long target, List<Int64> arr, int i, int length)
    {
        // arrの範囲外の場合
        if (target <= arr[0]) return new List<long>(){arr[0]};
        else if (target >= arr[length - 1]) return new List<long>(){arr[length - 1]};

        // 範囲を特定 2^i ～ 2^(i+1)の範囲に存在する
        int first = i==0 ? 0 : (int)Math.Pow(2, i);
        int last = (int)Math.Pow(2, i + 1);
        if (last >= length) last = length - 1;

        //Console.WriteLine("esearch " + first + " " + last + ", " + target + " " + arr[first] + " " + arr[last]);
        if(target >= arr[first] && target <= arr[last])
        {
            // first～lastの間で見つける
            return lower_bound(first, last, arr, target);
        }
        else
        {
            return esearch(target, arr, i + 1, length);
        }
    }

    static List<long> lower_bound(int first, int last, List<Int64> arr, long target)
    {
        // 二分探索で一番近い値を探す
        //Console.WriteLine("nibuntansaku " + first + " " + last + ", " + target + " " + arr[first] + " " + arr[last]);
        if(last - first <= 1)
        {
            if(Math.Abs(target - arr[first]) < Math.Abs(target - arr[last]))
            {
                return new List<long>(){arr[first]};
            }
            else if (Math.Abs(target - arr[first]) > Math.Abs(target - arr[last]))
            {
                return new List<long>(){arr[last]};
            }
            else
            {
                return new List<long>() { arr[first], arr[last] };
            }
        }

        if(target > arr[(first + last)/2])
        {
            return lower_bound((first + last) / 2, last, arr, target);
        }
        else
        {
            return lower_bound(first, (first + last) / 2, arr, target);
        }
    }

}
public static class IEnumerableExtensions
{
    public static Int64 Nearest(this IEnumerable<Int64> self, Int64 target)
    {
        Int64 min = self.Min(c => Math.Abs(c - target));
        return self.First(c => Math.Abs(c - target) == min);
    }
}