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)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ 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 ) * } } ; ( $ ( $ 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 , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ 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 : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
#[doc = " RMaxQ & RUpdQ M1=max F1=M2=subs"]
#[doc = " RMinQ & RAddQ M1=min F1=M2=add"]
#[doc = " RSumQ & RAddQ M1=F1=M2=add                  F2=a*b"]
#[doc = " RSumQ & RUpdQ M1=add F1=M2=subs             F2=a*b"]
#[doc = " RSumQ & RXorQ M1=add F1=(b>=1?b-a:a) M2=a^b F2=a*b"]
pub struct LazySegmentTree<
    M1: Monoid,
    M2: Monoid,
    F1: Fn(&M1::T, &M2::T) -> M1::T,
    F2: Fn(&M2::T, usize) -> M2::T,
> {
    n: usize,
    seg: Vec<M1::T>,
    lazy: Vec<M2::T>,
    m1: M1,
    m2: M2,
    op: F1,
    p: F2,
}
impl<M1: Monoid, M2: Monoid, F1: Fn(&M1::T, &M2::T) -> M1::T, F2: Fn(&M2::T, usize) -> M2::T>
    LazySegmentTree<M1, M2, F1, F2>
{
    pub fn new(n: usize, m1: M1, m2: M2, op: F1, p: F2) -> LazySegmentTree<M1, M2, F1, F2> {
        let n = 1 << format!("{:b}", n - 1).len();
        let seg = vec![m1.mempty(); 2 * n - 1];
        let lazy = vec![m2.mempty(); 2 * n - 1];
        LazySegmentTree {
            n: n,
            seg: seg,
            lazy: lazy,
            m1: m1,
            m2: m2,
            op: op,
            p: p,
        }
    }
    pub fn from_vec(
        v: Vec<M1::T>,
        m1: M1,
        m2: M2,
        op: F1,
        p: F2,
    ) -> LazySegmentTree<M1, M2, F1, F2> {
        let n = 1 << format!("{:b}", v.len() - 1).len();
        let mut seg = vec![m1.mempty(); 2 * n - 1];
        for (i, x) in v.into_iter().enumerate() {
            seg[i + n - 1] = x;
        }
        for i in (0..n - 1).rev() {
            seg[i] = m1.mappend(&seg[2 * i + 1], &seg[2 * i + 2]);
        }
        let lazy = vec![m2.mempty(); 2 * n - 1];
        LazySegmentTree {
            n: n,
            seg: seg,
            lazy: lazy,
            m1: m1,
            m2: m2,
            op: op,
            p: p,
        }
    }
    pub fn eval(&mut self, k: usize, len: usize) {
        if self.lazy[k] != self.m2.mempty() {
            if k * 2 + 1 < self.n * 2 - 1 {
                self.lazy[2 * k + 1] = self.m2.mappend(&self.lazy[2 * k + 1], &self.lazy[k]);
                self.lazy[2 * k + 2] = self.m2.mappend(&self.lazy[2 * k + 2], &self.lazy[k]);
            }
            self.seg[k] = (self.op)(&self.seg[k], &(self.p)(&self.lazy[k], len));
            self.lazy[k] = self.m2.mempty();
        }
    }
    fn update_inner(
        &mut self,
        l: usize,
        r: usize,
        x: M2::T,
        k: usize,
        a: usize,
        b: usize,
    ) -> M1::T {
        self.eval(k, b - a);
        if b <= l || r <= a {
            self.seg[k].clone()
        } else if l <= a && b <= r {
            self.lazy[k] = self.m2.mappend(&self.lazy[k], &x);
            (self.op)(&self.seg[k], &(self.p)(&self.lazy[k], b - a))
        } else {
            let lx = self.update_inner(l, r, x.clone(), k * 2 + 1, a, (a + b) / 2);
            let rx = self.update_inner(l, r, x, k * 2 + 2, (a + b) / 2, b);
            let res = self.m1.mappend(&lx, &rx);
            self.seg[k] = res.clone();
            res
        }
    }
    pub fn update(&mut self, l: usize, r: usize, x: M2::T) -> M1::T {
        let n = self.n;
        self.update_inner(l, r, x, 0, 0, n)
    }
    fn query_inner(&mut self, l: usize, r: usize, k: usize, a: usize, b: usize) -> M1::T {
        self.eval(k, b - a);
        if b <= l || r <= a {
            self.m1.mempty()
        } else if l <= a && b <= r {
            self.seg[k].clone()
        } else {
            let lx = self.query_inner(l, r, k * 2 + 1, a, (a + b) / 2);
            let rx = self.query_inner(l, r, k * 2 + 2, (a + b) / 2, b);
            self.m1.mappend(&lx, &rx)
        }
    }
    pub fn query(&mut self, l: usize, r: usize) -> M1::T {
        let n = self.n;
        self.query_inner(l, r, 0, 0, n)
    }
}
pub struct MinMonoid<T: Ord + Clone> {
    min: T,
}
impl<T: Ord + Clone> MinMonoid<T> {
    pub fn new(min: T) -> MinMonoid<T> {
        MinMonoid { min: min }
    }
}
impl<T: Ord + Clone> Monoid for MinMonoid<T> {
    type T = T;
    fn mempty(&self) -> T {
        self.min.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        std::cmp::min(x, y).clone()
    }
}
pub fn lower_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
    binary_search(|i| v[i as usize] >= x, v.len() as i64, -1) as usize
}
pub trait Bisect: Copy {
    fn halve(self, other: Self) -> Self;
    fn section_end(self, other: Self) -> bool;
}
impl Bisect for usize {
    fn halve(self, other: Self) -> Self {
        if self > other {
            other + (self - other) / 2
        } else {
            self + (other - self) / 2
        }
    }
    fn section_end(self, other: Self) -> bool {
        (if self > other {
            self - other
        } else {
            other - self
        }) <= 1
    }
}
impl Bisect for i64 {
    fn halve(self, other: Self) -> Self {
        (self + other) / 2
    }
    fn section_end(self, other: Self) -> bool {
        (self - other).abs() <= 1
    }
}
impl Bisect for f64 {
    fn halve(self, other: Self) -> Self {
        (self + other) / 2.
    }
    fn section_end(self, other: Self) -> bool {
        (self - other).abs() <= 1e-8
    }
}
pub fn binary_search<T: Bisect, F: Fn(T) -> bool>(f: F, ok: T, err: T) -> T {
    let mut ok = ok;
    let mut err = err;
    while !ok.section_end(err) {
        let m = ok.halve(err);
        if f(m) {
            ok = m;
        } else {
            err = m;
        }
    }
    ok
}
pub struct Compress<T: Ord> {
    v: Vec<T>,
}
impl<T: Bisect + Ord> Compress<T> {
    pub fn get(&self, x: T) -> usize {
        lower_bound(&self.v, x)
    }
    pub fn len(&self) -> usize {
        self.v.len()
    }
}
impl<T: Ord> std::iter::FromIterator<T> for Compress<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Compress<T> {
        use std::collections::BTreeSet;
        let v = iter
            .into_iter()
            .collect::<BTreeSet<T>>()
            .into_iter()
            .collect::<Vec<T>>();
        Compress { v: v }
    }
}
const INF: i64 = 1_001_001_001;
fn main() {
    input! { n, q, stx: [(i64,i64,i64);n], d: [i64; q] };
    let mut v = vec![-INF, INF];
    v.extend(stx.iter().map(|&(s, _t, x)| s - x));
    v.extend(stx.iter().map(|&(_s, t, x)| t - x));
    let z = v.into_iter().collect::<Compress<_>>();
    let mut seg = LazySegmentTree::new(
        z.len(),
        MinMonoid::new(INF),
        MinMonoid::new(INF),
        |&x, &y| std::cmp::min(x, y),
        |&x, _y| x,
    );
    for &(s, t, x) in &stx {
        let i = z.get(s - x);
        let j = z.get(t - x);
        seg.update(i, j, x);
    }
    for &a in &d {
        let i = z.get(a);
        let x = seg.query(i, i + 1);
        println!("{}", if x >= INF { -1 } else { x });
    }
}