Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

#define ma 212345

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, q, s[ma], t[ma], x[ma], d[ma];

  V v;

  set<int> st;

  V ro[ma][2];

  cin >> n >> q;
  REP(i, n) {
    cin >> s[i] >> t[i] >> x[i];
    s[i] -= x[i];
    t[i] -= x[i];

    if (st.count(s[i]) == 0)
      v.push_back(s[i]);
    if (st.count(t[i]) == 0)
      v.push_back(t[i]);

    st.insert(s[i]);
    st.insert(t[i]);
  }
  REP(i, q)
  cin >> d[i];

  v.push_back(1e9 + 1111);
  v.push_back(-100);

  SORT(v);

  map<int, int> mp;
  REP(i, v.size()) { mp[v[i]] = i; }

  REP(i, n) {
    ro[mp[s[i]]][0].push_back(i);
    ro[mp[t[i]]][1].push_back(i);
  }

  int di[412345];
  REP(i, v.size())
  di[i] = 1e9 + 100;

  priority_queue<P> qu;

  bool use[ma];
  fill(use, use + n + 1, false);

  use[n] = true;
  qu.push({-1e9 + 100, n});

  REP(i, v.size()) {
    for (int j : ro[i][0]) {
      use[j] = true;
      qu.push({-x[j], j});
    }
    for (int j : ro[i][1]) {
      use[j] = false;
    }

    while (qu.size() && !use[qu.top().second])
      qu.pop();

    di[i] = -qu.top().first;
  }

  REP(i, q) {
    int l = 0;
    int r = v.size();

    while (r - l > 1) {
      int mid = (l + r) / 2;

      if (v[mid] <= d[i])
        l = mid;
      else
        r = mid;
    }

    // cout << l << " ";

    if (-di[l] == -1e9 + 100)
      cout << -1 << endl;
    else
      cout << di[l] << endl;
  }

  /*
  REP(i, v.size())
  cout << di[i] << " ";
  cout << endl;

  REP(i, v.size())
  cout << v[i] << " ";
  cout << endl;
  */

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

#define ma 212345

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, q, s[ma], t[ma], x[ma], d[ma];

  V v;

  set<int> st;

  V ro[ma + ma][2];

  cin >> n >> q;
  REP(i, n) {
    cin >> s[i] >> t[i] >> x[i];
    s[i] -= x[i];
    t[i] -= x[i];

    if (st.count(s[i]) == 0)
      v.push_back(s[i]);
    if (st.count(t[i]) == 0)
      v.push_back(t[i]);

    st.insert(s[i]);
    st.insert(t[i]);
  }
  REP(i, q)
  cin >> d[i];

  v.push_back(1e9 + 1111);
  v.push_back(-100);

  SORT(v);

  map<int, int> mp;
  REP(i, v.size()) { mp[v[i]] = i; }

  REP(i, n) {
    ro[mp[s[i]]][0].push_back(i);
    ro[mp[t[i]]][1].push_back(i);
  }

  int di[412345];
  REP(i, v.size())
  di[i] = 1e9 + 100;

  priority_queue<P> qu;

  bool use[ma];
  fill(use, use + n + 1, false);

  use[n] = true;
  qu.push({-1e9 + 100, n});

  REP(i, v.size()) {
    for (int j : ro[i][0]) {
      use[j] = true;
      qu.push({-x[j], j});
    }
    for (int j : ro[i][1]) {
      use[j] = false;
    }

    while (qu.size() && !use[qu.top().second])
      qu.pop();

    di[i] = -qu.top().first;
  }

  REP(i, q) {
    int l = 0;
    int r = v.size();

    while (r - l > 1) {
      int mid = (l + r) / 2;

      if (v[mid] <= d[i])
        l = mid;
      else
        r = mid;
    }

    // cout << l << " ";

    if (-di[l] == -1e9 + 100)
      cout << -1 << endl;
    else
      cout << di[l] << endl;
  }

  /*
  REP(i, v.size())
  cout << di[i] << " ";
  cout << endl;

  REP(i, v.size())
  cout << v[i] << " ";
  cout << endl;
  */

  return 0;
}