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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

macro_rules! stdin {
    () => {{
        use std::io::Read;
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }};
}

macro_rules! test {
    ($($input:expr => $expected_output:expr),*) => {
        #[test]
        fn solve_test() {
            let mut i = 0;
            println!("");
            $(
                i += 1;
                println!("Case {}:", i);
                println!("[in]\n{}", $input);
                println!("[out]\n{}", solve($input));
                println!("[expected out]\n{}", $expected_output);
                println!("");
                assert_eq!(solve($input), $expected_output);
             )*
        }
    }
}

test! {
r"2 3 4
100
600
400
900
1000
150
2000
899
799
" => r"350
1400
301
399",

r"1 1 3
1
10000000000
2
9999999999
5000000000
" => r"10000000000
10000000000
14999999998"
}

use std::cmp::max;
use std::cmp::min;

fn main() {
    println!("{}", solve(&stdin!()));
}

fn solve(src: &str) -> String {
    input! {
        source = src,
        A: i32, B: i32, Q: i32,
        s: [i64; A],
        t: [i64; B],
        x: [i64; Q]
    }
    
    let mut ans: String = String::new();
    for ix in x {
        if ! ans.is_empty() {
            ans += "\n";
        }
        let min_d: i64 = min(calc_min_d(&s, &t, ix), calc_min_d(&t, &s, ix));
        ans += &min_d.to_string();
    }

    return ans;
}

fn calc_min_d(l1: &Vec<i64>, l2: &Vec<i64>, p0: i64) -> i64 {
    let mut min_d: i64 = std::i64::MAX;
    let lrp1: Vec<i64> = nearest_lr(l1, p0);
    for p1 in lrp1 {
        let lrp2: Vec<i64> = nearest_lr(l2, p1);
        for p2 in lrp2 {
            let d: i64 = (p1-p0).abs() + (p2-p1).abs();
            min_d = min(min_d, d);
        }
    }
    return min_d;
}

fn nearest_lr(v: &Vec<i64>, p: i64) -> Vec<i64> {
    let np: i64 = b_search(v, p, 0, v.len() as i64);

    let mut lr: Vec<i64> = Vec::new();
    if p == v[np as usize] {
        lr.push(v[np as usize]);
    } else if p < v[np as usize] {
        if np-1 >= 0 {
            lr.push(v[(np-1) as usize]);
        }
        lr.push(v[np as usize]);
    } else {
        lr.push(v[np as usize]);
        if ((np+1) as usize) < v.len() {
            lr.push(v[(np+1) as usize]);
        }
    }
    return lr;
}

#[test]
fn nearest_lr_test() {
    let v: Vec<i64> = vec![2, 5];
    let e1: Vec<i64> = vec![2];
    let e2: Vec<i64> = vec![5];
    let e3: Vec<i64> = vec![2, 5];
    assert_eq!(nearest_lr(&v, 1), e1);
    assert_eq!(nearest_lr(&v, 6), e2);
    assert_eq!(nearest_lr(&v, 3), e3);
}

fn b_search(v: &Vec<i64>, p: i64, b: i64, e: i64) -> i64 {
    if e-b == 1 {
        return b;
    }
    let rb: i64 = b + (e-b)/2;
    if p < v[rb as usize] {
        return b_search(v, p, b, rb);
    } else {
        return b_search(v, p, rb, e);
    }
}