Problem p03033 (C++)

Buggy submission (status: Runtime Error):
//{{{
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(prints)
FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde)
    FUNC(printdu);
#endif
template <class S, class T> istream &operator>>(istream &in, pair<S, T> &p) {
  return in >> p.first >> p.second;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> ss(n), ts(n), xs(n);
  for (int i = 0; i < n; i++) {
    cin >> ss[i] >> ts[i] >> xs[i];
  }
  // vector<int> ds(q); cin >> ds;
  set<pair<int, int>> ds2;
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    ds2.emplace(d, i);
  }
  vector<int> is(n);
  iota(is.begin(), is.end(), 0);
  sort(is.begin(), is.end(), [&](int i, int j) { return xs[i] < xs[j]; });
  vector<int> anss(q, -1);
  for (auto i : is) {
    int l = ss[i] - xs[i], r = ts[i] - xs[i];
    printde(ss[i], ts[i], xs[i], l, r);
    if (ds2.empty()) {
      break;
    }
    auto ite = ds2.lower_bound(pair<int, int>(l, 0));
    if (ite == ds2.end()) {
      continue;
    }
    while (ite->first < r) {
      anss[ite->second] = xs[i];
      ite = ds2.erase(ite);
      if (ite == ds2.end()) {
        continue;
      }
    }
  }
  for (auto &ans : anss) {
    cout << ans << '\n';
  }
}


Fixed submission:
//{{{
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(prints)
FUNC(printw) FUNC(printwe) FUNC(printb) FUNC(printbe) FUNC(printd) FUNC(printde)
    FUNC(printdu);
#endif
template <class S, class T> istream &operator>>(istream &in, pair<S, T> &p) {
  return in >> p.first >> p.second;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> ss(n), ts(n), xs(n);
  for (int i = 0; i < n; i++) {
    cin >> ss[i] >> ts[i] >> xs[i];
  }
  // vector<int> ds(q); cin >> ds;
  set<pair<int, int>> ds2;
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    ds2.emplace(d, i);
  }
  vector<int> is(n);
  iota(is.begin(), is.end(), 0);
  sort(is.begin(), is.end(), [&](int i, int j) { return xs[i] < xs[j]; });
  vector<int> anss(q, -1);
  for (auto i : is) {
    int l = ss[i] - xs[i], r = ts[i] - xs[i];
    printde(ss[i], ts[i], xs[i], l, r);
    if (ds2.empty()) {
      break;
    }
    auto ite = ds2.lower_bound(pair<int, int>(l, 0));
    if (ite == ds2.end()) {
      continue;
    }
    while (ite->first < r) {
      anss[ite->second] = xs[i];
      ite = ds2.erase(ite);
      if (ite == ds2.end()) {
        break;
      }
    }
  }
  for (auto &ans : anss) {
    cout << ans << '\n';
  }
}