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 <vector>
#include <functional>
#include <algorithm>
#include <iostream>
#include <climits>
#include <map>
using namespace std;

template <typename MonoidType, typename OperatorType, class SegFunc>
struct LazySegmentTree {
    // node, lazy, update flag (for lazy), identity element
    int n;
    vector<MonoidType> node;
    vector<OperatorType> lazy;
    vector<bool> need_update;
    MonoidType E0;
    OperatorType E1;

    void build(int m, const vector<MonoidType> &v = vector<MonoidType>()) {
        if(v != vector<MonoidType>()) m = v.size();
        n = 1; while(n < m) n *= 2;

        node = vector<MonoidType>(2*n-1, E0);
        lazy = vector<OperatorType>(2*n-1, E1);
        need_update = vector<bool>(2*n-1, false);
        if(v != vector<MonoidType>()) {
            for(int i=0; i<m; i++) {
                node[n-1+i] = v[i];
            }
            for(int i=n-2; i>=0; i--) {
                node[i] = SegFunc::combine(node[2*i+1], node[2*i+2]);
            }
        }
    }

    // initialize
    LazySegmentTree() {}
    LazySegmentTree(int n_, MonoidType E0_, OperatorType E1_,
                    vector<MonoidType> v = vector<MonoidType>()) :
        E0(E0_), E1(E1_) {
        build(n_, v);
    }

    void eval(int k, int l, int r) {
        if(!need_update[k]) return;
        node[k] = SegFunc::update(node[k], SegFunc::accumulate(lazy[k], r - l));
        if(r - l > 1) {
            lazy[2*k+1] = SegFunc::lazy_update(lazy[2*k+1], lazy[k]);
            lazy[2*k+2] = SegFunc::lazy_update(lazy[2*k+2], lazy[k]);
            need_update[2*k+1] = need_update[2*k+2] = true;
        }
        lazy[k] = E1;
        need_update[k] = false;
    }

    void update(int a, int b, OperatorType x, int l, int r, int k) {
        eval(k, l, r);
        if(b <= l or  r <= a) return;
        if(a <= l and r <= b) {
            lazy[k] = SegFunc::lazy_update(lazy[k], x);
            need_update[k] = true;
            eval(k, l, r);
        }
        else {
            int mid = (l + r) / 2;
            update(a, b, x, l, mid, 2*k+1);
            update(a, b, x, mid, r, 2*k+2);
            node[k] = SegFunc::combine(node[2*k+1], node[2*k+2]);
        }
    }

    MonoidType query(int a, int b, int l, int r, int k) {
        if(b <= l or  r <= a) return E0;
        eval(k, l, r);
        if(a <= l and r <= b) return node[k];
        int mid = (l + r) / 2;
        MonoidType vl = query(a, b, l, mid, 2*k+1);
        MonoidType vr = query(a, b, mid, r, 2*k+2);
        return SegFunc::combine(vl, vr);
    }

    // update [a, b)-th element (applied value, x)
    void update(int a, int b, OperatorType x) {
        update(a, b, x, 0, n, 0);
    }

    // range query for [a, b)
    MonoidType query(int a, int b) {
        return query(a, b, 0, n, 0);
    }

    void dump() {
        fprintf(stderr, "[lazy]\n");
        for(int i=0; i<2*n-1; i++) {
            if(i == n-1) fprintf(stderr, "xxx ");
            if(lazy[i] == E1) fprintf(stderr, "  E ");
            else fprintf(stderr, "%3d ", lazy[i]);
        }
        fprintf(stderr, "\n");

        fprintf(stderr, "[node]\n");
        for(int i=0; i<2*n-1; i++) {
            if(i == n-1) fprintf(stderr, "xxx ");
            if(node[i] == E0) fprintf(stderr, "  E ");
            else fprintf(stderr, "%3d ", node[i]);
        }
        fprintf(stderr, "\n");
    }
};


// Verified on Apr 27, 2019
void DSL_2_D() {
    struct SegFunc {
        static int update(int a, int b) { return b; }
        static int combine(int a, int b) { return min(a, b); }
        static int lazy_update(int a, int b) { return b; }
        static int accumulate(int a, int b) { return a; }
    };
    
    int N, Q; cin >> N >> Q;
    LazySegmentTree<int, int, SegFunc> seg(N, INT_MAX, INT_MAX);
    
    int indexed = 0;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s; cin >> s;
            s -= indexed;
            cout << seg.query(s, s+1) << endl;
        }
        // seg.dump();
    }
}

// Verified on Apr 27, 2019
void DSL_2_E() {
    struct SegFunc {
        static int update(int a, int b) { return a + b; }
        static int combine(int a, int b) { return a + b; }
        static int lazy_update(int a, int b) { return a + b; }
        static int accumulate(int a, int x) { return a * x; }
    };

    int N, Q; cin >> N >> Q;
    LazySegmentTree<int, int, SegFunc> seg(N, 0, 0);
    
    int indexed = 1;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s; cin >> s;
            s -= indexed;
            cout << seg.query(s, s+1) << endl;
        }
        // seg.dump();
    }    
}

// Verified on Apr 27, 2019
void DSL_2_F() {
    struct SegFunc {
        static int update(int a, int b) { return b; }
        static int combine(int a, int b) { return min(a, b); }
        static int lazy_update(int a, int b) { return b; }
        static int accumulate(int a, int x) { return a; }
    };
    
    int N, Q; cin >> N >> Q;
    LazySegmentTree<int, int, SegFunc> seg(N, INT_MAX, 0);
    
    int indexed = 0;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s, t; cin >> s >> t; t++;
            s -= indexed; t -= indexed;
            cout << seg.query(s, t) << endl;
        }
        // seg.dump();
    }    
}

// Verified on Apr 27, 2019
void DSL_2_G() {
    using ll = long long int;
    struct SegFunc {
        static ll update(ll a, ll b) { return a + b; }
        static ll combine(ll a, ll b) { return a + b; }
        static ll lazy_update(ll a, ll b) { return a + b; }
        static ll accumulate(ll a, ll x) { return a * x; }
    };

    int N, Q; cin >> N >> Q;
    LazySegmentTree<ll, ll, SegFunc> seg(N, 0, 0);
    
    int indexed = 1;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s, t; cin >> s >> t; t++;
            s -= indexed; t -= indexed;
            cout << seg.query(s, t) << endl;
        }
        // seg.dump();
    }    
}

// Verified on Apr 27, 2019
void DSL_2_H() {
    int N, Q; cin >> N >> Q;
    using ll = long long int;

    struct SegFunc {
        static ll update(ll a, ll b) { return a + b; }
        static ll combine(ll a, ll b) { return min(a, b); }
        static ll lazy_update(ll a, ll b) { return a + b; }
        static ll accumulate(ll a, ll x) { return a; }
    };
    
    LazySegmentTree<ll, ll, SegFunc> seg(N, INT_MAX, 0, vector<ll>(N));
    
    int indexed = 0;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s, t; cin >> s >> t; t++;
            s -= indexed; t -= indexed;
            cout << seg.query(s, t) << endl;
        }
        // seg.dump();
    }    
}

// Verified on Apr 27, 2019
void DSL_2_I() {
    int N, Q; cin >> N >> Q;
    using ll = long long int;

    struct SegFunc {
        static ll update(ll a, ll b) { return b; }
        static ll combine(ll a, ll b) { return a + b; }
        static ll lazy_update(ll a, ll b) { return b; }
        static ll accumulate(ll a, ll x) { return a * x; }
    };

    LazySegmentTree<ll, ll, SegFunc> seg(N, 0, 0, vector<ll>(N));
    
    int indexed = 0;
    for(int i=0; i<Q; i++) {
        int query_type; cin >> query_type;
        if(query_type == 0) {
            int s, t, x; cin >> s >> t >> x; t++;
            s -= indexed, t -= indexed;
            seg.update(s, t, x);
        }
        if(query_type == 1) {
            int s, t; cin >> s >> t; t++;
            s -= indexed; t -= indexed;
            cout << seg.query(s, t) << endl;
        }
        // seg.dump();
    }    
}

void ABC128_E() {
    int N, Q; cin >> N >> Q;

    map<int, int> cmp; int id = 0;
    vector<int> s(N), t(N), x(N), p(Q);
    for(int i=0; i<N; i++) {
        cin >> s[i] >> t[i] >> x[i];
        s[i] = max(0, s[i] - x[i]);
        t[i] = max(0, t[i] - x[i]);
        cmp[ s[i] ]++; cmp[ t[i] ]++; cmp[ t[i] - 1 ]++;
    }
    for(int i=0; i<Q; i++) {
        cin >> p[i];
        cmp[ p[i] ]++;
    }
    for(auto &e : cmp) e.second = id++;
    for(int i=0; i<N; i++) {
        s[i] = cmp[ s[i] ], t[i] = cmp[ t[i] ];
    }
    for(int i=0; i<Q; i++) {
        p[i] = cmp[ p[i] ];
    }
    
    struct SegFunc {
        static int update(int a, int b) { return min(a, b); }
        static int combine(int a, int b) { return min(a, b); }
        static int lazy_update(int a, int b) { return min(a, b); }
        static int accumulate(int a, int x) { return a; }
    };
    LazySegmentTree<int, int, SegFunc> seg(id, INT_MAX, INT_MAX);
    
    for(int i=0; i<N; i++) {
        seg.update(s[i], t[i], x[i]);
    }
    for(int i=0; i<Q; i++) {
        int res = seg.query(p[i], p[i]+1);
        if(res == INT_MAX) cout << -1 << endl;
        else cout << res << endl;
    }
}

/*
void yuki_749() {
    int N, Q; cin >> N >> Q;

    
    
}
*/

int main() {
    // DSL_2_D();
    // DSL_2_E();
    // DSL_2_F();
    // DSL_2_G();
    // DSL_2_H();
    // DSL_2_I();
    ABC128_E();
    return 0;
}