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: Wrong Answer)

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdint>
#include<cstdlib>
#include<functional>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

template<typename T>
void sort_u(T& v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(),v.end()), v.end());
}

template<typename T, typename DT=T>
struct monoid_factory {
  using value_type = T;
  using vv_type = std::function<value_type(value_type,value_type)>;
  using va_type = std::function<void(value_type&,value_type)>;
  value_type e;
  vv_type v_v;
  va_type v_assign;
  monoid_factory(value_type in_e, vv_type&& in_vv, va_type&& in_va):
    e(in_e), v_v(in_vv), v_assign(in_va) {}
};

// seg_tree
template<typename MonoidFactory>
struct seg_tree {
  using T = typename MonoidFactory::value_type;
  std::vector<T> data_;
  int NN;
  MonoidFactory mf;
  
  seg_tree(int N, MonoidFactory const& in_mf) : mf(in_mf) {
    NN = 1;
    while(NN < N+2) NN *= 2;
    data_.assign(NN*2-1, mf.e);
  }
  
  void update(int k, T v) {
    k += NN-1;
    data_[k] = v;
    while(k > 0) {
      k = (k-1)/2;
      data_[k] = mf.v_v(data_[k*2+1], data_[k*2+2]);
    }
  }
  
  void update(int a, int b, T v, int k, int l, int r) {
    if(r <= a || b <= l) return;
    if(a <= l && r <= b) {
       mf.v_assign(data_[k], v);
       return;
    }
    int m = (l+r)/2;
    update(a, b, v, k*2+1, l, m);
    update(a, b, v, k*2+2, m, r);
  }
  void update(int a, int b, T v){ update(a, b, v, 0, 0, NN); }
  
  T query(int a, int b, int k, int l, int r) {
    if(r <= a || b <= l) return mf.e;
    if(a <= l && r <= b) return data_[k];
    int m = (l+r)/2;
    T vl = query(a, b, k*2+1, l, m);
    T vr = query(a, b, k*2+2, m, r);
    return mf.v_v(vl, vr);
  }
  T query(int a, int b){ return query(a, b, 0, 0, NN); }
};


struct C {
  int l, r, v;
  C(int S, int T, int X): l(std::max(0,S-X)), r(std::max(0, T-X)), v(X) {}
};

int const INF = 1e9+5;

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  int N, Q; std::cin >> N >> Q;
  std::vector<int> xs({0, INF});
  std::vector<C> cond;
  for(int i = 0; i < N; ++i) {
    int S, T, X; std::cin >> S >> T >> X;
    if(X >= T) continue;
    cond.emplace_back(S, T, X);
    xs.push_back(cond.back().l);
    xs.push_back(cond.back().r);
  }
  sort_u(xs); 
  for(auto& c: cond) {
    c.l = std::lower_bound(xs.begin(), xs.end(), c.l) - xs.begin();
    c.r = std::lower_bound(xs.begin(), xs.end(), c.r) - xs.begin();
  }
  
  auto op_min = [](int x, int y)->int { return std::min(x, y); };
  auto ch_min = [](int& o1, int o2)->void { if(o1 > o2) o1 = o2; };
  monoid_factory<int> mf(INF, op_min, ch_min);
  seg_tree<monoid_factory<int>> st(xs.size(), mf);
  for(auto& c: cond) st.update(c.l, c.r, c.v);
  for(int i = 0; i < Q; ++i) {
    int D; std::cin >> D;
    int d = std::upper_bound(xs.begin(), xs.end(), D) - xs.begin();
    int ans = st.query(d-1, d);
    if(ans == INF) ans = -1;
    std::cout << ans << "\n";
  }
  return 0;
}