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 <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n)  for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif

// $ cp-batch Roadwork | diff Roadwork.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address Roadwork.cpp && ./a.out

/*
 
 5/26/2019
 
 6:04-6:54 REx2
 6:56 AC
 
 https://img.atcoder.jp/abc128/editorial.pdf
 http://kmjp.hatenablog.jp/entry/2019/05/26/1300
 http://kazune-lab.net/contest/2019/05/26/abc128/
 https://twitter.com/knshnb/status/1132664716889690112
 
 Coordinate compression along with queries is easier
 
 */

const int MAX_N=1e6+1;
LL S[MAX_N],T[MAX_N],X[MAX_N],D[MAX_N];
int N,Q;

const LL Inf=1e18;

void solve() {
  vector<tuple<LL,int,LL>> es;
  REP(i,N) {
    LL s=S[i],t=T[i],x=X[i];
    es.emplace_back(s-x,0,x);
    es.emplace_back(t-x,1,x);
  }
  REP(i,Q) es.emplace_back(D[i],2,-1);
  sort(ALL(es));
  multiset<LL> Q;
  REP(i,SZ(es)) {
    LL t,x; int type; tie(t,type,x)=es[i];
    if(type==0) Q.emplace(x);
    if(type==1) {
      assert(Q.count(x));
      Q.erase(Q.find(x));
    }
    if(type==2) {
      LL res=-1;
      if(SZ(Q)) res=*Q.begin();
      cout<<res<<endl;
    }
  }
}

void solve_org() {
  map<LL,vector<LL>> imos;
  REP(i,N) {
    LL s=S[i],t=T[i],x=X[i];
    imos[s-x].push_back(x);
    imos[t-x].push_back(-x);
  }
  vector<pair<LL,LL>> X;
  X.emplace_back(-Inf,Inf);
  multiset<LL> cur;
  FORR(kvp,imos) {
    LL d=kvp.first;
    vector<LL> xs=kvp.second;
    FORR(x,xs) {
      if(x>0) {
        cur.emplace(x);
      } else {
        x=-x;
        assert(cur.count(x));
        cur.erase(cur.find(x));
      }
    }
    LL xx=Inf;
    if(SZ(cur)) xx=*cur.begin();
    X.emplace_back(d,xx);
  }
  X.emplace_back(Inf,Inf);
//  dumpc(X);
  REP(i,Q) {
    auto p=upper_bound(ALL(X),make_pair(D[i],Inf))-X.begin();
    assert(p<SZ(X));
    --p;
    assert(p>=0);
    LL d,x; tie(d,x)=X[p];
    LL res=-1;
    if(x!=Inf) res=x;
//        dump(i,D[i],p,res);
    cout<<res<<endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout<<setprecision(12)<<fixed;
  
  cin>>N>>Q;
  REP(i,N) cin>>S[i]>>T[i]>>X[i];
  REP(i,Q) cin>>D[i];
  solve();
  
  return 0;
}