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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// graph
const int MAX_V = 100010;
const int MAX_E = 100010;
struct edge {
    int from, to;
    ll cost;
};
edge ES[MAX_E];
vector<edge> G[MAX_V];
ll d[MAX_V];
ll prev_path[MAX_V];
ll V, E;

const ll MOD = (ll) 1e9 + 7;
const int MAX_INT = 1 << 17;
const long long INF_LL = 1LL << 60;
vector<bool> prime;

#define all(x) (x).begin(),(x).end()
#define PRI(n) cout << n <<endl;
#define PRI2(n, m) cout << n << " " << m << " "<<endl;

#define REP(i, n)  for(int i = 0; i < n; ++i)
#define FOR(i, t, n)  for(ll i = t; i <= (ll)n; ++i)

void Era(int x) {
    prime.resize(x + 1, 1);
    prime[0] = 0;
    prime[1] = 0;
    FOR(i, 2, x) {
        if (prime[i]) {
            for (int j = 2 * i; j <= x; j += i) {
                prime[j] = 0;
            }
        }
    }
}

bool isPrime(ll x) {
    if (x == 0)return 0;
    if (x == 1)return 0;
    if (x == 2)return 1;
    if (x % 2 == 0)return 0;
    FOR(i, 3, sqrt(x) + 1) {
        if (x % i == 0)return 0;
    }
    return 1;
}

ll GCD(ll a, ll b) {
    if (b == 0)return a;
    return GCD(b, a % b);
}

ll LCM(ll a, ll b) {
    ll gcd = GCD(a, b);
    return a / gcd * b;
}

ll nCr(ll n, ll r) {
    vector<ll> C(r + 1);
    C[0] = 1;
    FOR(i, 1, n)
        for (ll j = min(i, r); j < 1; --j)
            C[j] = (C[j] + C[j - 1]);
    return C[r];
}

template<class T>
class SegTree {
    int n;
    vector<T> data;
    T def;
    function<T(T, T)> operation;
    function<T(T, T)> update;

    T _query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return def;
        if (a <= l && r <= b)
            return data[k];
        else {
            T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
            T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
            return operation(c1, c2);
        }
    }

public:
    SegTree(size_t _n, T _def, function<T(T, T)> _operation,
            function<T(T, T)> _update)
            : def(_def), operation(_operation), update(_update) {
        n = 1;
        while (n < _n) {
            n *= 2;
        }
        data = vector<T>(2 * n - 1, def);
    }

    void change(int i, T x) {
        i += n - 1;
        data[i] = update(data[i], x);
        while (i > 0) {
            i = (i - 1) / 2;
            data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    T query(int a, int b) {
        return _query(a, b, 0, 0, n);
    }

    T operator[](int i) {
        return data[i + n - 1];
    }
};

struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<int> sz;

    UnionFind(int N) {
        for (int i = 0; i < N; ++i) {
            par.push_back(i);
            rank.push_back(0);
            sz.push_back(-1);
        }
    }

    int find(int x) {
        if (par[x] == x)return x;
        else return par[x] = find(par[x]);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (rank[x] < rank[y])par[x] = y;
        else {
            par[y] = x;
            if (rank[x] == rank[y])rank[x]++;
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int root(int x) {
        if (sz[x] < 0) return x;
        else return sz[x] = root(sz[x]);
    }

    bool isSame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (sz[x] > sz[y]) swap(x, y); // merge technique
        sz[x] += sz[y];
        sz[y] = x;
        return true;
    }

    int size(int x) {
        return -sz[root(x)];
    }

};

void Bellman_short(int s) {
    REP(i, V)d[i] = 1LL << 50;
    d[s] = 0;
    REP(i, V)REP(i, E) {
            edge e = ES[i];
            if (d[e.from] != 1LL << 50 && d[e.to] > d[e.from] + e.cost) {
                d[e.to] = d[e.from] + e.cost;
            }
        }

}

bool Bellman_negLoop(int s) {
    REP(i, V)d[i] = 1LL << 50;
    d[s] = 0;
    REP(i, V)REP(j, E) {
            edge e = ES[j];
            if (d[e.from] != 1LL << 50 && d[e.to] > d[e.from] + e.cost) {
                d[e.to] = d[e.from] + e.cost;
                if (i == V - 1)return true;
            }
        }
    return false;
}

void dijkstra(int s) {
    typedef pair<ll, ll> P;
    priority_queue<P, vector<P>, greater<P>>
            Q;
    fill(d, d + V, MOD);
    fill(prev_path, prev_path + V, -1);
    d[s] = 0;
    Q.push(P(0, s));
    while (!Q.empty()) {
        P p = Q.top();
        Q.pop();
        ll v = p.second;
        if (d[v] < p.first)continue;
        for (edge e:G[v]) {
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                Q.push(P(d[e.to], e.to));
                prev_path[e.to] = v;

            }
        }
    }
}

vector<ll> getPath(int t) {
    vector<ll> path;
    for (; t != -1; t = prev_path[t]) {
        path.push_back(t);
    }
    reverse(all(path));
    return path;
}

class Combination {

private:
    vector<ll> fac, finv, inv;

public:
    Combination(ll N) {
        fac.resize(N);
        finv.resize(N);
        inv.resize(N);
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for (int i = 2; i < N; i++) {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    ll nCr(int n, int k) {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
};

int N, Q;

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<tuple<int, int, int>> T(N);
    vector<ll> res(Q, -1);
    set<pair<int, int>> D;

    REP(i, N) {
        int s, t, x;
        cin >> s >> t >> x;
        T[i] = make_tuple(x, s - x, t - x - 1);
    }

    REP(i, Q) {
        int d;
        cin >> d;
        D.emplace(d, i);
    }

    sort(all(T));

    REP(i, N) {
        int s = get<1>(T[i]), t = get<2>(T[i]), x = get<0>(T[i]);
        auto vs = lower_bound(all(D), make_pair(s, 0));
        auto vt = upper_bound(all(D), make_pair(t, Q));
        for (auto it = vs; it != vt; it++)res[it->second] = x;
        D.erase(vs, vt);
    }
    REP(i, Q)PRI(res[i])
    return 0;
}