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: Accepted)

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
#include<iomanip>

using namespace std;

typedef long long ll;

template <class Monoid1, class Monoid2> struct LazySegTree {
	int N;
	vector<Monoid1> node;
	vector<Monoid2> lazy;
	std::function<Monoid1(Monoid1, Monoid1)> operation1;
	std::function<Monoid2(Monoid2, Monoid2)> operation2;
	std::function<Monoid1(Monoid1, Monoid2)> operation3;
	std::function<Monoid2(Monoid2, int)> operation4;
	Monoid1 unit1;//Monoid1の単位元
	Monoid2 unit2;//Monoid2の単位元

	LazySegTree(vector<Monoid1>& data,
		std::function<Monoid1(Monoid1, Monoid1)> op1,
		std::function<Monoid2(Monoid2, Monoid2)> op2,
		std::function<Monoid1(Monoid1, Monoid2)> op3,
		std::function<Monoid2(Monoid2, int)> op4,
		Monoid1 U1, Monoid2 U2) {
		//データ　演算1~4　単位元1~2
		operation1 = op1;
		operation2 = op2;
		operation3 = op3;
		operation4 = op4;
		unit1 = U1;
		unit2 = U2;
		int size = data.size();
		N = 1;
		while (N < size)N <<= 1;
		node.resize(2 * N - 1, unit1);
		lazy.resize(2 * N - 1, unit2);

		for (int i = 0; i < size; i++) {
			node[i + N - 1] = data[i];
		}

		for (int i = N - 2; i >= 0; i--) {
			node[i] = operation1(node[i * 2 + 1], node[i * 2 + 2]);
		}
	}

	//番号kのデータを伝播させる。
	void eval(int k, int l, int r) {
		if (lazy[k] == unit2)return;
		node[k] = operation3(node[k], operation4(lazy[k], r - l));
		if (r - l > 1) {
			//子を持ってる
			lazy[2 * k + 1] = operation2(lazy[2 * k + 1], lazy[k]);
			lazy[2 * k + 2] = operation2(lazy[2 * k + 2], lazy[k]);
		}
		lazy[k] = unit2;
	}

	void update(Monoid2 val, int idx, int a, int b, int l = 0, int r = -1) {
		//[a, b)
		if (r < 0)r = N;
		eval(idx, l, r);
		//範囲外なら何もしない
		if (b <= l || r <= a)return;
		if (a <= l && r <= b) {
			//完全に被覆してるのなら、遅延配列を更新して評価。
			lazy[idx] = operation3(lazy[idx], val);
			eval(idx, l, r);
			return;
		}
		update(val, idx * 2 + 1, a, b, l, (l + r) / 2);
		update(val, idx * 2 + 2, a, b, (l + r) / 2, r);
		node[idx] = operation1(node[idx * 2 + 1], node[idx * 2 + 2]);
	}

	void update(Monoid2 val, int a, int b) {
		//[a, b)
		update(val, 0, a, b, 0, -1);
	}

	Monoid1 getval(int a, int b, int idx, int l, int r = -1) {
		eval(idx, l, r);
		//[a, b)を求める。　使い方はgetval(a, b, );
		if (r == -1)r = N;
		if (b <= l || r <= a)return unit1;
		if (a <= l && r <= b)return node[idx];
		Monoid1 left = getval(a, b, idx * 2 + 1, l, (l + r) / 2);
		Monoid1 right = getval(a, b, idx * 2 + 2, (l + r) / 2, r);
		return operation1(left, right);
	}

	Monoid1 getval(int a, int b) {
		//ラッパー。
		return getval(a, b, 0, 0, -1);
	}
};

int N, Q;
vector<int> S, T, X, D;
vector<int> pressref;

int unit = 1145141919;

void press(vector<int>& base1, vector<int>& base2, vector<int>& base3, vector<int>& pressref) {
	for (int i = 0; i < base1.size(); i++) {
		pressref.push_back(base1[i]);
	}
	for (int i = 0; i < base2.size(); i++) {
		pressref.push_back(base2[i]);
	}
	for (int i = 0; i < base3.size(); i++) {
		pressref.push_back(base3[i]);
	}
	sort(pressref.begin(), pressref.end());
	pressref.erase(unique(pressref.begin(), pressref.end()), pressref.end());

	for (int i = 0; i < base1.size(); i++) {
		base1[i] = lower_bound(pressref.begin(), pressref.end(), base1[i]) - pressref.begin();
	}
	for (int i = 0; i < base2.size(); i++) {
		base2[i] = lower_bound(pressref.begin(), pressref.end(), base2[i]) - pressref.begin();
	}
	for (int i = 0; i < base3.size(); i++) {
		base3[i] = lower_bound(pressref.begin(), pressref.end(), base3[i]) - pressref.begin();
	}
}

int main() {
	cin >> N >> Q;
	S.resize(N), T.resize(N), D.resize(Q), X.resize(N);
	for (int i = 0; i < N; i++) {
		int s, t, x;
		//[s, t)
		cin >> s >> t >> x;
		S[i] = s - x, T[i] = t - x;
		X[i] = x;
	}
	for (int i = 0; i < Q; i++) {
		cin >> D[i];
	}
	
	press(S, T, D, pressref);

	vector<int> tmp2(pressref.size(), unit);
	LazySegTree<int, int> lst(tmp2, 
		[](int a, int b) {return min(a, b); },
		[](int a, int b) {return min(a, b); }, 
		[](int a, int b) {return min(a, b); }, 
		[](int a, int b) {return a; },
		unit, unit);

	for (int i = 0; i < N; i++) {
		lst.update(X[i], S[i], T[i]);
	}
	for (int i = 0; i < Q; i++) {
		int val = lst.getval(D[i], D[i] + 1);
		if (val == unit)cout << -1 << endl;
		else {
			cout << val << endl;
		}
	}
	return 0;
}