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 C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;

#define mem( arr, i ) fill( arr, arr + sizeof (arr), i )
#define MEM( arr, i ) memset( arr, i, sizeof (arr) )
#define Push push_back
#define Pair make_pair
typedef unsigned long long ull;
typedef long long lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
typedef vector<int> vcI;
typedef vector<lli> vcL;
typedef vector<pii> vPI;
typedef vector<pll> vPL;
//typedef priority_queue<T> prior_S<T>;
//typedef priority_queue< T, vector<T>, greater<T> > prior_B<T>;
const double PI = 3.141592653589793;
const lli I_MAX = 1LL << 60;
const lli mod = 1000000007;

struct p3l {
	lli S, T, X;
};

bool cmp(p3l x, p3l y) {
	if ( x.X == y.X ) return x.S < y.S;
	else return x.X < y.X;
}

// segment tree 

vcL tree( 800000+99, 0 ), lazy( 800000+99, 0 );

lli ls(lli x) { return x << 1; }
lli rs(lli x) { return x << 1|1; }

lli next(lli n) {
	int lg = (int)ceil( log2(n) );
	return 1LL << lg;
}

void update(vcL &tree, vcL &lazy, lli nl, lli nr, lli np, lli gl, lli gr, lli add) {
	// tree, lazy_tag, now_left, now_right, now_point, goal_left, goal_right, add_num
	lli mid = (nl+nr) >> 1;
	tree[np] = add;
	if ( gl <= nl and gr >= nr ) {
		if ( nl != nr ) lazy[np] = add;
	}
	else {
		lazy[ ls(np) ] = lazy[np];
		lazy[ rs(np) ] = lazy[np];
		lazy[np] = 0;
		if ( gl <= mid ) {
			update( tree, lazy, nl, mid, ls(np), gl, gr, add );
		}
		if ( gr >= mid+1 ) {
			update( tree, lazy, mid+1, nr, rs(np), gl, gr, add );
		}
	}
}

void query(vcL &tree, vcL &lazy, lli nl, lli nr, lli np, lli gl, lli gr, lli &sum) {
	// tree, lazy_tag, now_left, now_right, now_point, goal_left, goal_right
	lli mid = (nl+nr) >> 1;
	
	if ( gl <= nl and gr >= nr ) {
		sum = tree[np];
	}
	else {
		if ( lazy[np] ) {
			lazy[ ls(np) ] = lazy[np]; tree[ ls(np) ] = lazy[np];
			lazy[ rs(np) ] = lazy[np]; tree[ rs(np) ] = lazy[np];
			lazy[np] = 0;
		}
		if ( gl <= mid ) {
			query( tree, lazy, nl, mid, ls(np), gl, gr, sum );
		}
		if ( gr >= mid+1 ) {
			query( tree, lazy, mid+1, nr, rs(np), gl, gr, sum );
		}
	}
}

bool solve() {
	lli n, q;
	if ( cin >> n >> q );
	else return false;
	
	vector<p3l> block(n);
	vcL walk(q);
	
	for (auto &x : block) cin >> x.S >> x.T >> x.X;
	for (auto &x : walk) cin >> x;
	
	sort( block.begin(), block.end(), cmp );
	
	for (int i = n-1; i >= 0; --i) {
		vcL::iterator low = lower_bound( walk.begin(), walk.end(), block[i].S-block[i].X );
		vcL::iterator  up = upper_bound( walk.begin(), walk.end(), block[i].T-block[i].X-1 );
		// change [low, up) to block[i].X
		
//		cout << "lower_bound at position " << ( low - walk.begin() ) << '\n';
//		cout << "upper_bound at position " << ( up - walk.begin() ) << '\n';
		
		int l = low - walk.begin();
		int u =  up - walk.begin();
		
//		printf("[%d,%d) = %lld\n", l, u, block[i].X );
		update( tree, lazy, 1, next(q), 1, l+1, u, block[i].X+1 );
	}
	
	for (int i = 1; i <= q; ++i) {
		lli sum;
		query( tree, lazy, 1, next(q), 1, i, i, sum = 0 );
		cout << sum-1 << '\n';
	}
	
	return true;
}

int main() {
	int t = INT_MAX;
//	cin >> t;
	for (int i = 1; i <= t; ++i) {
//		printf("Case #%d: ", i );
		if ( !solve() ) break;
	}
	return 0;
}