Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> s(n), t(n), x(n);
  rep(i, n) {
    scanf("%lld%lld%lld", &s[i], &t[i], &x[i]);
    s[i] -= x[i];
    t[i] -= x[i];
  }
  auto cmp1 = [&](const int &i, const int &j) { return s[i] > s[j]; };
  auto cmp2 = [&](const int &i, const int &j) { return t[i] > t[j]; };
  priority_queue<int, vector<int>, decltype(cmp1)> que1(cmp1);
  priority_queue<int, vector<int>, decltype(cmp2)> que2(cmp2);
  multiset<int> S;
  rep(i, n) que1.push(i);
  rep(_, q) {
    int d;
    scanf("%d", &d);
    while (!que1.empty() && s[que1.top()] <= d) {
      int i = que1.top();
      que1.pop();
      S.insert(x[i]);
      que2.push(i);
    }
    while (!que2.empty() && t[que2.top()] <= d) {
      int i = que2.top();
      que2.pop();
      S.erase(S.find(x[i]));
    }
    printf("%d\n", S.empty() ? -1 : *S.begin());
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> s(n), t(n), x(n);
  rep(i, n) {
    scanf("%d%d%d", &s[i], &t[i], &x[i]);
    s[i] -= x[i];
    t[i] -= x[i];
  }
  auto cmp1 = [&](const int &i, const int &j) { return s[i] > s[j]; };
  auto cmp2 = [&](const int &i, const int &j) { return t[i] > t[j]; };
  priority_queue<int, vector<int>, decltype(cmp1)> que1(cmp1);
  priority_queue<int, vector<int>, decltype(cmp2)> que2(cmp2);
  multiset<int> S;
  rep(i, n) que1.push(i);
  rep(_, q) {
    int d;
    scanf("%d", &d);
    while (!que1.empty() && s[que1.top()] <= d) {
      int i = que1.top();
      que1.pop();
      S.insert(x[i]);
      que2.push(i);
    }
    while (!que2.empty() && t[que2.top()] <= d) {
      int i = que2.top();
      que2.pop();
      S.erase(S.find(x[i]));
    }
    printf("%d\n", S.empty() ? -1 : *S.begin());
  }
}