Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

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

  ll N, Q;
  cin >> N >> Q;
  typedef pair<ll, pll> plll;
  vector<plll> qs;
  REP(i, N) {
    ll S, T, X;
    cin >> S >> T >> X;
    qs.push_back({X, {S, T}});
  }
  set<pll> D;
  REP(i, Q) {
    ll d;
    cin >> d;
    D.insert({d, i});
  }
  sort(qs.begin(), qs.end());
  vector<ll> res(Q, -1);
  REP(i, N) {
    if (D.empty())
      break;
    ll X = qs[i].first;
    ll S = qs[i].second.first;
    ll T = qs[i].second.second;
    // dump(X)
    // dump(S-X)
    // dump(T-1-X)
    auto itr = D.lower_bound({S - X, -1});
    if (itr == D.end())
      continue;
    // dump(itr->first)
    while (1) {
      ll d = itr->first;
      ll idx = itr->second;
      if (d > T - 1 - X)
        break;
      // dump(d)
      // dump(idx)
      res[idx] = X;
      auto itr2 = itr;
      itr++;
      D.erase(itr2);
    }
    // dump("--------------");
  }
  REP(i, Q) { cout << res[i] << endl; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

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

  ll N, Q;
  cin >> N >> Q;
  typedef pair<ll, pll> plll;
  vector<plll> qs;
  REP(i, N) {
    ll S, T, X;
    cin >> S >> T >> X;
    qs.push_back({X, {S, T}});
  }
  set<pll> D;
  REP(i, Q) {
    ll d;
    cin >> d;
    D.insert({d, i});
  }
  sort(qs.begin(), qs.end());
  vector<ll> res(Q, -1);
  REP(i, N) {
    if (D.empty())
      break;
    ll X = qs[i].first;
    ll S = qs[i].second.first;
    ll T = qs[i].second.second;
    // dump(X)
    // dump(S-X)
    // dump(T-1-X)
    auto itr = D.lower_bound({S - X, -1});
    if (itr == D.end())
      continue;
    // dump(itr->first)
    while (1) {
      if (itr == D.end())
        break;
      ll d = itr->first;
      ll idx = itr->second;
      if (d > T - 1 - X)
        break;
      // dump(d)
      // dump(idx)
      res[idx] = X;
      auto itr2 = itr;
      itr++;
      D.erase(itr2);
    }
    // dump("--------------");
  }
  REP(i, Q) { cout << res[i] << endl; }

  return 0;
}