Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

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

  int n, q;
  cin >> n >> q;
  vector<tuple<int, int, int>> xst(n);
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    xst[i] = make_tuple(x, s - x, t - x);
  }
  sort(all(xst));
  set<P> st;
  rep(i, q) {
    int d;
    cin >> d;
    st.emplace(d, i);
  }
  vector<int> ans(q, -1);

  rep(i, n) {
    int s, t, x;
    tie(x, s, t) = xst[i];
    auto it = st.lower_bound(P(s, 0));
    while (it->first < t) {
      ans[it->second] = x;
      st.erase(it++);
    }
  }
  rep(i, q) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

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

  int n, q;
  cin >> n >> q;
  vector<tuple<int, int, int>> xst(n);
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    xst[i] = make_tuple(x, s - x, t - x);
  }
  sort(all(xst));
  set<P> st;
  rep(i, q) {
    int d;
    cin >> d;
    st.emplace(d, i);
  }
  vector<int> ans(q, -1);

  rep(i, n) {
    int s, t, x;
    tie(x, s, t) = xst[i];
    auto it = st.lower_bound(P(s, 0));
    while (it != st.end()) {
      if (it->first >= t)
        break;
      ans[it->second] = x;
      st.erase(it++);
    }
  }
  rep(i, q) cout << ans[i] << endl;
}