prithivMLmods/Coder-Stat huggingface dataset:
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

const ll INF = 1e18 + 10;

int N, Q;

int main() {
  cin >> N >> Q;
  set<P> D;
  vector<tuple<int, int, int>> X;
  rep(i, N) {
    int s, t, x;
    cin >> s >> t >> x;
    X.emplace_back(x, s - x, t - x);
  }
  rep(i, Q) {
    int d;
    cin >> d;
    D.insert(P(d, i));
  }
  vector<int> res(Q, -1);
  sort(ALL(X));
  rep(i, N) {
    int s, t, x;
    tie(x, s, t) = X[i];
    if (D.empty())
      continue;
    auto it = lower_bound(D.begin(), D.end(), P(s, -1));
    while (!D.empty() && it != D.end()) {
      if (it->first >= t)
        break;
      res[it->second] = x;
      D.erase(it++);
    }
  }
  rep(i, Q) cout << res[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

const ll INF = 1e18 + 10;

int N, Q;

int main() {
  cin >> N >> Q;
  set<P> D;
  vector<tuple<int, int, int>> X;
  rep(i, N) {
    int s, t, x;
    cin >> s >> t >> x;
    X.emplace_back(x, s - x, t - x);
  }
  rep(i, Q) {
    int d;
    cin >> d;
    D.insert(P(d, i));
  }
  vector<int> res(Q, -1);
  sort(ALL(X));
  rep(i, N) {
    int s, t, x;
    tie(x, s, t) = X[i];
    if (D.empty())
      continue;
    auto it = D.lower_bound(P(s, -1));
    while (D.size() && it != D.end()) {
      if (it->first >= t)
        break;
      res[it->second] = x;
      D.erase(it++);
    }
  }
  rep(i, Q) cout << res[i] << endl;
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

set<pair<int, pair<int, int>>>
    query; // time, q, x. q=0:delete or 1: insert or 2:query
Int N, Q;

int main(void) {
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    query.insert({s - x, make_pair(1, x)});
    query.insert({t - x, make_pair(0, x)});
  }
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    query.insert({d, make_pair(2, i)});
  }

  vector<int> D(Q);
  multiset<int> se;
  for (auto it : query) {
    int time = it.first;
    int q = it.second.first;
    int x = it.second.second;
    if (q == 0) {
      se.erase(lower_bound(se.begin(), se.end(), x));
    } else if (q == 1) {
      se.insert(x);
    } else {
      if (se.empty()) {
        D[x] = -1;
      } else {
        D[x] = *se.begin();
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    cout << D[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

set<pair<int, pair<int, int>>>
    query; // time, q, x. q=0:delete or 1: insert or 2:query
Int N, Q;

int main(void) {
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    query.insert({s - x, make_pair(1, x)});
    query.insert({t - x, make_pair(0, x)});
  }
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    query.insert({d, make_pair(2, i)});
  }

  vector<int> D(Q);
  multiset<int> se;
  for (auto it : query) {
    int time = it.first;
    int q = it.second.first;
    int x = it.second.second;
    if (q == 0) {
      se.erase(se.find(x));
    } else if (q == 1) {
      se.insert(x);
    } else {
      if (se.empty()) {
        D[x] = -1;
      } else {
        D[x] = *se.begin();
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    cout << D[i] << endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define BIT(X, i) ((X >> i) & 1)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define r_all(a) (a).rbegin(), (a).rend()

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef pair<ll, pii> piii;
typedef vector<pii> vii;
typedef vector<piii> viii;
typedef priority_queue<ll> pq;

const int MOD = 1e9 + 7;
const int INF = (1 << 30);
const ll INFL = (1L << 62);

int main() {
  int n, q;
  cin >> n >> q;
  vi ans(q, -1);
  viii events;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    events.pb(mp(s - x, mp(1, x)));
    events.pb(mp(t - x, mp(-1, x)));
  }
  sort(all(events));
  vi d(q);
  rep(i, q) { cin >> d[i]; }

  set<int> closed;
  int di = 0;
  for (auto e : events) {
    int t = e.ff, io = e.ss.ff, x = e.ss.ss;
    while (d[di] < t) {
      if (!closed.empty()) {
        ans[di] = *closed.begin();
      }
      di++;
    }
    if (io == 1) {
      closed.insert(x);
    } else {
      closed.erase(x);
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define BIT(X, i) ((X >> i) & 1)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define r_all(a) (a).rbegin(), (a).rend()

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef pair<ll, pii> piii;
typedef vector<pii> vii;
typedef vector<piii> viii;
typedef priority_queue<ll> pq;

const int MOD = 1e9 + 7;
const int INF = (1 << 30);
const ll INFL = (1L << 62);

int main() {
  int n, q;
  cin >> n >> q;
  vi ans(q, -1);
  viii events;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    events.pb(mp(s - x, mp(1, x)));
    events.pb(mp(t - x, mp(-1, x)));
  }
  sort(all(events));
  vi d(q);
  rep(i, q) { cin >> d[i]; }

  set<int> closed;
  int di = 0;
  for (auto e : events) {
    int t = e.ff, io = e.ss.ff, x = e.ss.ss;
    while (di < q && d[di] < t) {
      if (!closed.empty()) {
        ans[di] = *closed.begin();
      }
      di++;
    }
    if (io == 1) {
      closed.insert(x);
    } else {
      closed.erase(x);
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);

using namespace std;
using Int = long long;
// using namespace boost::multiprecision;

const Int MOD = 1000000007;

void fast_input() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int main(void) {
  int N, Q;
  cin >> N >> Q;
  vector<tuple<int, int, int>> S;
  vector<int> ans(Q, -1);
  set<pair<int, int>> D;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    S.push_back({c, a, b});
  }
  sort(S.begin(), S.end());
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert({d, i});
  }

  sort(S.begin(), S.end());
  for (int i = 0; i < N; i++) {
    auto itr = D.lower_bound({get<1>(S[i]) - get<0>(S[i]), 0});
    while (itr->first < get<2>(S[i]) - get<0>(S[i])) {
      ans[itr->second] = get<0>(S[i]);
      D.erase(itr++);
    }
  }
  for (auto itr = D.begin(); itr != D.end(); itr++) {
    ans[itr->second] = -1;
  }
  for (auto i : ans) {
    cout << i << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);

using namespace std;
using Int = long long;
// using namespace boost::multiprecision;

const Int MOD = 1000000007;

void fast_input() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int main(void) {
  int N, Q;
  cin >> N >> Q;
  vector<tuple<int, int, int>> S;
  vector<int> ans(Q, -1);
  set<pair<int, int>> D;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    S.push_back({c, a, b});
  }
  sort(S.begin(), S.end());
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert({d, i});
  }

  sort(S.begin(), S.end());
  for (int i = 0; i < N; i++) {
    auto itr = D.lower_bound({get<1>(S[i]) - get<0>(S[i]), 0});
    while (itr->first < get<2>(S[i]) - get<0>(S[i]) && itr != D.end()) {
      ans[itr->second] = get<0>(S[i]);
      D.erase(itr++);
    }
  }
  for (auto itr = D.begin(); itr != D.end(); itr++) {
    ans[itr->second] = -1;
  }
  for (auto i : ans) {
    cout << i << endl;
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  // VARIABLES
  int N, Q;
  vector<tuple<int, int, int>> L;
  set<tuple<int, int>> D;

  // INPUT
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    L.emplace_back(s - x, t - x, x);
  }

  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert(make_tuple(d, i));
  }

  // PROC
  sort(L.begin(), L.end(),
       [](tuple<int, int, int> left, tuple<int, int, int> right) {
         return get<2>(left) <= get<2>(right);
       });

  vector<int> DD(Q, -1);
  for (auto l : L) {
    int s, t, x;
    tie(s, t, x) = l;

    auto it = D.lower_bound(make_tuple(s, -1));
    while (it != D.end()) {
      int d = get<0>(*it);
      int idx = get<1>(*it);

      if (d >= t) {
        break;
      }

      DD[idx] = x;
      D.erase(it++);
    }
  }

  // OUTPUT
  for (auto d : DD) {
    cout << d << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  // VARIABLES
  int N, Q;
  vector<tuple<int, int, int>> L;
  set<tuple<int, int>> D;

  // INPUT
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    L.emplace_back(s - x, t - x, x);
  }

  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert(make_tuple(d, i));
  }

  // PROC
  sort(L.begin(), L.end(),
       [](const tuple<int, int, int> &left, const tuple<int, int, int> &right) {
         if (get<2>(left) != get<2>(right)) {
           return get<2>(left) <= get<2>(right);
         } else if ((get<0>(left) != get<0>(right))) {
           return get<0>(left) <= get<0>(right);
         } else {
           return get<1>(left) <= get<1>(right);
         }
       });

  vector<int> DD(Q, -1);
  for (auto l : L) {
    int s, t, x;
    tie(s, t, x) = l;

    auto it = D.lower_bound(make_tuple(s, -1));
    while (it != D.end()) {
      int d = get<0>(*it);
      int idx = get<1>(*it);

      if (d >= t) {
        break;
      }

      DD[idx] = x;
      D.erase(it++);
    }
  }

  // OUTPUT
  for (auto d : DD) {
    cout << d << endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  // VARIABLES
  int N, Q;
  vector<tuple<int, int, int>> L;
  set<tuple<int, int>> D;

  // INPUT
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    L.emplace_back(s - x, t - x, x);
  }

  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert(make_tuple(d, i));
  }

  // PROC
  sort(L.begin(), L.end(),
       [](const tuple<int, int, int> &left, const tuple<int, int, int> &right) {
         return get<2>(left) <= get<2>(right);
       });

  vector<int> DD(Q, -1);
  for (auto l : L) {
    int s, t, x;
    tie(s, t, x) = l;

    auto it = D.lower_bound(make_tuple(s, -1));
    while (it != D.end()) {
      int d = get<0>(*it);
      int idx = get<1>(*it);

      if (d >= t) {
        break;
      }

      DD[idx] = x;
      D.erase(it++);
    }
  }

  // OUTPUT
  for (auto d : DD) {
    cout << d << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  // VARIABLES
  int N, Q;
  vector<tuple<int, int, int>> L;
  set<tuple<int, int>> D;

  // INPUT
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    L.emplace_back(s - x, t - x, x);
  }

  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.insert(make_tuple(d, i));
  }

  // PROC
  sort(L.begin(), L.end(),
       [](tuple<int, int, int> left, tuple<int, int, int> right) {
         if (get<2>(left) != get<2>(right)) {
           return get<2>(left) <= get<2>(right);
         } else if ((get<0>(left) != get<0>(right))) {
           return get<0>(left) <= get<0>(right);
         } else {
           return get<1>(left) <= get<1>(right);
         }
       });

  vector<int> DD(Q, -1);
  for (auto l : L) {
    int s, t, x;
    tie(s, t, x) = l;

    auto it = D.lower_bound(make_tuple(s, -1));
    while (it != D.end()) {
      int d = get<0>(*it);
      int idx = get<1>(*it);

      if (d >= t) {
        break;
      }

      DD[idx] = x;
      D.erase(it++);
    }
  }

  // OUTPUT
  for (auto d : DD) {
    cout << d << endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

struct s_t {
  ll s, t, x;
};

bool asc(s_t left, s_t right) { return left.x < right.x; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<s_t> v;
  s_t z;
  for (ll i = 0; i < n; i++) {
    cin >> z.s >> z.t >> z.x;
    v.push_back(z);
  }
  sort(v.begin(), v.end(), asc);
  set<ll> st;
  vector<ll> vv(q, 0);
  for (ll i = 0; i < q; i++) {
    cin >> vv[i];
    st.insert(vv[i]);
  }
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    ll a = v[i].s - v[i].x;
    ll b = v[i].t - v[i].x;
    auto it1 = lower_bound(st.begin(), st.end(), a);
    auto it2 = lower_bound(st.begin(), st.end(), b);
    for (auto itr = it1; itr != it2;) {
      mp[*itr] = v[i].x + 1;
      itr = st.erase(itr);
    }
  }
  for (ll i = 0; i < q; i++) {
    ll ans = mp[vv[i]] - 1;
    cout << ans << endl;
  }
}


Fixed submission:
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

struct s_t {
  ll s, t, x;
};

bool asc(s_t left, s_t right) { return left.x < right.x; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<s_t> v;
  s_t z;
  for (ll i = 0; i < n; i++) {
    cin >> z.s >> z.t >> z.x;
    v.push_back(z);
  }
  sort(v.begin(), v.end(), asc);
  set<ll> st;
  vector<ll> vv(q, 0);
  for (ll i = 0; i < q; i++) {
    cin >> vv[i];
    st.insert(vv[i]);
  }
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    ll a = v[i].s - v[i].x;
    ll b = v[i].t - v[i].x;
    // auto it1=lower_bound(st.begin(),st.end(),a);
    // auto it2=lower_bound(st.begin(),st.end(),b);
    auto it1 = st.lower_bound(a);
    auto it2 = st.lower_bound(b);
    for (auto itr = it1; itr != it2;) {
      mp[*itr] = v[i].x + 1;
      itr = st.erase(itr);
    }
  }
  for (ll i = 0; i < q; i++) {
    ll ans = mp[vv[i]] - 1;
    cout << ans << endl;
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct event {
  int t;
  bool add;
  int pos;
};

bool event_sort(const event &e1, const event &e2) { return e1.t < e2.t; }

int main() {
  int N, Q;
  cin >> N >> Q;

  vector<int> S(N);
  vector<int> T(N);
  vector<int> X(N);
  vector<int> D(Q);
  for (int i = 0; i < N; i++)
    scanf("%d%d%d", &S[i], &T[i], &X[i]);
  for (int i = 0; i < Q; i++)
    scanf("%d", &D[i]);

  vector<event> E;
  for (int i = 0; i < N; i++) {
    event e;
    e.t = S[i] - X[i];
    e.add = true;
    e.pos = X[i];
    E.push_back(e);

    e.t = T[i] - X[i];
    e.add = false;
    E.push_back(e);
  }

  sort(E.begin(), E.end(), event_sort);
  set<int> s;

  int t = -1000000000;
  int q = 0;
  for (int i = 0; i < E.size(); i++) {
    auto e = E[i];
    t = e.t;
    while (t > D[q]) {
      if (s.empty())
        cout << "-1" << endl;
      else
        cout << *s.begin() << endl;
      q++;
      if (q == Q)
        return 0;
    }
    if (t <= D[q]) {
      if (e.add) {
        s.insert(e.pos);
      } else {
        auto it = s.find(e.pos);
        s.erase(it);
      }
    }
  }
  while (q < Q) {
    cout << "-1" << endl;
    q++;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct event {
  int t;
  bool add;
  int pos;
};

bool event_sort(const event &e1, const event &e2) {
  if (e1.t != e2.t)
    return e1.t < e2.t;
  else
    return !e1.add;
}

int main() {
  int N, Q;
  cin >> N >> Q;

  vector<int> S(N);
  vector<int> T(N);
  vector<int> X(N);
  vector<int> D(Q);
  for (int i = 0; i < N; i++)
    scanf("%d%d%d", &S[i], &T[i], &X[i]);
  for (int i = 0; i < Q; i++)
    scanf("%d", &D[i]);

  vector<event> E;
  for (int i = 0; i < N; i++) {
    event e;
    e.t = S[i] - X[i];
    e.add = true;
    e.pos = X[i];
    E.push_back(e);

    e.t = T[i] - X[i];
    e.add = false;
    E.push_back(e);
  }

  sort(E.begin(), E.end(), event_sort);
  set<int> s;

  int t = -1000000000;
  int q = 0;
  for (int i = 0; i < E.size(); i++) {
    auto e = E[i];
    t = e.t;
    while (t > D[q]) {
      if (s.empty())
        cout << "-1" << endl;
      else
        cout << *s.begin() << endl;
      q++;
      if (q == Q)
        return 0;
    }
    if (t <= D[q]) {
      if (e.add) {
        s.insert(e.pos);
      } else {
        auto it = s.find(e.pos);
        s.erase(it);
      }
    }
  }
  while (q < Q) {
    cout << "-1" << endl;
    q++;
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

// order statistics tree
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

const int N = 2e5 + 10;
int n, q;
vector<tuple<int, int, int>> events;
set<int> roadblocks;
// S_i - X_i - 0.5 <= D_i <= T_i -0.5 - X_i
int main() {
  cin.tie(0);
  cin.exceptions(cin.failbit);
  scanf("%d%d", &n, &q);
  rep(i, 1, n + 1) {
    int s, t, x;
    scanf("%d%d%d", &s, &t, &x);
    events.PB(make_tuple(s - x, 1, x)); // add roadblock
    events.PB(make_tuple(t - x, 0, x)); // remove roadblock
  }
  sort(events.begin(), events.end());
  int event_cnt = 0;
  rep(i, 1, q + 1) {
    int st;
    scanf("%d", &st);
    while (st >= get<0>(events[event_cnt])) {
      int event_type = get<1>(events[event_cnt]);
      int x = get<2>(events[event_cnt]);

      if (event_type)
        roadblocks.insert(x);
      else
        roadblocks.erase(x);

      event_cnt++;
    }

    if (!roadblocks.empty()) {
      printf("%d\n", *roadblocks.begin());
    } else {
      puts("-1");
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

// order statistics tree
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

const int N = 2e5 + 10;
int n, q;
vector<tuple<int, int, int>> events;
set<int> roadblocks;
// S_i - X_i - 0.5 <= D_i <= T_i -0.5 - X_i
int main() {
  cin.tie(0);
  cin.exceptions(cin.failbit);
  scanf("%d%d", &n, &q);
  rep(i, 1, n + 1) {
    int s, t, x;
    scanf("%d%d%d", &s, &t, &x);
    events.PB(make_tuple(s - x, 1, x)); // add roadblock
    events.PB(make_tuple(t - x, 0, x)); // remove roadblock
  }
  sort(events.begin(), events.end());
  int event_cnt = 0;
  rep(i, 1, q + 1) {
    int st;
    scanf("%d", &st);
    while (event_cnt < events.size() && st >= get<0>(events[event_cnt])) {
      int event_type = get<1>(events[event_cnt]);
      int x = get<2>(events[event_cnt]);

      if (event_type)
        roadblocks.insert(x);
      else
        roadblocks.erase(x);

      event_cnt++;
    }

    if (!roadblocks.empty()) {
      printf("%d\n", *roadblocks.begin());
    } else {
      puts("-1");
    }
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  vector<tuple<int, int, int>> a(n);
  map<int, int> d, f;

  rep(i, n) {
    int s, t, u;
    cin >> s >> t >> u;
    a[i] = make_tuple(u, s - u, t - u);
  }
  sort(a.begin(), a.end());

  rep(i, q) {
    int e;
    cin >> e;
    d.insert(make_pair(e, -1));
  }

  rep(i, n) {
    auto k = d.lower_bound(get<1>(a[i]));
    auto l = d.upper_bound(get<2>(a[i]) - 1);
    while (k != l) {
      f.insert(make_pair(k->first, get<0>(a[i])));
      d.erase(k);
    }
  }

  for (auto x : d) {
    f.insert(x);
  }
  for (auto x : f) {
    cout << x.second << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  vector<tuple<int, int, int>> a(n);
  map<int, int> d, f;

  rep(i, n) {
    int s, t, u;
    cin >> s >> t >> u;
    a[i] = make_tuple(u, s - u, t - u);
  }
  sort(a.begin(), a.end());

  rep(i, q) {
    int e;
    cin >> e;
    d.insert(make_pair(e, -1));
  }

  rep(i, n) {
    while (d.lower_bound(get<1>(a[i])) != d.upper_bound(get<2>(a[i]) - 1)) {
      f.insert(make_pair(d.lower_bound(get<1>(a[i]))->first, get<0>(a[i])));
      d.erase(d.lower_bound(get<1>(a[i])));
    }
  }

  for (auto x : d) {
    f.insert(x);
  }
  for (auto x : f) {
    cout << x.second << endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
/// TONI BO$$ was here
/// #MLC

using namespace std;

struct inter {
  int s, t, x;
} v[200001];
int d[200001];

int arbint[800001];

void update(int a, int b, int x, int st, int dr, int nod) {
  if (st >= a && dr <= b) {
    arbint[nod] = min(arbint[nod], x);
    return;
  }
  int mij = (st + dr) / 2;
  if (mij >= a)
    update(a, b, x, st, mij, 2 * nod);
  if (mij < b)
    update(a, b, x, mij + 1, dr, 2 * nod + 1);
}

int build(int i, int st, int dr, int nod) {
  arbint[nod] = min(arbint[nod], arbint[nod / 2]);
  if (st == i && dr == i) {
    return arbint[nod];
  }
  int mij = (st + dr) / 2;
  if (mij >= i)
    return build(i, st, mij, 2 * nod);
  else
    return build(i, mij + 1, dr, 2 * nod + 1);
}

int main() {
  int n, m, i, pas, p1, p2;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d%d%d", &v[i].s, &v[i].t, &v[i].x);
  for (i = 1; i <= m; i++)
    scanf("%d", &d[i]);
  for (i = 0; i <= 4 * m; i++)
    arbint[i] = INT_MAX;
  for (i = 1; i <= n; i++) {
    pas = 1 << 20;
    p1 = 0;
    while (pas) {
      if (p1 + pas <= m && v[i].s - v[i].x > d[p1 + pas])
        p1 += pas;
      pas /= 2;
    }
    p1++;
    p2 = 0;
    pas = 1 << 20;
    while (pas) {
      if (p2 + pas <= m && d[p2 + pas] < v[i].t - v[i].x)
        p2 += pas;
      pas /= 2;
    }
    if (p1 >= p2)
      update(p1, p2, v[i].x, 1, m, 1);
  }
  for (i = 1; i <= m; i++) {
    int z = build(i, 1, m, 1);
    if (z == INT_MAX)
      z = -1;
    printf("%d\n", z);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
/// TONI BO$$ was here
/// #MLC

using namespace std;

struct inter {
  int s, t, x;
} v[200001];
int d[200001];

int arbint[800001];

void update(int a, int b, int x, int st, int dr, int nod) {
  if (st >= a && dr <= b) {
    arbint[nod] = min(arbint[nod], x);
    return;
  }
  int mij = (st + dr) / 2;
  if (mij >= a)
    update(a, b, x, st, mij, 2 * nod);
  if (mij < b)
    update(a, b, x, mij + 1, dr, 2 * nod + 1);
}

int build(int i, int st, int dr, int nod) {
  arbint[nod] = min(arbint[nod], arbint[nod / 2]);
  if (st == i && dr == i) {
    return arbint[nod];
  }
  int mij = (st + dr) / 2;
  if (mij >= i)
    return build(i, st, mij, 2 * nod);
  else
    return build(i, mij + 1, dr, 2 * nod + 1);
}

int main() {
  int n, m, i, pas, p1, p2;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d%d%d", &v[i].s, &v[i].t, &v[i].x);
  for (i = 1; i <= m; i++)
    scanf("%d", &d[i]);
  for (i = 0; i <= 4 * m; i++)
    arbint[i] = INT_MAX;
  for (i = 1; i <= n; i++) {
    pas = 1 << 20;
    p1 = 0;
    while (pas) {
      if (p1 + pas <= m && v[i].s - v[i].x > d[p1 + pas])
        p1 += pas;
      pas /= 2;
    }
    p1++;
    p2 = 0;
    pas = 1 << 20;
    while (pas) {
      if (p2 + pas <= m && d[p2 + pas] < v[i].t - v[i].x)
        p2 += pas;
      pas /= 2;
    }
    if (p1 <= p2)
      update(p1, p2, v[i].x, 1, m, 1);
  }
  for (i = 1; i <= m; i++) {
    int z = build(i, 1, m, 1);
    if (z == INT_MAX)
      z = -1;
    printf("%d\n", z);
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, q, s, t, x, d, u;
vector<pair<int, pair<int, bool>>> pr;
set<int> st;
int main() {
  scanf("%d%d", &n, &q);

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &s, &t, &x);
    pr.push_back(make_pair(s - x, make_pair(x, true)));
    pr.push_back(make_pair(t - x, make_pair(x, false)));
  }
  sort(pr.begin(), pr.end());
  for (int i = 0; i < q; i++) {
    scanf("%d", &d);
    while (pr[u].first <= d) {
      if (pr[u].second.second)
        st.insert(pr[u].second.first);
      else
        st.erase(pr[u].second.first);
      u++;
    }
    printf("%d\n", st.empty() ? -1 : *st.begin());
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, q, s, t, x, d, u;
vector<pair<int, pair<int, bool>>> pr;
set<int> st;
int main() {
  scanf("%d%d", &n, &q);

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &s, &t, &x);
    pr.push_back(make_pair(s - x, make_pair(x, true)));
    pr.push_back(make_pair(t - x, make_pair(x, false)));
  }
  sort(pr.begin(), pr.end());
  for (int i = 0; i < q; i++) {
    scanf("%d", &d);
    while (u < pr.size() && pr[u].first <= d) {
      if (pr[u].second.second)
        st.insert(pr[u].second.first);
      else
        st.erase(pr[u].second.first);
      u++;
    }
    printf("%d\n", st.empty() ? -1 : *st.begin());
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second

using namespace std;

typedef long long ll;

const ll M = 4e18;

struct st {
  ll s, e, i;
};

bool operator<(st p, st q) { return p.i > q.i; }

bool cmp(st p, st q) { return p.s < q.s; }

ll n, Q;
st a[200005];
priority_queue<st> pq;

int main() {
  scanf("%lld%lld", &n, &Q);
  for (ll i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i].s, &a[i].e, &a[i].i);
    a[i].s -= a[i].i;
    a[i].e -= a[i].i;
  }
  sort(a, a + n, cmp);
  ll j = 0;
  pq.push({0, M, M});
  for (ll i = 0; i < Q; i++) {
    ll t;
    scanf("%lld", &t);
    for (; a[j].s <= t; j++) {
      pq.push(a[j]);
    }
    while (pq.top().e <= t)
      pq.pop();
    if (pq.top().i != M)
      printf("%lld\n", pq.top().i);
    else
      printf("-1\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second

using namespace std;

typedef long long ll;

const ll M = 4e18;

struct st {
  ll s, e, i;
};

bool operator<(st p, st q) { return p.i > q.i; }

bool cmp(st p, st q) { return p.s < q.s; }

ll n, Q;
st a[200005];
priority_queue<st> pq;

int main() {
  scanf("%lld%lld", &n, &Q);
  for (ll i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i].s, &a[i].e, &a[i].i);
    a[i].s -= a[i].i;
    a[i].e -= a[i].i;
  }
  sort(a, a + n, cmp);
  ll j = 0;
  pq.push({0, M, M});
  for (ll i = 0; i < Q; i++) {
    ll t;
    scanf("%lld", &t);
    for (; j < n && a[j].s <= t; j++) {
      pq.push(a[j]);
    }
    while (pq.top().e <= t)
      pq.pop();
    if (pq.top().i != M)
      printf("%lld\n", pq.top().i);
    else
      printf("-1\n");
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define N 200010
#define mid ((l + r) >> 1)
#define ls a[t].l
#define rs a[t].r
using namespace std;
struct dat {
  int c, l, r;
} e[N];
struct segt {
  dat a[N * 60];
  int cnt = 1;
  void psd(int t) {
    if (!ls)
      ls = ++cnt;
    if (!rs)
      rs = ++cnt;
    if (!a[t].c)
      return;
    a[ls].c = a[rs].c = a[t].c;
    a[t].c = 0;
  }
  void upd(int t, int l, int r, int x, int y, int k) {
    if (x <= l && r <= y) {
      a[t].c = k;
      return;
    }
    psd(t);
    if (x <= mid)
      upd(ls, l, mid, x, y, k);
    if (y > mid)
      upd(rs, mid + 1, r, x, y, k);
  }
  int qry(int t, int l, int r, int x) {
    while (l < r) {
      psd(t);
      if (x <= mid)
        t = ls, r = mid;
      else
        l = mid + 1, t = rs;
    }
    return a[t].c;
  }
} sgt;
bool cmp(dat a, dat b) { return a.c > b.c; }
int main() {
  int n, q, i;
  cin >> n >> q;
  sgt.upd(1, 0, 1e9, 0, 1e9, -1);
  for (i = 1; i <= n; i++)
    scanf("%d%d%d", &e[i].l, &e[i].r, &e[i].c), e[i].l -= e[i].c,
        e[i].r -= e[i].c + 1;
  sort(e + 1, e + n + 1, cmp);
  for (i = 1; i <= n; i++)
    sgt.upd(1, 0, 1e9, max(e[i].l, 0), max(e[i].r, 0), e[i].c);
  while (q--) {
    scanf("%d", &i);
    printf("%d\n", sgt.qry(1, 0, 1e9, i));
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 200010
#define mid ((l + r) >> 1)
#define ls a[t].l
#define rs a[t].r
using namespace std;
struct dat {
  int c, l, r;
} e[N];
struct segt {
  dat a[N * 80];
  int cnt = 1;
  void psd(int t) {
    if (!ls)
      ls = ++cnt;
    if (!rs)
      rs = ++cnt;
    if (!a[t].c)
      return;
    a[ls].c = a[rs].c = a[t].c;
    a[t].c = 0;
  }
  void upd(int t, int l, int r, int x, int y, int k) {
    if (x <= l && r <= y) {
      a[t].c = k;
      return;
    }
    psd(t);
    if (x <= mid)
      upd(ls, l, mid, x, y, k);
    if (y > mid)
      upd(rs, mid + 1, r, x, y, k);
  }
  int qry(int t, int l, int r, int x) {
    while (l < r) {
      psd(t);
      if (x <= mid)
        t = ls, r = mid;
      else
        l = mid + 1, t = rs;
    }
    return a[t].c;
  }
} sgt;
bool cmp(dat a, dat b) { return a.c > b.c; }
int main() {
  int n, q, i;
  cin >> n >> q;
  sgt.upd(1, 0, 1e9, 0, 1e9, -1);
  for (i = 1; i <= n; i++)
    scanf("%d%d%d", &e[i].l, &e[i].r, &e[i].c), e[i].l -= e[i].c,
        e[i].r -= e[i].c + 1;
  sort(e + 1, e + n + 1, cmp);
  for (i = 1; i <= n; i++)
    sgt.upd(1, 0, 1e9, max(e[i].l, 0), max(e[i].r, 0), e[i].c);
  while (q--) {
    scanf("%d", &i);
    printf("%d\n", sgt.qry(1, 0, 1e9, i));
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, q;
  int s, t, x;
  int d;
  cin >> n >> q;
  vector<pair<pair<int, int>, int>> p;
  for (int i = 0; i < n; ++i) {
    cin >> s >> t >> x;
    p.push_back({{s - x, 1}, x});
    p.push_back({{t - x, -1}, x});
  }
  vector<pair<int, int>> person;
  for (int i = 0; i < q; ++i) {
    cin >> d;
    person.push_back({d, i});
  }
  sort(person.begin(), person.end());
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  reverse(person.begin(), person.end());
  vector<int> ans(q, -1);
  set<int> S;
  while (!p.empty() && !person.empty()) {
    while (person.back().first < p.back().first.first) {
      if (!S.empty())
        ans[person.back().second] = *S.begin();
      person.pop_back();
    }
    int T = p.back().first.first;
    while (!p.empty() && p.back().first.first == T) {
      if (p.back().first.second == 1) {
        S.insert(p.back().second);
      } else {
        S.erase(p.back().second);
      }
      p.pop_back();
    }
  }
  while (!person.empty()) {
    if (!S.empty())
      ans[person.back().second] = *S.begin();
    person.pop_back();
  }
  for (int u : ans)
    cout << u << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, q;
  int s, t, x;
  int d;
  cin >> n >> q;
  vector<pair<pair<int, int>, int>> p;
  for (int i = 0; i < n; ++i) {
    cin >> s >> t >> x;
    p.push_back({{s - x, 1}, x});
    p.push_back({{t - x, -1}, x});
  }
  vector<pair<int, int>> person;
  for (int i = 0; i < q; ++i) {
    cin >> d;
    person.push_back({d, i});
  }
  sort(person.begin(), person.end());
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  reverse(person.begin(), person.end());
  vector<int> ans(q, -1);
  set<int> S;
  while (!p.empty() && !person.empty()) {
    while (!person.empty() && person.back().first < p.back().first.first) {
      if (!S.empty())
        ans[person.back().second] = *S.begin();
      person.pop_back();
    }
    int T = p.back().first.first;
    while (!p.empty() && p.back().first.first == T) {
      if (p.back().first.second == 1) {
        S.insert(p.back().second);
      } else {
        S.erase(p.back().second);
      }
      p.pop_back();
    }
  }
  while (!person.empty()) {
    if (!S.empty())
      ans[person.back().second] = *S.begin();
    person.pop_back();
  }
  for (int u : ans)
    cout << u << endl;
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<tuple<int, int, int>> v;
  vector<int> d(Q);
  for (int i = 0; i < N; i++) {
    int S, T, X;
    cin >> S >> T >> X;
    S -= X;
    T -= X;
    v.push_back(make_tuple(S, 1, X));
    v.push_back(make_tuple(T, 0, X));
  }
  for (int i = 0; i < Q; i++) {
    cin >> d[i];
  }
  sort(v.begin(), v.end());
  multiset<int> s;
  int last = -1000000007;
  int t = 0;
  for (int i = 0; i < Q; i++) {
    while (last < get<0>(v[t]) && get<0>(v[t]) <= d[i]) {
      if (get<1>(v[t]) == 1) {
        s.insert(get<2>(v[t]));
      } else {
        s.erase(get<2>(v[t]));
      }
      t++;
    }
    if (!s.empty()) {
      cout << *begin(s) << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<tuple<int, int, int>> v;
  vector<int> d(Q);
  for (int i = 0; i < N; i++) {
    int S, T, X;
    cin >> S >> T >> X;
    S -= X;
    T -= X;
    v.push_back(make_tuple(S, 1, X));
    v.push_back(make_tuple(T, 0, X));
  }
  for (int i = 0; i < Q; i++) {
    cin >> d[i];
  }
  sort(v.begin(), v.end());
  multiset<int> s;
  int last = -1000000007;
  int t = 0;
  for (int i = 0; i < Q; i++) {
    while (last < get<0>(v[t]) && get<0>(v[t]) <= d[i]) {
      if (get<1>(v[t]) == 1) {
        s.insert(get<2>(v[t]));
      } else {
        s.erase(get<2>(v[t]));
      }
      t++;
    }
    if (!s.empty()) {
      cout << *begin(s) << endl;
    } else {
      cout << -1 << endl;
    }
    last = d[i];
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {

  int N, Q, i, j;
  typedef pair<int, int> P;
  cin >> N >> Q;

  vector<int> X(N), S(N), T(N), D(Q);
  set<pair<int, int>> shugo;

  rep(i, N) { cin >> S[i] >> T[i] >> X[i]; }
  rep(i, Q) { cin >> D[i]; }
  rep(i, Q) { shugo.insert(P(D[i], i)); }

  vector<tuple<int, int, int>> a(N);
  rep(i, N) { a[i] = tie(X[i], S[i], T[i]); }
  sort(a.begin(), a.end());

  vector<int> ans;
  rep(i, Q) { ans[i] = -1; }
  rep(i, N) {
    int x, l, r;
    tie(x, l, r) = a[i];
    auto it = shugo.lower_bound(P(l - x, -1));
    while (it != shugo.end()) {
      if ((it->first) >= r - x) {
        break;
      }
      ans[it->second] = x;
      shugo.erase(it++);
    }
  }

  rep(i, Q) { cout << ans[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main() {

  int N, Q, i, j;
  typedef pair<int, int> P;
  cin >> N >> Q;

  vector<int> X(N), S(N), T(N), D(Q);
  set<pair<int, int>> shugo;

  rep(i, N) { cin >> S[i] >> T[i] >> X[i]; }
  rep(i, Q) { cin >> D[i]; }
  rep(i, Q) { shugo.insert(P(D[i], i)); }

  vector<tuple<int, int, int>> a(N);
  rep(i, N) { a[i] = tie(X[i], S[i], T[i]); }
  sort(a.begin(), a.end());

  vector<int> ans;
  rep(i, Q) { ans.push_back(-1); }
  rep(i, N) {
    int x, l, r;
    tie(x, l, r) = a[i];
    auto it = shugo.lower_bound(P(l - x, -1));
    while (it != shugo.end()) {
      if ((it->first) >= r - x) {
        break;
      }
      ans[it->second] = x;
      shugo.erase(it++);
    }
  }

  rep(i, Q) { cout << ans[i] << endl; }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define INF (1 << 30) - 1
#define LLINF (1LL << 61) - 1
// #define int ll
using namespace std;
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<tuple<ll, ll, ll>> L(n);
  rep(i, n) cin >> get<1>(L[i]) >> get<2>(L[i]) >> get<0>(L[i]);
  sort(all(L));
  vector<ll> ans(q, -1);
  ll D[2000100000];
  set<ll> s;
  rep(i, q) {
    ll d;
    cin >> d;
    s.insert(d);
    D[d] = i;
  }
  rep(i, n) {
    auto t = L[i];
    ll X = get<0>(t);
    ll S = get<1>(t);
    ll T = get<2>(t);
    auto it = s.lower_bound(S - X);
    while (it != s.end() && *it < T - X) {
      ans[D[*it]] = X;
      s.erase(it++);
    }
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define INF (1 << 30) - 1
#define LLINF (1LL << 61) - 1
// #define int ll
using namespace std;
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  vector<tuple<ll, ll, ll>> L(n);
  rep(i, n) cin >> get<1>(L[i]) >> get<2>(L[i]) >> get<0>(L[i]);
  sort(all(L));
  vector<ll> ans(q, -1);
  map<ll, ll> D;
  set<ll> s;
  rep(i, q) {
    ll d;
    cin >> d;
    s.insert(d);
    D[d] = i;
  }
  rep(i, n) {
    auto t = L[i];
    ll X = get<0>(t);
    ll S = get<1>(t);
    ll T = get<2>(t);
    auto it = s.lower_bound(S - X);
    while (it != s.end() && *it < T - X) {
      ans[D[*it]] = X;
      s.erase(it++);
    }
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  int n, q;
  cin >> n >> q;
  int s[n], t[n], x[n];
  rep(i, n) cin >> s[i] >> t[i] >> x[i];
  set<pair<int, int>> st;
  rep(i, q) {
    int a;
    cin >> a;
    st.insert({a, i});
  }
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int u, int v) { return x[u] < x[v]; });
  vector<int> ans(q, -1);
  for (auto i : ord) {
    auto itr = st.lower_bound({-x[i] + s[i], -1});
    while (itr->first < -x[i] + t[i]) {
      ans[itr->second] = x[i];
      itr = st.erase(itr);
    }
  }
  for (auto e : ans)
    cout << e << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
  int n, q;
  cin >> n >> q;
  int s[n], t[n], x[n];
  rep(i, n) cin >> s[i] >> t[i] >> x[i];
  set<pair<int, int>> st;
  rep(i, q) {
    int a;
    cin >> a;
    st.insert({a, i});
  }
  st.insert({inf, -1});
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int u, int v) { return x[u] < x[v]; });
  vector<int> ans(q, -1);
  for (auto i : ord) {
    auto itr = st.lower_bound({-x[i] + s[i], -1});
    while (itr->first < -x[i] + t[i]) {
      ans[itr->second] = x[i];
      itr = st.erase(itr);
    }
  }
  for (auto e : ans)
    cout << e << endl;
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<pair<int, pair<int, int>>> a;
multiset<int> s;
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    int s, t, x;
    scanf("%d%d%d", &s, &t, &x);
    a.push_back(make_pair(s - x, make_pair(1, x)));
    a.push_back(make_pair(t - x, make_pair(-1, x)));
  }
  sort(a.begin(), a.end());
  int i = 0;
  while (q--) {
    int d;
    scanf("%d", &d);
    while (a[i].first <= d) {
      if (a[i].second.first == 1)
        s.insert(a[i].second.second);
      else
        s.erase(a[i].second.second);
      i++;
    }
    if (s.size())
      printf("%d\n", *s.begin());
    else
      printf("-1\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<pair<int, pair<int, int>>> a;
multiset<int> s;
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    int s, t, x;
    scanf("%d%d%d", &s, &t, &x);
    a.push_back(make_pair(s - x, make_pair(1, x)));
    a.push_back(make_pair(t - x, make_pair(-1, x)));
  }
  sort(a.begin(), a.end());
  int i = 0;
  while (q--) {
    int d;
    scanf("%d", &d);
    while (a[i].first <= d && i < a.size()) {
      if (a[i].second.first == 1)
        s.insert(a[i].second.second);
      else
        s.erase(a[i].second.second);
      i++;
    }
    if (s.size())
      printf("%d\n", *s.begin());
    else
      printf("-1\n");
  }
  return 0;
}