Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<double> VD;
typedef vector<VD> VVD;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<ll, PL>> p(n);
  REP(i, n) {
    ll s = in(), t = in(), x = in();
    p[i] = make_pair(x, PL(s, t));
  }
  sort(ALL(p));
  set<PL> st;
  REP(i, q) {
    ll d = in();
    st.insert(PL(d, i));
  }
  VL ans(q, -1);
  REP(i, n) {
    ll x = p[i].first;
    ll s = p[i].second.first - x, t = p[i].second.second - x;
    set<PL> del;
    auto itr = st.lower_bound(PL(s, -1));
    while (itr->first < t) {
      ans[itr->second] = x;
      del.insert(*itr);
      itr++;
    }
    for (auto tmp : del)
      st.erase(tmp);
  }
  REP(i, q) printf("%lld\n", ans[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<double> VD;
typedef vector<VD> VVD;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<ll, PL>> p(n);
  REP(i, n) {
    ll s = in(), t = in(), x = in();
    p[i] = make_pair(x, PL(s, t));
  }
  sort(ALL(p));
  set<PL> st;
  REP(i, q) {
    ll d = in();
    st.insert(PL(d, i));
  }
  VL ans(q, -1);
  REP(i, n) {
    ll x = p[i].first;
    ll s = p[i].second.first - x, t = p[i].second.second - x;
    set<PL> del;
    auto itr = st.lower_bound(PL(s, -1));
    while (itr->first < t && itr != st.end()) {
      ans[itr->second] = x;
      del.insert(*itr);
      itr++;
    }
    for (auto tmp : del)
      st.erase(tmp);
  }
  REP(i, q) printf("%lld\n", ans[i]);
  return 0;
}