Problem p03033 (C++)

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

using namespace std;
typedef long long ll;

// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define RALL(x) (x).rbegin(), (x).rend() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define seq vector
#define READLINE(vec, time)                                                    \
  for (int indexofvec = 0; indexofvec < time; indexofvec++)                    \
  cin >> vec[indexofvec]
#define VPRINT(vec)                                                            \
  for (auto i : vec) {                                                         \
    cout << i << " ";                                                          \
  }                                                                            \
  cout << endl;
#define lambda []
#define clambda [=]
#define reflambda [&]

template <typename A, typename B, class functype>
void select(vector<A> &in, vector<B> &out, functype func) {
  out.resize(in.size());

  for (int i = 0; i < in.size(); i++) {
    out[i] = func(in[i]);
  }
}

signed main() {

  int N, Q;
  cin >> N >> Q;
  vector<tuple<ll, int, ll>> ivents(0);
  REP(i, N) {
    ll S, T, X;
    cin >> S >> T >> X;
    ivents.push_back(make_tuple(S - X, 1, X));
    ivents.push_back(make_tuple(T - X, 0, X));
  }
  sort(ALL(ivents));
  ivents.push_back(make_tuple(-(1 << 30), 0, 0));
  auto st = ivents.begin();
  auto ed = ivents.end();
  ll d, pos;
  set<ll> using_pos;
  int flg;
  REP(i, Q) {
    cin >> d;
    while (get<0>(*st) <= d) {
      pos = get<2>(*st);
      flg = get<1>(*st);
      if (flg == 1) {
        using_pos.insert(pos);
      } else {
        using_pos.erase(pos);
      }
      st++;
    }

    if (using_pos.empty()) {
      cout << -1 << endl;
    } else {
      cout << *(using_pos.begin()) << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define RALL(x) (x).rbegin(), (x).rend() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define seq vector
#define READLINE(vec, time)                                                    \
  for (int indexofvec = 0; indexofvec < time; indexofvec++)                    \
  cin >> vec[indexofvec]
#define VPRINT(vec)                                                            \
  for (auto i : vec) {                                                         \
    cout << i << " ";                                                          \
  }                                                                            \
  cout << endl;
#define lambda []
#define clambda [=]
#define reflambda [&]

template <typename A, typename B, class functype>
void select(vector<A> &in, vector<B> &out, functype func) {
  out.resize(in.size());

  for (int i = 0; i < in.size(); i++) {
    out[i] = func(in[i]);
  }
}

signed main() {

  int N, Q;
  cin >> N >> Q;
  vector<tuple<ll, int, ll>> ivents(0);
  REP(i, N) {
    ll S, T, X;
    cin >> S >> T >> X;
    ivents.push_back(make_tuple(S - X, 1, X));
    ivents.push_back(make_tuple(T - X, 0, X));
  }
  sort(ALL(ivents));
  ivents.push_back(make_tuple((1 << 30), 0, 0));
  auto st = ivents.begin();
  auto ed = ivents.end();
  ll d, pos;
  set<ll> using_pos;
  int flg;
  REP(i, Q) {
    cin >> d;
    while (get<0>(*st) <= d) {
      pos = get<2>(*st);
      flg = get<1>(*st);
      if (flg == 1) {
        using_pos.insert(pos);
      } else {
        using_pos.erase(pos);
      }
      st++;
    }

    if (using_pos.empty()) {
      cout << -1 << endl;
    } else {
      cout << *(using_pos.begin()) << endl;
    }
  }
}