Problem:
Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

1 \leq N, Q \leq 2 \times 10^5

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.

Solution in Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/abc128/tasks/abc128_e

// https://qiita.com/penguinshunya/items/cd96803b74635aebefd6
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
fn read_vec_lines<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
struct WSTuple2<T1, T2>((T1, T2)); // White Space Tuple
impl<T1, T2> std::str::FromStr for WSTuple2<T1, T2>
where
    T1: std::str::FromStr,
    T2: std::str::FromStr,
{
    type Err = (); // should be never type "!"
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let vec: Vec<String> = s.split_whitespace().take(2).map(String::from).collect();
        Ok(WSTuple2((
            vec[0].parse().ok().unwrap(),
            vec[1].parse().ok().unwrap(),
        )))
    }
}
struct WSTuple3<T1, T2, T3>((T1, T2, T3)); // White Space Tuple
impl<T1, T2, T3> std::str::FromStr for WSTuple3<T1, T2, T3>
where
    T1: std::str::FromStr,
    T2: std::str::FromStr,
    T3: std::str::FromStr,
{
    type Err = (); // should be never type "!"
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let vec: Vec<String> = s.split_whitespace().take(3).map(String::from).collect();
        Ok(WSTuple3((
            vec[0].parse().ok().unwrap(),
            vec[1].parse().ok().unwrap(),
            vec[2].parse().ok().unwrap(),
        )))
    }
}
struct WSTuple4<T1, T2, T3, T4>((T1, T2, T3, T4)); // White Space Tuple
impl<T1, T2, T3, T4> std::str::FromStr for WSTuple4<T1, T2, T3, T4>
where
    T1: std::str::FromStr,
    T2: std::str::FromStr,
    T3: std::str::FromStr,
    T4: std::str::FromStr,
{
    type Err = (); // should be never type "!"
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let vec: Vec<String> = s.split_whitespace().take(3).map(String::from).collect();
        Ok(WSTuple4((
            vec[0].parse().ok().unwrap(),
            vec[1].parse().ok().unwrap(),
            vec[2].parse().ok().unwrap(),
            vec[3].parse().ok().unwrap(),
        )))
    }
}

/// Find the first element that satisfies cond, where cond is a lower-bound condition on vec.
/// Returns end if no element satisfies cond.
fn find_first_satisfy<T>(vec: &Vec<T>, start: usize, end: usize, cond: &Fn(&T) -> bool) -> usize {
    assert_ne!(start, end);
    if end - start == 1 {
        if cond(&vec[start]) {
            start
        } else {
            vec.len()
        }
    } else {
        let mid = start + (end - start) / 2;
        if cond(&vec[mid]) {
            std::cmp::min(find_first_satisfy(vec, start, mid, cond), mid)
        } else {
            find_first_satisfy(vec, mid, end, cond)
        }
    }
}

fn main() {
    let (n, q) = read::<WSTuple2<i32, i32>>().0;
    struct RoadWork {
        start: i32,
        end: i32,
        position: i32,
    }
    let mut road_works: Vec<RoadWork> = vec![];
    for _ in 0..n {
        let (si, ti, xi) = read::<WSTuple3<i32, i32, i32>>().0;
        road_works.push(RoadWork {
            start: si - xi,
            end: ti - 1 - xi,
            position: xi,
        });
    }
    let mut dis: Vec<i32> = vec![];
    for _ in 0..q {
        let di = read::<i32>();
        dis.push(di);
    }
    dis.sort();
    let mut tree: Vec<Vec<i32>> = vec![];
    let mut q2 = 1i32;
    loop {
        tree.push(vec![std::i32::MAX; q2 as usize]);
        if q2 >= q {
            break;
        }
        q2 = q2 * 2;
    }
    debug_assert_eq!(2i32.pow(tree.len() as u32 - 1), q2);
    for _ in q..q2 {
        dis.push(std::i32::MAX);
    }
    debug_assert_eq!(dis.len(), q2 as usize);
    let tree_depth = tree.len();
    let idx_in_level = |level: usize, idx: i32| -> usize {
        (idx / 2i32.pow((tree_depth - 1 - level) as u32)) as usize
    };
    for road_work in &road_works {
        let idx_start =
            find_first_satisfy(&dis, 0, dis.len(), &|q: &i32| *q >= road_work.start) as i32;
        let idx_end = find_first_satisfy(&dis, 0, dis.len(), &|q: &i32| *q > road_work.end) as i32;
        let mut idx = idx_start;
        while idx < idx_end {
            let mut level_width: Option<(usize, i32)> = None;
            for i in 0..tree.len() {
                let width = q2 / 2i32.pow(i as u32);
                if idx % width == 0 && idx + width <= idx_end {
                    level_width = Some((i, width));
                    break;
                }
            }
            let (level, width) = level_width.unwrap();
            let idx_in_level = idx_in_level(level, idx);
            tree[level][idx_in_level] =
                std::cmp::min(tree[level][idx_in_level], road_work.position);
            idx += width;
        }
    }
    for idx in 0..q {
        let mut min = std::i32::MAX;
        for level in 0..tree.len() {
            let idx_in_level = idx_in_level(level, idx);
            min = std::cmp::min(min, tree[level][idx_in_level]);
        }
        if min == std::i32::MAX {
            println!("-1");
        } else {
            println!("{}", min);
        }
    }
}