Problem p03033 (C++)

Buggy submission (status: Runtime Error):
/// kAAzuki Hoshino
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}

int n, q, ans[202020];
vector<pair<int, pair<int, int>>> v;
set<int> s;
map<int, int> mp;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v.pb({z, {x, y}});
  }
  sort(all(v));

  for (int i = 1; i <= q; i++) {
    int d;
    cin >> d;
    s.insert(d);
    mp[d] = i;
  }

  for (int i = 0; i < n; i++) {
    int mn = v[i].sc.fs;
    int mx = v[i].sc.sc;
    int x = v[i].fs;

    while (true) {
      if (s.size()) {
        auto it = s.lower_bound(mn - x);
        if ((*it) < mx - x) {
          // cout << (*it) << " " << mn << " " << mx << " " << x << endl;
          ans[mp[(*it)]] = x;
          s.erase(it);
        } else
          break;
      } else
        break;
    }
  }

  for (int i = 1; i <= q; i++) {
    if (ans[i] == 0)
      ans[i] = -1;
    cout << ans[i] << endl;
  }

  return 0;
}
///....


Fixed submission:
/// kAAzuki Hoshino
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}

int n, q, ans[202020];
vector<pair<int, pair<int, int>>> v;
set<int> s;
map<int, int> mp;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v.pb({z, {x, y}});
  }
  sort(all(v));

  for (int i = 1; i <= q; i++) {
    int d;
    cin >> d;
    s.insert(d);
    mp[d] = i;
  }

  for (int i = 0; i < n; i++) {
    int mn = v[i].sc.fs;
    int mx = v[i].sc.sc;
    int x = v[i].fs;

    while (true) {
      if (s.size()) {
        auto it = s.lower_bound(mn - x);
        if (it == s.end())
          break;
        if ((*it) < mx - x) {
          // cout << (*it) << " " << mn << " " << mx << " " << x << endl;
          ans[mp[(*it)]] = x;
          s.erase(it);
        } else
          break;
      } else
        break;
    }
  }

  for (int i = 1; i <= q; i++) {
    if (ans[i] == 0)
      ans[i] = -1;
    cout << ans[i] << endl;
  }

  return 0;
}
///....