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)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

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

    let mut constructions = vec![];
    for _ in 0..n {
        let v = read_vec::<i64>();
        constructions.push((v[0] - v[2], v[1] - v[2], v[2]));
    }
    let mut queries = vec![];
    for _ in 0..q {
        queries.push(read::<i64>());
    }
    let mut coordinates = BTreeSet::new();
    for &(s, t, _) in constructions.iter() {
        coordinates.insert(s);
        coordinates.insert(t);
    }
    for &d in queries.iter() {
        coordinates.insert(d);
    }

    let mut compress_dict = HashMap::new();

    for (i, &x) in coordinates.iter().enumerate() {
        compress_dict.insert(x, i);
    }

    let mut segtree = LazySegmentTree::new(coordinates.len());
    for &(s, t, x) in constructions.iter() {
        segtree.update(
            compress_dict[&s],
            compress_dict[&t],
            x,
            0,
            0,
            coordinates.len(),
        );
    }
    for &d in queries.iter() {
        let didx = compress_dict[&d];
        let min_x = segtree.getmin(didx, didx + 1, 0, 0, coordinates.len());
        if min_x == std::i64::MAX {
            println!("{:?}", -1);
        } else {
            println!("{:?}", min_x);
        }
    }
}

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()
}

#[derive(Debug)]
struct LazySegmentTree {
    n: usize,
    node: Vec<i64>,
    lazy: Vec<i64>,
}

impl LazySegmentTree {
    fn new(sz: usize) -> LazySegmentTree {
        let mut n = 1;
        while n < sz {
            n *= 2;
        }
        LazySegmentTree {
            n: n,
            node: vec![std::i64::MAX; n * 2],
            lazy: vec![std::i64::MAX; n * 2],
        }
    }

    fn eval(&mut self, k: usize, l: usize, r: usize) {
        if self.lazy[k] != std::i64::MAX {
            self.node[k] = min(self.node[k], self.lazy[k]);
            if r - l > 1 {
                self.lazy[2 * k + 1] = min(self.lazy[2 * k + 1], self.lazy[k]);
                self.lazy[2 * k + 2] = min(self.lazy[2 * k + 2], self.lazy[k]);
            }
            self.lazy[k] = std::i64::MAX;
        }
    }

    fn update(&mut self, a: usize, b: usize, x: i64, k: usize, l: usize, r: usize) {
        // update [l, r) k = 0, l = 0, r = n is default
        self.eval(k, l, r);

        // if out of range, do nothing.
        if b <= l || r <= a {
            return;
        }

        if a <= l && r <= b {
            self.lazy[k] = min(self.lazy[k], x);
            self.eval(k, l, r);
        } else {
            self.update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            self.update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            self.node[k] = min(self.node[2 * k + 1], self.node[2 * k + 2]);
        }
    }

    fn getmin(&mut self, a: usize, b: usize, k: usize, l: usize, r: usize) -> i64 {
        if b <= l || r <= a {
            return std::i64::MAX;
        }

        self.eval(k, l, r);
        if a <= l && r <= b {
            return self.node[k];
        }

        min(
            self.getmin(a, b, 2 * k + 1, l, (l + r) / 2),
            self.getmin(a, b, 2 * k + 2, (l + r) / 2, r),
        )
    }
}