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: Time Limit Exceeded)

use std::io::*;

const SP: u8 = b' ';
const LF: u8 = b'\n';

struct CharacterInput<'a> {
    locked_stdin: StdinLock<'a>,
    bytes_buffer: Vec<u8>,
}

impl<'a> CharacterInput<'a> {
    fn read(&mut self, delimiter: u8) -> &[u8] {
        self.bytes_buffer.clear();

        let len = self
            .locked_stdin
            .read_until(delimiter, &mut self.bytes_buffer)
            .unwrap();

        if len > 0 {
            let len = len - 1;
            if self.bytes_buffer[len] == delimiter {
                self.bytes_buffer.truncate(len);
            }
        }

        &self.bytes_buffer
    }

    fn scan_u(&mut self, delimiter: u8) -> u32 {
        self.read(delimiter)
            .into_iter()
            .fold(0, |accumulator, byte| {
                (byte - b'0') as u32 + accumulator * 10
            })
    }

    fn scan_u_vec(&mut self, n: usize, separator: u8, delimiter: u8) -> Vec<u32> {
        let mut vector = Vec::with_capacity(n as usize);
        for _i in 0..n - 1 {
            vector.push(self.scan_u(separator));
        }
        vector.push(self.scan_u(delimiter));
        vector
    }
}

fn create_cin<'a>(input: StdinLock<'a>, buffer_capacity: usize) -> CharacterInput {
    CharacterInput {
        locked_stdin: input,
        bytes_buffer: Vec::with_capacity(buffer_capacity),
    }
}

struct CharacterOutput {
    string_buffer: String,
}

impl CharacterOutput {
    fn add<T: ToString>(&mut self, n: T, separator: char) {
        self.string_buffer += &n.to_string();
        self.string_buffer.push(separator);
    }

    fn flush(&mut self) {
        let out = stdout();
        let mut out = out.lock();
        out.write_all(self.string_buffer.as_bytes()).unwrap();
        self.string_buffer.clear();
    }
}

fn create_cout(buffer_capacity: usize) -> CharacterOutput {
    CharacterOutput {
        string_buffer: String::with_capacity(buffer_capacity),
    }
}

// ------------------------------------------------------------------------

#[derive(Clone, Copy)]
struct RoadworkEvent {
    departure_time: i32,
    position: u32,
    event_type: u8,
}

fn main() {
    let cin = stdin();
    let cin = &mut create_cin(cin.lock(), 32);

    // read header
    let roadwork_count = cin.scan_u(SP) as usize; // n
    let person_count = cin.scan_u(LF) as usize; // q

    // read roadwork data
    let mut roadwork_events: Vec<RoadworkEvent> = Vec::with_capacity(2 * roadwork_count);

    for _ in 0..roadwork_count {
        let s = cin.scan_u(SP) as i32;
        let t = cin.scan_u(SP) as i32;
        let x = cin.scan_u(LF);

        if t >= x as i32 {
            let departure_time_to = t - x as i32; // because x(t) = t - d for each person
            let departure_time_from = s - x as i32;
            roadwork_events.push(RoadworkEvent {
                departure_time: departure_time_from,
                event_type: 1,
                position: x,
            });
            roadwork_events.push(RoadworkEvent {
                departure_time: departure_time_to,
                event_type: 0,
                position: x,
            });
        }
    }

    roadwork_events.shrink_to_fit();
    roadwork_events.sort_by(|a, b| {
        let time_order = a.departure_time.partial_cmp(&b.departure_time);
        match time_order {
            Some(std::cmp::Ordering::Equal) => a.event_type.partial_cmp(&b.event_type).unwrap(),
            _ => time_order.unwrap(),
        }
    });

    // read departure time data of each person
    let departure_time_list = cin.scan_u_vec(person_count, LF, LF);

    // define how to determine stop position of each person
    let roadwork_events = &roadwork_events;
    let current_roadwork_positions = &mut std::collections::HashSet::new();

    let get_stop_position = |person_departure_time: u32| {
        current_roadwork_positions.clear();

        for event in roadwork_events
            .into_iter()
            .take_while(|event| event.departure_time <= person_departure_time as i32)
        {
            match event.event_type {
                1 => current_roadwork_positions.insert(event.position),
                0 => current_roadwork_positions.remove(&event.position),
                _ => false,
            };
        }

        current_roadwork_positions
            .iter()
            .map(|n| *n as i32)
            .min()
            .unwrap_or(-1)
    };

    // output
    let cout = &mut create_cout((6 + 1) * person_count);

    for stop_position in departure_time_list.into_iter().map(get_stop_position) {
        cout.add(stop_position, '\n');
    }

    cout.flush();
}