Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

using P = pair<int, int>;
void solve() {
  int n, q;
  cin >> n >> q;
  int a, b, c;
  vector<pair<int, P>> v;
  set<pair<int, int>> st;
  rep(i, n) {
    cin >> a >> b >> c;
    v.eb(c, P{a - c, b - c});
  }
  sort(v.begin(), v.end());
  rep(i, q) {
    int d;
    cin >> d;
    st.emplace(d, i);
  }
  vector<int> ans(q, -1);
  rep(i, n) {
    auto itr = st.lower_bound(make_pair(v[i].second.first, -1));
    while (!st.empty()) {
      if (v[i].second.second > itr->first)
        ans[itr->second] = v[i].first;
      else
        break;
      st.erase(itr++);
    }
  }
  rep(i, q) cout << ans[i] << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;

using P = pair<int, int>;
void solve() {
  int n, q;
  cin >> n >> q;
  int a, b, c;
  vector<pair<int, P>> v;
  set<pair<int, int>> st;
  rep(i, n) {
    cin >> a >> b >> c;
    v.eb(c, P{a - c, b - c});
  }
  sort(v.begin(), v.end());
  rep(i, q) {
    int d;
    cin >> d;
    st.emplace(d, i);
  }
  vector<int> ans(q, -1);
  rep(i, n) {
    auto itr = st.lower_bound(make_pair(v[i].second.first, -1));
    while (itr != st.end()) {
      if (v[i].second.second > itr->first)
        ans[itr->second] = v[i].first;
      else
        break;
      st.erase(itr++);
    }
  }
  rep(i, q) cout << ans[i] << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}