Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
// #define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

vector<int> init(int size) {
  vector<int> par(size, -1);
  return par;
}

int root(int x, vector<int> &par) {
  if (par[x] < 0) {
    return x;
  } else {
    return par[x] = root(par[x], par);
  }
}

bool same(int x, int y, vector<int> &par) {
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int> &par) {
  x = root(x, par), y = root(y, par);
  if (same(x, y, par))
    return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int> &par) { return -par[root(x, par)]; }

int pow(int x, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x % MOD, n / 2);
  else
    return x * pow(x, n - 1) % MOD;
}

signed main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, pair<int, int>>> p;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    p.push_back(make_pair(s - x, make_pair(1, x)));
    p.push_back(make_pair(t - x, make_pair(0, x)));
  }
  sort(p.begin(), p.end());
  int d[210000];
  rep(i, q) cin >> d[i];
  int ans[210000];
  set<int> st;
  int j = 0;
  rep(i, q) {
    while (d[i] >= p[j].first) {
      if (p[j].second.first)
        st.insert(p[j].second.second);
      else
        st.erase(p[j].second.second);
      j++;
    }
    ans[i] = (st.empty() ? -1 : *st.begin());
  }
  rep(i, q) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
// #define int long long
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const int INF = 1010000000;
const double EPS = 1e-10;
const pair<int, int> fd[] = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                             make_pair(0, -1)};

vector<int> init(int size) {
  vector<int> par(size, -1);
  return par;
}

int root(int x, vector<int> &par) {
  if (par[x] < 0) {
    return x;
  } else {
    return par[x] = root(par[x], par);
  }
}

bool same(int x, int y, vector<int> &par) {
  return root(x, par) == root(y, par);
}

void unite(int x, int y, vector<int> &par) {
  x = root(x, par), y = root(y, par);
  if (same(x, y, par))
    return;
  par[x] += par[y];
  par[y] = x;
}

int size(int x, vector<int> &par) { return -par[root(x, par)]; }

int pow(int x, int n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return pow(x * x % MOD, n / 2);
  else
    return x * pow(x, n - 1) % MOD;
}

signed main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, pair<int, int>>> p;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    p.push_back(make_pair(s - x, make_pair(1, x)));
    p.push_back(make_pair(t - x, make_pair(0, x)));
  }
  sort(p.begin(), p.end());
  int d[210000];
  rep(i, q) cin >> d[i];
  int ans[210000];
  set<int> st;
  int j = 0;
  rep(i, q) {
    while (d[i] >= p[j].first) {
      if (j >= p.size())
        break;
      if (p[j].second.first)
        st.insert(p[j].second.second);
      else
        st.erase(p[j].second.second);
      j++;
    }
    ans[i] = (st.empty() ? -1 : *st.begin());
  }
  rep(i, q) cout << ans[i] << endl;
}