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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class Main {

    public static final Long  INF              = Long.MAX_VALUE;
    public static List<Point> distanceFromWest = new ArrayList<>();
    public static int         A;
    public static int         B;
    public static int         Q;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        A = Integer.parseInt(sc.next());
        B = Integer.parseInt(sc.next());
        Q = Integer.parseInt(sc.next());

        long[] s = new long[A];

        for (int i = 0; i < A; i++) {

            s[i] = Long.parseLong(sc.next());
            String name = "s" + i;
            Point p = new Point("s", name, s[i]);

            distanceFromWest.add(p);
        }

        long[] t = new long[B];

        for (int i = 0; i < B; i++) {

            t[i] = Long.parseLong(sc.next());
            String name = "t" + i;
            Point p = new Point("t", name, t[i]);

            distanceFromWest.add(p);
        }

        long[] x = new long[Q];

        for (int i = 0; i < Q; i++) {

            x[i] = Long.parseLong(sc.next());
            String name = "x" + i;
            Point p = new Point("x", name, x[i]);

            distanceFromWest.add(p);
        }

        sc.close();

        Collections.sort(distanceFromWest, new PointComparator());

        //        for(int i=0;i<distanceFromWest.size();i++){
        //            System.out.format("%s - %d\n",
        //                distanceFromWest.get(i).getName(),
        //                distanceFromWest.get(i).getDistance());
        //        }

        for (int i = 0; i < Q; i++) {

            String point = "x" + i;

            //xiから見て東に進んで神社->寺とたどり着くパターン
            long est = est(point);

            //xiから見て東に進んで寺->神社とたどり着くパターン
            long ets = ets(point);

            //xiから見て東に進んだ神社にたどり着き、xiから見て西に進んだ寺にたどり着くパターン
            long eswt = eswt(point);

            //xiから見て東に進んだ寺にたどり着き、xiから見て西に進んだ神社にたどり着くパターン
            long etws = etws(point);

            //xiから見て西に進んで神社->寺とたどり着くパターン
            long wst = wst(point);

            //xiから見て西に進んで寺->神社とたどり着くパターン
            long wts = wts(point);

            //xiから見て西に進んだ神社にたどり着き、xiから見て東に進んだ寺にたどり着くパターン
            long wset = wset(point);

            //xiから見て西に進んだ寺にたどり着き、xiから見て東に進んだ神社にたどり着くパターン
            long wtes = wtes(point);

            //            System.out.println(est + " - est");
            //            System.out.println(ets + " - ets");
            //            System.out.println(eswt + " - eswt");
            //            System.out.println(etws + " - etws");
            //            System.out.println(wst + " - wst");
            //            System.out.println(wts + " - wts");
            //            System.out.println(wset + " - wset");
            //            System.out.println(wtes + " - wtes");

            System.out.println(Math.min(est,
                    Math.min(ets, Math.min(eswt, Math.min(etws, Math.min(wst, Math.min(wts, Math.min(wset, wtes))))))));
        }

    }

    private static long wtes(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("t")) {

                itr.next();
                tDistance = itr.previous().getDistance();
                break;
            }
        }

        //xiまで戻る
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                break;
            }
        }

        //次の神社まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("s")) {

                itr.previous();
                sDistance = itr.next().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = 2 * (xDistance - tDistance) + (sDistance - xDistance);

        return distance;
    }

    private static long wset(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("s")) {

                itr.next();
                sDistance = itr.previous().getDistance();
                break;
            }
        }

        //xiまで戻る
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                break;
            }
        }

        //次の寺まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("t")) {

                itr.previous();
                tDistance = itr.next().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = 2 * (xDistance - sDistance) + (tDistance - xDistance);

        return distance;
    }

    private static long est(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("s")) {

                itr.previous();
                sDistance = itr.next().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("t")) {

                itr.previous();
                tDistance = itr.next().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = tDistance - xDistance;

        return distance;
    }

    private static long ets(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("t")) {

                itr.previous();
                tDistance = itr.next().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("s")) {

                itr.previous();
                sDistance = itr.next().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = sDistance - xDistance;

        return distance;
    }

    private static long eswt(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("s")) {

                itr.previous();
                sDistance = itr.next().getDistance();
                break;
            }
        }

        //xiまで戻る
        while (itr.hasPrevious()) {

            if (itr.previous().getName().equals(point)) {

                break;
            }
        }

        //次の寺まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("t")) {

                itr.next();
                tDistance = itr.next().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = 2 * (sDistance - xDistance) + (xDistance - tDistance);

        return distance;
    }

    private static long etws(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasNext()) {

            if (itr.next().getType().equals("t")) {

                itr.previous();
                tDistance = itr.next().getDistance();
                break;
            }
        }

        //xiまで戻る
        while (itr.hasPrevious()) {

            if (itr.previous().getName().equals(point)) {

                break;
            }
        }

        //次の神社まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("s")) {

                itr.next();
                sDistance = itr.previous().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = 2 * (tDistance - xDistance) + (xDistance - sDistance);

        return distance;
    }

    private static long wst(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("s")) {

                itr.next();
                sDistance = itr.previous().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("t")) {

                itr.next();
                tDistance = itr.previous().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = xDistance - tDistance;

        return distance;
    }

    private static long wts(String point) {

        ListIterator<Point> itr = distanceFromWest.listIterator();
        long distance = INF;
        long xDistance = 0;
        long sDistance = 0;
        long tDistance = 0;

        //xiまで移動
        while (itr.hasNext()) {

            if (itr.next().getName().equals(point)) {

                itr.previous();
                xDistance = itr.next().getDistance();
                break;
            }
        }

        //次の寺まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("t")) {

                itr.next();
                tDistance = itr.previous().getDistance();
                break;
            }
        }

        //次の神社まで
        while (itr.hasPrevious()) {

            if (itr.previous().getType().equals("s")) {

                itr.next();
                sDistance = itr.previous().getDistance();
                break;
            }
        }

        if (xDistance == 0 || sDistance == 0 || tDistance == 0) {

            return INF;
        }

        distance = xDistance - sDistance;

        return distance;
    }
}

class Point {

    private String type;
    private String name;
    private long   distance;

    public Point(String type, String name, long distance) {
        super();
        this.type = type;
        this.name = name;
        this.distance = distance;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getDistance() {
        return distance;
    }

    public void setDistance(long distance) {
        this.distance = distance;
    }

}

class PointComparator implements Comparator<Point> {

    @Override
    public int compare(Point p1, Point p2) {
        return p1.getDistance() < p2.getDistance() ? -1 : 1;
    }
}