Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  int n, q, s[200010], t[200010], x[200010], d[200010];
  bool ison[200010] = {};
  vector<P> on[400010];
  vector<int> query[400010], off[400010], vec;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i] >> x[i];
    vec.pb(s[i] - x[i]);
    vec.pb(t[i] - x[i]);
  }
  for (int i = 0; i < q; i++) {
    cin >> d[i];
    vec.pb(d[i]);
  }
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
  for (int i = 0; i < n; i++) {
    int ind = lower_bound(all(vec), s[i] - x[i]) - vec.begin();
    on[ind].pb(P(x[i], i));
    ind = lower_bound(all(vec), t[i] - x[i]) - vec.begin();
    off[ind].pb(i);
  }
  for (int i = 0; i < q; i++) {
    int ind = lower_bound(all(vec), d[i]) - vec.begin();
    query[ind].pb(i);
  }
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < vec.size(); i++) {
    for (P p : on[i]) {
      que.push(p);
      ison[p.second] = true;
    }
    for (int v : off[i]) {
      ison[v] = false;
    }
    for (int v : query[i]) {
      while (!que.empty() && !ison[que.top().second])
        que.pop();
      if (que.empty())
        cout << -1 << endl;
      else
        cout << que.top().first << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  int n, q, s[200010], t[200010], x[200010], d[200010];
  bool ison[200010] = {};
  vector<P> on[600010];
  vector<int> query[600010], off[600010], vec;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> t[i] >> x[i];
    vec.pb(s[i] - x[i]);
    vec.pb(t[i] - x[i]);
  }
  for (int i = 0; i < q; i++) {
    cin >> d[i];
    vec.pb(d[i]);
  }
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
  for (int i = 0; i < n; i++) {
    int ind = lower_bound(all(vec), s[i] - x[i]) - vec.begin();
    on[ind].pb(P(x[i], i));
    ind = lower_bound(all(vec), t[i] - x[i]) - vec.begin();
    off[ind].pb(i);
  }
  for (int i = 0; i < q; i++) {
    int ind = lower_bound(all(vec), d[i]) - vec.begin();
    query[ind].pb(i);
  }
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < vec.size(); i++) {
    for (P p : on[i]) {
      que.push(p);
      ison[p.second] = true;
    }
    for (int v : off[i]) {
      ison[v] = false;
    }
    for (int v : query[i]) {
      while (!que.empty() && !ison[que.top().second])
        que.pop();
      if (que.empty())
        cout << -1 << endl;
      else
        cout << que.top().first << endl;
    }
  }
}