Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for (LL i = (l); i < (r); ++i)
#define incII(i, l, r) for (LL i = (l); i <= (r); ++i)
#define decID(i, l, r) for (LL i = (r)-1; i >= (l); --i)
#define decII(i, l, r) for (LL i = (r); i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
template <typename T> bool setmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmineq(T &a, T b) {
  if (b <= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmaxeq(T &a, T b) {
  if (b >= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
LL mo(LL a, LL b) {
  assert(b > 0);
  a %= b;
  if (a < 0) {
    a += b;
  }
  return a;
}
LL fl(LL a, LL b) {
  assert(b > 0);
  return (a > 0 ? a / b : (a - b + 1) / b);
}
LL ce(LL a, LL b) {
  assert(b > 0);
  return (a < 0 ? a / b : (a + b - 1) / b);
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define BC __builtin_popcountll
#define SC static_cast
#define SI(v) SC<int>(v.size())
#define SL(v) SC<LL>(v.size())
#define RF(e, v) for (auto &e : v)
#define ef else if
#define UR assert(false)

// ---- ----

#define UQwoS(v) v.erase(unique(ALL(v)), v.end())
#define UQ(v)                                                                  \
  sort(ALL(v));                                                                \
  UQwoS(v)
#define LB(v, x) distance(v.begin(), lower_bound(ALL(v), x))
#define UB(v, x) distance(v.begin(), upper_bound(ALL(v), x))

#define PQ priority_queue
template <typename T> class RPQ : public PQ<T, vector<T>, greater<T>> {};

#define until(e) while (!(e))

const int M = 200000;
int n, q, d[M];
array<int, 3> a[M];

int main() {
  cin >> n >> q;
  inc(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    a[i] = {x, s - x, t - x};
  }
  inc(i, q) { cin >> d[i]; }
  sort(a, a + n);

  vector<int> v;
  inc(i, n) { v.PB(a[i][1]); }
  inc(i, n) { v.PB(a[i][2]); }
  inc(i, q) { v.PB(d[i]); }
  UQ(v);

  vector<vector<pair<bool, int>>> w(SI(v));
  inc(i, n) {
    w[LB(v, a[i][1])].EB(true, i);
    w[LB(v, a[i][2])].EB(false, i);
  }

  set<int> ds;
  inc(i, q) { ds.insert(d[i]); }

  RPQ<int> Q;
  vector<bool> ok(q, false);
  inc(i, SI(v)) {
    RF(e, w[i]) {
      if (e.FI) {
        Q.push(e.SE);
        ok[e.SE] = true;
      } else {
        ok[e.SE] = false;
      }
    }

    if (ds.count(v[i]) == 1) {
      until(Q.empty() || ok[Q.top()]) { Q.pop(); }
      cout << (Q.empty() ? -1 : a[Q.top()][0]) << "\n";
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for (LL i = (l); i < (r); ++i)
#define incII(i, l, r) for (LL i = (l); i <= (r); ++i)
#define decID(i, l, r) for (LL i = (r)-1; i >= (l); --i)
#define decII(i, l, r) for (LL i = (r); i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
template <typename T> bool setmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmineq(T &a, T b) {
  if (b <= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmaxeq(T &a, T b) {
  if (b >= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
LL mo(LL a, LL b) {
  assert(b > 0);
  a %= b;
  if (a < 0) {
    a += b;
  }
  return a;
}
LL fl(LL a, LL b) {
  assert(b > 0);
  return (a > 0 ? a / b : (a - b + 1) / b);
}
LL ce(LL a, LL b) {
  assert(b > 0);
  return (a < 0 ? a / b : (a + b - 1) / b);
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define BC __builtin_popcountll
#define SC static_cast
#define SI(v) SC<int>(v.size())
#define SL(v) SC<LL>(v.size())
#define RF(e, v) for (auto &e : v)
#define ef else if
#define UR assert(false)

// ---- ----

#define UQwoS(v) v.erase(unique(ALL(v)), v.end())
#define UQ(v)                                                                  \
  sort(ALL(v));                                                                \
  UQwoS(v)
#define LB(v, x) distance(v.begin(), lower_bound(ALL(v), x))
#define UB(v, x) distance(v.begin(), upper_bound(ALL(v), x))

#define PQ priority_queue
template <typename T> class RPQ : public PQ<T, vector<T>, greater<T>> {};

#define until(e) while (!(e))

const int M = 200000;
int n, q, d[M];
array<int, 3> a[M];

int main() {
  cin >> n >> q;
  inc(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    a[i] = {x, s - x, t - x};
  }
  inc(i, q) { cin >> d[i]; }
  sort(a, a + n);

  vector<int> v;
  inc(i, n) { v.PB(a[i][1]); }
  inc(i, n) { v.PB(a[i][2]); }
  inc(i, q) { v.PB(d[i]); }
  UQ(v);

  vector<vector<pair<bool, int>>> w(SI(v));
  inc(i, n) {
    w[LB(v, a[i][1])].EB(true, i);
    w[LB(v, a[i][2])].EB(false, i);
  }

  set<int> ds;
  inc(i, q) { ds.insert(d[i]); }

  RPQ<int> Q;
  vector<bool> ok(n, false);
  inc(i, SI(v)) {
    RF(e, w[i]) {
      if (e.FI) {
        Q.push(e.SE);
        ok[e.SE] = true;
      } else {
        ok[e.SE] = false;
      }
    }

    if (ds.count(v[i]) == 1) {
      until(Q.empty() || ok[Q.top()]) { Q.pop(); }
      cout << (Q.empty() ? -1 : a[Q.top()][0]) << "\n";
    }
  }

  return 0;
}