Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <iomanip>       //cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <iterator>      //iterators
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, gcd, lcm
#include <queue>         // queue
#include <set>           // sets
#include <stack>         // stack
#include <string>        // string
#include <unordered_map> // hashed by keys
#include <vector>        // vectors
#define rep(i, n) for (long long i = 0; i < n; i++)

typedef long long ll;           // at least int64 > 9*10^18
typedef unsigned long long ull; // at least uint64

struct Roadwork {
  ll s, t, x;
  Roadwork(ll ss, ll tt, ll xx) : s(ss), t(tt), x(xx) {}
};

ll ans[200001];
int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  ll n, q;
  std::cin >> n >> q;
  std::vector<Roadwork> RW;
  RW.reserve(n);
  std::set<ll> P;
  rep(i, n) {
    ll s, t, x;
    std::cin >> s >> t >> x;
    RW.emplace_back(s - x, t - x, x);
  }
  std::unordered_map<ll, ll> um;
  rep(i, q) {
    ll d;
    std::cin >> d;
    P.insert(d);
    um[d] = i;
    ans[i] = -1;
  }
  std::sort(RW.begin(), RW.end(), [](Roadwork a, Roadwork b) {
    if (a.x == b.x)
      return a.s < b.s;
    return a.x < b.x;
  });
  for (auto r : RW) {
    while (P.size() > 0) {
      auto p = lower_bound(P.begin(), P.end(), r.s);
      if (p == P.end())
        break;
      if (r.t <= (*p))
        break;
      ans[um[(*p)]] = r.x;
      P.erase(p);
    }
  }
  rep(i, q) std::cout << ans[i] << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <iomanip>       //cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <iterator>      //iterators
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, gcd, lcm
#include <queue>         // queue
#include <set>           // sets
#include <stack>         // stack
#include <string>        // string
#include <unordered_map> // hashed by keys
#include <vector>        // vectors
#define rep(i, n) for (long long i = 0; i < n; i++)

typedef long long ll;           // at least int64 > 9*10^18
typedef unsigned long long ull; // at least uint64

struct Roadwork {
  ll s, t, x;
  Roadwork(ll ss, ll tt, ll xx) : s(ss), t(tt), x(xx) {}
};

ll ans[200001];
int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  ll n, q;
  std::cin >> n >> q;
  std::vector<Roadwork> RW;
  RW.reserve(n);
  std::set<ll> P;
  rep(i, n) {
    ll s, t, x;
    std::cin >> s >> t >> x;
    RW.emplace_back(s - x, t - x, x);
  }
  std::unordered_map<ll, ll> um;
  rep(i, q) {
    ll d;
    std::cin >> d;
    P.insert(d);
    um[d] = i;
    ans[i] = -1;
  }
  std::sort(RW.begin(), RW.end(), [](Roadwork a, Roadwork b) {
    if (a.x == b.x)
      return a.s < b.s;
    return a.x < b.x;
  });
  for (auto r : RW) {
    while (P.size() > 0) {
      auto p = P.lower_bound(r.s);
      if (p == P.end())
        break;
      if (r.t <= (*p))
        break;
      ans[um[(*p)]] = r.x;
      P.erase(p);
    }
  }
  rep(i, q) std::cout << ans[i] << "\n";

  return 0;
}