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: Wrong Answer)

use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::cmp;
use std::f64::consts;
use std::cmp::Ordering;
use std::collections::VecDeque; //push_back, pop_front

const MOD: i64 = 1000000007;

fn main() {
    let a: i64 = read();
    let b: i64 = read();
    let q: i64 = read();

    let vec_a: Vec<i64> = (0..a).map(|_| read()).collect();
    let vec_b: Vec<i64> = (0..b).map(|_| read()).collect();
    let vec_q: Vec<i64> = (0..q).map(|_| read()).collect();

//    println!("{:?}", vec_a);
//    println!("{:?}", vec_b);
//    println!("{:?}", vec_q);


    for &i in vec_q.iter() {
        let mut min = <i64>::max_value();

        let nibun_a = upper_bound(&vec_a, i);
        let nibun_b = upper_bound(&vec_b, i);

        let mut a_e = if nibun_a == 0 {nibun_a} else {nibun_a-1};
        let mut b_e = if nibun_b == 0 {nibun_b} else {nibun_b-1};

//        println!("{}", a_e);
//        println!("{}", b_e);

        let diff_b = (vec_b[b_e] - vec_a[a_e]).abs();
        let diff_b_2 = (i - vec_b[b_e]).abs();
        let diff_a = (i - vec_a[a_e]).abs();
        let diff_a_2 = (vec_a[a_e] - vec_b[b_e]).abs();

        let each_min = cmp::min(diff_a+diff_b, diff_a_2+diff_b_2);

//        println!("1: {}", diff_a+diff_b);
//        println!("2: {}", diff_a_2+diff_b_2);
        min = cmp::min(min, each_min);

        if a_e != vec_a.len()-1 {
            a_e+=1;
        }

        if b_e != vec_b.len()-1{
            b_e+=1;
        }

//        println!("{}", a_e);
//        println!("{}", b_e);
        let diff_b = (vec_b[b_e] - vec_a[a_e]).abs();
        let diff_b_2 = (i - vec_b[b_e]).abs();
        let diff_a = (i - vec_a[a_e]).abs();
        let diff_a_2 = (vec_a[a_e] - vec_b[b_e]).abs();

        let each_min = cmp::min(diff_a+diff_b, diff_a_2+diff_b_2);

//        println!("1: {}", diff_a+diff_b);
//        println!("2: {}", diff_a_2+diff_b_2);
        min = cmp::min(min, each_min);

//        println!("{}", a_e);
//        println!("{}", b_e);

//
//        'loop_a: for (a_i, &a_e) in vec_a.iter().enumerate() {
//            let diff_a = (i - a_e).abs();
//            if diff_a > min {
//                break;
//            }
//
//            for (b_i, &b_e) in vec_b.iter().enumerate() {
//                let diff_b = (b_e - a_e).abs();
//                let diff_b_2 = (i - b_e).abs();
//                if diff_b_2 > min {
//                    break 'loop_a;
//                }
//                let diff_a_2 = (a_e - b_e).abs();
//
//                let each_min = cmp::min(diff_a+diff_b, diff_a_2+diff_b_2);
//                min = cmp::min(min, each_min);
//            }
//        }

        println!("{}", min);
    }

//    let n: i32 = read();
//    let a: i32 = read();
//    let b: i32 = read();
//    let c: i32 = read();
//
//    let mut vec: Vec<i32> = (0..n).map(|_| read()).collect();
//
//    vec.sort();
//    vec.reverse();
//
//    println!("{:?}", vec);
}

// 素因数分解
fn prime_factorization(number: u64) -> Vec<u64> {
    let mut result = number;
    for i in 2..((number as f64).sqrt() as u64)+1 {
        if number % i == 0 {
            result = i;
            break;
        }
    }

    if result == number {
        return vec![number];
    } else {
        let mut v1 = vec![result];
        let mut v2 = prime_factorization(number / result);

        v1.append(&mut v2);

        return v1;
    }
}

fn calc_distance(xa: f64, ya: f64, xb: f64, yb: f64)->f64 {
    (f64::powf(xb-xa, 2.0) + f64::powf(yb-ya, 2.0)).sqrt()
}

fn calc_triangle_area(x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) -> f64 {
    1.0/2.0 * ((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3)).abs()
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

// 最大公約数
fn gcd(a: i64, b: i64) -> i64 {
    match b {
        0 => a,
        _ => gcd(b, a % b)
    }
}

// 最小公倍数
fn lcm(a: i64, b: i64) -> i64 {
    let g = gcd(a ,b);
    a / g * b
}

// 階乗-dp (overflow回避で1000000007の余りを使って計算)
fn kaijou_dp(n: i64) -> i64 {
    let mut dp: Vec<i64> = vec![0; n as usize+1];
    dp[0] = 1;
    dp[1] = 1;

    for i in 1..n {
        dp[i as usize+1] = dp[i as usize] % 1000000007 * (i+1)  % 1000000007;
    }
    return dp[n as usize];
}

// 階乗
fn kaijou(n: i64)->i64 {
    if n == 1 {
        return n;
    }
    return n * kaijou(n-1);
}

// 順列全列挙
pub trait LexicalPermutation {
    /// Return `true` if the slice was permuted, `false` if it is already
    /// at the last ordered permutation.
    fn next_permutation(&mut self) -> bool;
}

impl<T> LexicalPermutation for [T] where T: Ord {
    /// Original author in Rust: Thomas Backman <serenity@exscape.org>
    /// let mut data = [1, 2, 3];
    //    let mut permutations = Vec::new();
    //
    //    loop {
    //        permutations.push(data.to_vec());
    //        if !data.next_permutation() {
    //            break;
    //        }
    //    }
    //
    //    println!("{:?}", permutations);
    //    // [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
    fn next_permutation(&mut self) -> bool {
        // These cases only have 1 permutation each, so we can't do anything.
        if self.len() < 2 { return false; }

        // Step 1: Identify the longest, rightmost weakly decreasing part of the vector
        let mut i = self.len() - 1;
        while i > 0 && self[i-1] >= self[i] {
            i -= 1;
        }

        // If that is the entire vector, this is the last-ordered permutation.
        if i == 0 {
            return false;
        }

        // Step 2: Find the rightmost element larger than the pivot (i-1)
        let mut j = self.len() - 1;
        while j >= i && self[j] <= self[i-1]  {
            j -= 1;
        }

        // Step 3: Swap that element with the pivot
        self.swap(j, i-1);

        // Step 4: Reverse the (previously) weakly decreasing part
        self[i..].reverse();

        true
    }
}

//iより小さい数字の数 = lower_bound(&a, i) as i64;
//iより大きい数字の数 = n - upper_bound(&c, i) as i64;
fn lower_bound<T: Ord>(vec: &Vec<T>, x: T) -> usize {
    let (mut left, mut right): (i64, i64) = (-1, vec.len() as i64);

    while (right - left) > 1 {
        let mid = (right + left) / 2;

        if x <= vec[mid as usize] {
            right = mid;
        } else {
            left = mid;
        }
    }

    return right as usize;
}

fn upper_bound<T: Ord>(vec: &Vec<T>, x: T) -> usize {
    let (mut left, mut right): (i64, i64) = (-1, vec.len() as i64);

    while (right - left) > 1 {
        let mid = (right + left) / 2;

        if x < vec[mid as usize] {
            right = mid;
        } else {
            left = mid;
        }
    }

    return right as usize;
}