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

#[allow(dead_code)]
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()
}

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

type Work = (i32, i32, i32); // S', T', X

fn main() {
    let nq :Vec<usize>  = read_vec();
    let n = nq[0];
    let q = nq[1];


    let mut works : Vec<Work> = Vec::with_capacity(n);
    for _i in 0..n {
        let stx :Vec<i32> = read_vec();
        let s = stx[0] - stx[2];
        let t = stx[1] - stx[2];
        let w : Work = (s, t, stx[2]);
        works.push(w);
    }

    let mut starts : Vec<i32> = Vec::with_capacity(q);
    for _i in 0..q {
        let d : i32 = read();
        starts.push(d);
    }

    let result = solve(q, &mut works, &starts);
    //result.iter().for_each(|x|println!("{}", x));
    for stop in &result {
        println!("{}", stop);
    }
}

fn solve(q: usize, works : &mut [Work], starts: &[i32]) -> Vec<i32> {
    let mut stops : Vec<i32> = vec![-1; q];
    for w in works {
        let m = get_intercepted(q, starts, w);
        //println!("{:?} -> {:?}", w, m);
        if m == (q, q) {
            break;
        }
        for i in m.0..m.1 {
            if stops[i] < 0 || w.2 < stops[i] {
                stops[i] = w.2;
            }
        }
    }
    return stops;
}

fn get_intercepted(q: usize, starts: &[i32], w: &Work) -> (usize, usize) {
    let m = binary_search(q, starts, w);
    if m == q {
        return (q, q);
    }

    // check neighbors
    let mut from = m;
    let mut to = m + 1;
    for i in (0..m).rev() {
        if starts[i] < w.0 {
            from = i + 1;
            break;
        }
    }

    for i in m+1..q {
        if w.1 <= starts[i] {
            to = i;
            break;
        }
    }

    return (from, to);
}

fn binary_search(q: usize, starts: &[i32], w: &Work) -> usize {
    let mut lo = 0;
    let mut hi = q - 1;
    while lo <= hi {
        let mid = (lo + hi) / 2;
        if starts[mid] < w.0 {
            if mid == q - 1 {
                break;
            }
            lo = mid + 1;
        }else if w.1 <= starts[mid] {
            if mid == 0 {
                break;
            }
            hi = mid - 1;
        } else {
            return mid;
        }
    }
    return q;
}