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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1e9 + 7;
using P = pair<int, int>;
int main() {
  int n, q;
  cin >> n >> q;
  multimap<int, P> m;
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    m.insert(make_pair(x, P(s, t)));
  }
  map<int, int> ans; // <time, distance>
  set<int> tm;
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    ans[d] = -1;
    tm.insert(d);
  }

  for (auto ite = m.begin(); ite != m.end(); ite++) {
    int x = ite->first;
    P p = ite->second;
    int s = p.first;
    int t = p.second;
    auto sit = tm.lower_bound(s - x);
    while (*sit < t - x) {
      ans[*sit] = x;
      sit = tm.erase(sit);
    }
  }
  for (auto ite = ans.begin(); ite != ans.end(); ite++) {
    cout << ite->second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1e9 + 7;
using P = pair<int, int>;
int main() {
  int n, q;
  cin >> n >> q;
  multimap<int, P> m;
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    m.insert(make_pair(x, P(s, t)));
  }
  map<int, int> ans; // <time, distance>
  set<int> tm;
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    ans[d] = -1;
    tm.insert(d);
  }

  for (auto ite = m.begin(); ite != m.end(); ite++) {
    int x = ite->first;
    P p = ite->second;
    int s = p.first;
    int t = p.second;
    auto sit = tm.lower_bound(s - x);
    while (sit != tm.end() && *sit < t - x) {
      ans[*sit] = x;
      sit = tm.erase(sit);
    }
  }
  for (auto ite = ans.begin(); ite != ans.end(); ite++) {
    cout << ite->second << endl;
  }
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
  ll n, q;
  cin >> n >> q;
  vector<pair<ll, ll>> v;
  rep(i, n) {
    ll s, t, x;
    cin >> s >> t >> x;
    s -= x;
    if (s < 0)
      s = 0;
    t -= x;
    if (t < 0)
      t = 0;
    v.push_back({s, x});
    v.push_back({t, -x});
  }
  sort(v.begin(), v.end());
  vector<ll> ans(q);
  multiset<ll> s;
  ll itr = 0;
  rep(i, q) {
    ll d;
    cin >> d;
    while (itr < v.size() && d >= v[itr].first) {
      if (v[itr].second > 0)
        s.insert(v[itr].second);
      else
        s.erase(s.find(-1 * v[itr].second));
      itr++;
    }
    if (s.size() > 0)
      ans[i] = *s.begin();
    else
      ans[i] = -1;
  }

  for (auto an : ans)
    cout << an << endl;
  return 0;
}

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

int main() {
  ll n, q;
  cin >> n >> q;
  vector<pair<ll, ll>> v;
  rep(i, n) {
    ll s, t, x;
    cin >> s >> t >> x;
    s -= x;
    if (s < 0)
      s = 0;
    t -= x;
    if (t < 0)
      t = 0;
    if (t > 0) {
      v.push_back({s, x});
      v.push_back({t, -x});
    }
  }
  sort(v.begin(), v.end());
  vector<ll> ans(q);
  multiset<ll> s;
  ll itr = 0;
  rep(i, q) {
    ll d;
    cin >> d;
    while (itr < v.size() && d >= v[itr].first) {
      if (v[itr].second > 0)
        s.insert(v[itr].second);
      else
        s.erase(s.find(-1 * v[itr].second));
      itr++;
    }
    if (s.size() > 0)
      ans[i] = *s.begin();
    else
      ans[i] = -1;
  }

  for (auto an : ans)
    cout << an << endl;
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
int main() {
  int n, q;
  cin >> n >> q;
  vector<PP> p(n);
  rep(i, n) {
    int s, t, x;
    scanf("%d %d %d", &s, &t, &x);
    get<2>(p[i]) = x - t + 1;
    get<1>(p[i]) = x - s;
    get<0>(p[i]) = x;
  }
  sort(p.begin(), p.end());
  set<P> s;
  vi ans(q, -1);
  rep(i, q) {
    int a;
    scanf("%d", &a);
    s.insert(P(-a, i));
  }
  rep(i, n) {
    if (s.size() == 0)
      break;
    int a = get<2>(p[i]), b = get<1>(p[i]);
    auto from = lower_bound(s.begin(), s.end(), P(a, -1));
    while (from != s.end()) {
      if ((from->first) > get<1>(p[i]))
        break;
      ans[from->second] = get<0>(p[i]);
      s.erase(from++);
    }
  }
  rep(i, q) printf("%d\n", ans[i]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
int main() {
  int n, q;
  cin >> n >> q;
  vector<PP> p(n);
  rep(i, n) {
    int s, t, x;
    scanf("%d %d %d", &s, &t, &x);
    get<2>(p[i]) = x - t + 1;
    get<1>(p[i]) = x - s;
    get<0>(p[i]) = x;
  }
  sort(p.begin(), p.end());
  set<P> s;
  vi ans(q, -1);
  rep(i, q) {
    int a;
    scanf("%d", &a);
    s.insert(P(-a, i));
  }
  rep(i, n) {
    if (s.size() == 0)
      break;
    int a = get<2>(p[i]), b = get<1>(p[i]);
    auto from = s.lower_bound(P(a, -1));
    while (from != s.end()) {
      if ((from->first) > get<1>(p[i]))
        break;
      ans[from->second] = get<0>(p[i]);
      s.erase(from++);
    }
  }
  rep(i, q) printf("%d\n", ans[i]);
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <tuple>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n, q, c = (int)1e9 + 7;
  cin >> n >> q;
  tuple<int, int> a[2 * n + q];
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    a[2 * i] = tuple<int, int>(s - x, x);
    a[2 * i + 1] = tuple<int, int>(t - x, x);
  }
  rep(i, q) {
    int d;
    cin >> d;
    a[2 * n + i] = tuple<int, int>(d, c);
  }

  sort(a, a + 2 * n + q);

  int r[q], j = 0;
  set<int> s = {c};
  rep(i, 2 * n + q) {
    int k = get<1>(a[i]);
    if (k == c) {
      r[j] = *min_element(s.begin(), s.end());
      if (r[j] == c)
        r[j] = -1;
      j++;
    } else {
      if (s.count(k))
        s.erase(k);
      else
        s.insert(k);
    }
  }

  rep(i, q) cout << r[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <tuple>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n, q, c = (int)1e9 + 7;
  cin >> n >> q;
  tuple<int, int> a[2 * n + q];
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    a[2 * i] = tuple<int, int>(s - x, x);
    a[2 * i + 1] = tuple<int, int>(t - x, x);
  }
  rep(i, q) {
    int d;
    cin >> d;
    a[2 * n + i] = tuple<int, int>(d, c);
  }

  sort(a, a + 2 * n + q);

  int r[q], j = 0;
  set<int> s = {c};
  rep(i, 2 * n + q) {
    int k = get<1>(a[i]);
    if (k == c) {
      r[j] = *s.begin();
      if (r[j] == c)
        r[j] = -1;
      j++;
    } else {
      if (s.count(k))
        s.erase(k);
      else
        s.insert(k);
    }
  }

  rep(i, q) cout << r[i] << endl;
  return 0;
}
Problem p03033 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, Q;
  cin >> N >> Q;
  int Si, Ti, Xi, Di;
  vector<array<int, 3>> stx(N);
  for (int i = 0; i < N; i++) {
    cin >> Si >> Ti >> Xi;
    auto l = Si - Xi;
    auto r = Ti - Xi;
    if (l < 0)
      l = 0;
    stx[i] = array<int, 3>{l, r, Xi};
  }
  sort(stx.begin(), stx.end(),
       [](array<int, 3> &a, array<int, 3> &b) { return a[2] < b[2]; });
  set<int> D;
  map<int, int> res;
  for (int i = 0; i < Q; i++) {
    cin >> Di;
    D.insert(Di);
  }

  for (auto v : stx) {
    auto itrl = lower_bound(D.begin(), D.end(), v[0]);
    auto itrr = upper_bound(D.begin(), D.end(), v[1] - 1);
    for (auto itr = itrl; itr != itrr; ++itr) {
      res[*itr] = v[2];
    }
    D.erase(itrl, itrr);
  }
  for (auto v : D) {
    res[v] = -1;
  }
  for (auto m : res) {
    cout << m.second << endl;
  }

  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, Q;
  cin >> N >> Q;
  int Si, Ti, Xi, Di;
  vector<array<int, 3>> stx(N);
  for (int i = 0; i < N; i++) {
    cin >> Si >> Ti >> Xi;
    auto l = Si - Xi;
    auto r = Ti - Xi;
    if (l < 0)
      l = 0;
    stx[i] = array<int, 3>{l, r, Xi};
  }
  sort(stx.begin(), stx.end(),
       [](array<int, 3> &a, array<int, 3> &b) { return a[2] < b[2]; });
  set<int> D;
  map<int, int> res;
  for (int i = 0; i < Q; i++) {
    cin >> Di;
    D.insert(Di);
  }

  for (auto v : stx) {
    auto itrl = D.lower_bound(v[0]);
    auto itrr = D.upper_bound(v[1] - 1);
    for (auto itr = itrl; itr != itrr; ++itr) {
      res[*itr] = v[2];
    }
    D.erase(itrl, itrr);
  }
  for (auto v : D) {
    res[v] = -1;
  }
  for (auto m : res) {
    cout << m.second << endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, pair<int, int>>> v(n);
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    v[i] = {x, {s, t}};
  }
  sort(v.begin(), v.end());
  set<pair<int, int>> d;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    d.insert({t, i});
  }
  int ans[q];
  memset(ans, -1, sizeof(ans));
  for (int i = 0; i < n; i++) {
    if (d.empty())
      break;
    int x = v[i].first;
    int s = v[i].second.first;
    int t = v[i].second.second;
    auto it = d.lower_bound({s - x, -1});
    if (it == d.end())
      continue;
    while ((*it).first < t - x) {
      ans[(*it).second] = x;
      d.erase(it);
      if (d.empty())
        break;
      it = d.lower_bound({s - x, -1});
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<int, pair<int, int>>> v(n);
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    v[i] = {x, {s, t}};
  }
  sort(v.begin(), v.end());
  set<pair<int, int>> d;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    d.insert({t, i});
  }
  int ans[q];
  memset(ans, -1, sizeof(ans));
  for (int i = 0; i < n; i++) {
    if (d.empty())
      break;
    int x = v[i].first;
    int s = v[i].second.first;
    int t = v[i].second.second;
    auto it = d.lower_bound({s - x, -1});
    if (it == d.end())
      continue;
    while ((*it).first < t - x) {
      ans[(*it).second] = x;
      d.erase(it);
      if (d.empty())
        break;
      it = d.lower_bound({s - x, -1});
      if (it == d.end())
        break;
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
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++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using ll = long long;
using Grid = vector<vector<int>>;
using vi = vector<int>;

int main() {
  int N, Q;
  cin >> N >> Q;
  list<tuple<int, int, int>> eve;
  rep(i, N) {
    int s, t, x;
    cin >> s >> t >> x;
    eve.emplace_back(s - x, 1, x);
    eve.emplace_back(t - x, -1, x);
  }
  eve.sort();
  multiset<int> ms;
  rep(i, Q) {
    int p;
    cin >> p;
    while (get<0>(eve.front()) <= p) {
      if (get<1>(eve.front()) == 1)
        ms.insert(get<2>(eve.front()));
      else
        ms.erase(get<2>(eve.front()));
      eve.pop_front();
    }
    cout << (ms.size() == 0 ? -1 : *(ms.begin())) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using ll = long long;
using Grid = vector<vector<int>>;
using vi = vector<int>;

int main() {
  int N, Q;
  cin >> N >> Q;
  list<tuple<int, int, int>> eve;
  rep(i, N) {
    int s, t, x;
    cin >> s >> t >> x;
    eve.emplace_back(s - x, 1, x);
    eve.emplace_back(t - x, -1, x);
  }
  eve.sort();
  multiset<int> ms;
  rep(i, Q) {
    int p;
    cin >> p;
    while (eve.size() > 0 && get<0>(eve.front()) <= p) {
      if (get<1>(eve.front()) == 1)
        ms.insert(get<2>(eve.front()));
      else
        ms.erase(get<2>(eve.front()));
      eve.pop_front();
    }
    cout << (ms.size() == 0 ? -1 : *(ms.begin())) << endl;
  }
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
priority_queue<P, vector<P>, greater<P>> p;
priority_queue<P, vector<P>, greater<P>> pq;

int main() {
  int n, q;
  int t[100009], x[100009];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s >> t[i] >> x[i];
    t[i] -= x[i];
    p.push(P(s - x[i], i));
  }
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    while (!p.empty()) {
      P now = p.top();
      int s = now.first;
      int idx = now.second;
      if (s > d)
        break;
      pq.push(P(x[idx], idx));
      p.pop();
    }
    while (!pq.empty()) {
      P now = pq.top();
      int idx = now.second;
      if (t[idx] > d) {
        cout << x[idx] << endl;
        break;
      }
      pq.pop();
    }
    if (pq.empty())
      cout << -1 << endl;
  }
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
priority_queue<P, vector<P>, greater<P>> p;
priority_queue<P, vector<P>, greater<P>> pq;

int main() {
  int n, q;
  int t[200009], x[200009];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s >> t[i] >> x[i];
    t[i] -= x[i];
    p.push(P(s - x[i], i));
  }
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    while (!p.empty()) {
      P now = p.top();
      int s = now.first;
      int idx = now.second;
      if (s > d)
        break;
      pq.push(P(x[idx], idx));
      p.pop();
    }
    while (!pq.empty()) {
      P now = pq.top();
      int idx = now.second;
      if (t[idx] > d) {
        cout << x[idx] << endl;
        break;
      }
      pq.pop();
    }
    if (pq.empty())
      cout << -1 << endl;
  }
  return (0);
}
Problem p03033 (Python)

Buggy submission (status: Time Limit Exceeded):
from heapq import heappush, heappop

n, q = map(int, input().split())
STX = [list(map(int, input().split())) for _ in range(n)]
task = []
for s, t, x in STX:
    task.append((t - x, 0, x))
    task.append((s - x, 1, x))
for i in range(q):
    d = int(input())
    task.append((d, 2, i))

task.sort()
kouho = set()
kouho_hp = []
ans = [-1] * q
for a, b, c in task:
    if b == 0:
        kouho.remove(c)
    if b == 1:
        kouho.add(c)
        heappush(kouho_hp, c)
    if b == 2:
        while kouho_hp and kouho_hp[0] not in kouho:
            heappop(kouho_hp)
        if not kouho_hp:
            continue
        else:
            ans[c] = kouho_hp[0]
        # heapは0-indexが最小値になっているからheapified_list[0]でもよい
print(*ans, sep="\n")


Fixed submission:
from heapq import heappush, heappop
import sys

input = sys.stdin.buffer.readline


n, q = map(int, input().split())
STX = [list(map(int, input().split())) for _ in range(n)]
task = []
for s, t, x in STX:
    task.append((t - x, 0, x))
    task.append((s - x, 1, x))
for i in range(q):
    d = int(input())
    task.append((d, 2, i))

task.sort()
kouho = set()
kouho_hp = []
ans = [-1] * q
for a, b, c in task:
    if b == 0:
        kouho.remove(c)
    if b == 1:
        kouho.add(c)
        heappush(kouho_hp, c)
    if b == 2:
        while kouho_hp and kouho_hp[0] not in kouho:
            heappop(kouho_hp)
        if not kouho_hp:
            continue
        else:
            ans[c] = kouho_hp[0]
        # heapは0-indexが最小値になっているからheapified_list[0]でもよい
print(*ans, sep="\n")
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 200005

struct LOC {

  bool operator<(const struct LOC &arg) const { return X < arg.X; }
  ll S, T, X;
};

ll N, Q;
ll ans[SIZE];
map<ll, ll> MAP;
set<ll> SET;
LOC loc[SIZE];

int main() {

  scanf("%lld %lld", &N, &Q);

  for (int i = 0; i < Q; i++) {

    ans[i] = -1;
  }

  for (ll i = 0; i < N; i++) {

    scanf("%lld %lld %lld", &loc[i].S, &loc[i].T, &loc[i].X);
  }
  sort(loc, loc + N);

  ll D;
  for (ll i = 0; i < Q; i++) {

    scanf("%lld", &D);
    MAP[D] = i;
    SET.insert(D);
  }

  vector<ll> work;

  for (int i = 0; i < N; i++) {

    // printf("%lld-%lld\n",max(0LL,loc[i].S-loc[i].X),max(0LL,(loc[i].T-1)-loc[i].X));

    auto left = SET.lower_bound(max(0LL, loc[i].S - loc[i].X));
    auto right = SET.lower_bound(max(0LL, loc[i].T - loc[i].X));

    if (left == SET.end())
      continue;

    work.clear();

    while (true) {

      ans[MAP[*left]] = loc[i].X;
      work.push_back(*left);
      left++;
      if (left == right)
        break;
    }

    for (int k = 0; k < work.size(); k++) {

      SET.erase(work[k]);
    }
  }

  for (int i = 0; i < Q; i++) {

    printf("%lld\n", ans[i]);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 200005

struct LOC {

  bool operator<(const struct LOC &arg) const { return X < arg.X; }
  ll S, T, X;
};

ll N, Q;
ll ans[SIZE];
map<ll, ll> MAP;
set<ll> SET;
LOC loc[SIZE];

int main() {

  scanf("%lld %lld", &N, &Q);

  for (int i = 0; i < Q; i++) {

    ans[i] = -1;
  }

  for (ll i = 0; i < N; i++) {

    scanf("%lld %lld %lld", &loc[i].S, &loc[i].T, &loc[i].X);
  }
  sort(loc, loc + N);

  ll D;
  for (ll i = 0; i < Q; i++) {

    scanf("%lld", &D);
    MAP[D] = i;
    SET.insert(D);
  }

  vector<ll> work;

  for (int i = 0; i < N; i++) {

    // printf("%lld-%lld\n",max(0LL,loc[i].S-loc[i].X),max(0LL,(loc[i].T-1)-loc[i].X));

    auto left = SET.lower_bound(max(0LL, loc[i].S - loc[i].X));

    if (*left + loc[i].X >= loc[i].T)
      continue;

    auto right = SET.lower_bound(max(0LL, loc[i].T - loc[i].X));

    if (left == SET.end())
      continue;

    work.clear();

    while (true) {

      ans[MAP[*left]] = loc[i].X;
      work.push_back(*left);
      left++;
      if (left == right)
        break;
    }

    for (int k = 0; k < work.size(); k++) {

      SET.erase(work[k]);
    }
  }

  for (int i = 0; i < Q; i++) {

    printf("%lld\n", ans[i]);
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
// Author: Shusuke Ueda
// AtCoder Beginner Contest 128 - Problem E

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>

struct Con {
  int s, t, x;
};

int main() {
  int n, q;
  std::cin >> n >> q;
  std::vector<Con> c(n);
  for (auto &e : c) {
    int s, t, x;
    std::cin >> s >> t >> x;
    s -= x;
    t -= x;
    e = {s, t, x};
  }
  std::set<int> d;
  std::map<int, int> ans_corr;
  for (int i = 0; i < q; i++) {
    int di;
    std::cin >> di;
    d.insert(di);
    ans_corr[di] = i;
  }

  std::vector<int> ans(q, -1);
  std::sort(c.begin(), c.end(),
            [](const Con a, const Con b) { return a.x < b.x; });
  for (auto e : c) {
    auto it = d.lower_bound(e.s);
    while (*it < e.t) {
      ans[ans_corr[*it]] = e.x;
      it = d.erase(it);
    }
  }

  for (auto e : ans) {
    std::cout << e << std::endl;
  }

  return 0;
}

Fixed submission:
// Author: Shusuke Ueda
// AtCoder Beginner Contest 128 - Problem E

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>

struct Con {
  int s, t, x;
};

int main() {
  int n, q;
  std::cin >> n >> q;
  std::vector<Con> c(n);
  for (auto &e : c) {
    int s, t, x;
    std::cin >> s >> t >> x;
    s -= x;
    t -= x;
    e = {s, t, x};
  }
  std::set<int> d;
  std::map<int, int> ans_corr;
  for (int i = 0; i < q; i++) {
    int di;
    std::cin >> di;
    d.insert(di);
    ans_corr[di] = i;
  }

  std::vector<int> ans(q, -1);
  std::sort(c.begin(), c.end(),
            [](const Con a, const Con b) { return a.x < b.x; });
  for (auto e : c) {
    auto it = d.lower_bound(e.s);
    while (it != d.end() and *it < e.t) {
      ans[ans_corr[*it]] = e.x;
      it = d.erase(it);
    }
  }

  for (auto e : ans) {
    std::cout << e << std::endl;
  }

  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

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

  ll N, Q;
  cin >> N >> Q;
  vector<tuple<ll, ll, ll>> roadwork;
  Rep(i, N) {
    ll s, t, x;
    cin >> s >> t >> x;
    roadwork.push_back(make_tuple(s - x, 1, x));
    roadwork.push_back(make_tuple(t - x, -1, x));
  }
  vector<ll> D(Q);
  Rep(i, Q) { cin >> D[i]; }

  sort(roadwork.begin(), roadwork.end());
  set<ll> stop;
  ll now = 0;
  vector<ll> dist(Q, -1);
  for (auto e : roadwork) {
    ll time = get<0>(e);
    ll kind = get<1>(e);
    ll place = get<2>(e);

    while (now < N && time > D[now]) {
      if (!stop.empty()) {
        dist[now] = *stop.begin();
      }
      now++;
    }

    if (kind == 1) {
      stop.insert(place);
    } else {
      stop.erase(place);
    }
  }

  for (auto e : dist) {
    cout << e << "\n";
  }

  cout << flush;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

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

  ll N, Q;
  cin >> N >> Q;
  vector<tuple<ll, ll, ll>> roadwork;
  Rep(i, N) {
    ll s, t, x;
    cin >> s >> t >> x;
    roadwork.push_back(make_tuple(s - x, 1, x));
    roadwork.push_back(make_tuple(t - x, -1, x));
  }
  vector<ll> D(Q);
  Rep(i, Q) { cin >> D[i]; }

  sort(roadwork.begin(), roadwork.end());
  set<ll> stop;
  ll now = 0;
  vector<ll> dist(Q, -1);
  for (auto e : roadwork) {
    ll time = get<0>(e);
    ll kind = get<1>(e);
    ll place = get<2>(e);

    while (now < Q && time > D[now]) {
      if (!stop.empty()) {
        dist[now] = *stop.begin();
      }
      now++;
    }

    if (kind == 1) {
      stop.insert(place);
    } else {
      stop.erase(place);
    }
  }

  for (auto e : dist) {
    cout << e << "\n";
  }

  cout << flush;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n >> q;
  vector<P> e;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    s -= x;
    t -= x;
    if (t <= 0)
      continue;
    else if (s < 0)
      s = 0;
    e.push_back(make_pair(s, x));
    e.push_back(make_pair(t, -x));
  }
  sort(e.begin(), e.end());

  vector<int> qu(n);
  rep(i, q) cin >> qu[i];
  vector<int> ans(q, -1);

  set<int> dist;
  int idx = 0;
  for (P p : e) {
    int time = p.first;
    int x = p.second;
    if (idx < q) {
      while (qu[idx] < time) {
        if (dist.empty())
          ans[idx] = -1;
        else
          ans[idx] = *dist.lower_bound(-1);
        idx++;
        if (idx == q)
          break;
      }
    }

    if (x > 0) {
      dist.insert(x);
    } else {
      dist.erase(-x);
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n >> q;
  vector<P> e;
  rep(i, n) {
    int s, t, x;
    cin >> s >> t >> x;
    s -= x;
    t -= x;
    if (t <= 0)
      continue;
    else if (s < 0)
      s = 0;
    e.push_back(make_pair(s, x));
    e.push_back(make_pair(t, -x));
  }
  sort(e.begin(), e.end());

  vector<int> qu(q);
  rep(i, q) cin >> qu[i];
  vector<int> ans(q, -1);

  set<int> dist;
  int idx = 0;
  for (P p : e) {
    int time = p.first;
    int x = p.second;
    if (idx < q) {
      while (qu[idx] < time) {
        if (dist.empty())
          ans[idx] = -1;
        else
          ans[idx] = *dist.lower_bound(-1);
        idx++;
        if (idx == q)
          break;
      }
    }

    if (x > 0) {
      dist.insert(x);
    } else {
      dist.erase(-x);
    }
  }

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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

int N, Q;
int S[212345], T[212345], X[212345], D[212345];

int main() {

  scanf("%d%d", &N, &Q);
  rep(i, N) scanf("%d%d%d", &S[i], &T[i], &X[i]);
  rep(i, Q) scanf("%d", &D[i]);

  vector<vector<int>> d(Q);

  rep(i, N) {
    d[distance(D, lower_bound(D, D + Q, S[i] - X[i]))].push_back(X[i]);
    d[distance(D, lower_bound(D, D + Q, T[i] - X[i]))].push_back(-X[i]);
  }

  set<int> s;
  rep(q, Q) {
    for (auto dd : d[q]) {
      if (dd > 0)
        s.insert(dd);
      if (dd < 0) {
        if (s.find(-dd) != s.end())
          s.erase(s.find(-dd));
      }
    }
    if (s.size())
      printf("%d\n", *s.begin());
    else
      printf("-1\n");
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)

int N, Q;
int S[212345], T[212345], X[212345], D[212345];

int main() {

  scanf("%d%d", &N, &Q);
  rep(i, N) scanf("%d%d%d", &S[i], &T[i], &X[i]);
  rep(i, Q) scanf("%d", &D[i]);

  vector<vector<int>> d(Q + 1);

  vector<pair<int, pair<int, int>>> sorter;
  rep(i, N) sorter.push_back({X[i], {S[i], T[i]}});

  sort(sorter.begin(), sorter.end());
  rep(i, N) {
    X[i] = sorter[i].first;
    S[i] = sorter[i].second.first;
    T[i] = sorter[i].second.second;
  }

  rep(i, N) {
    d[distance(D, lower_bound(D, D + Q, S[i] - X[i]))].push_back(X[i]);
    d[distance(D, lower_bound(D, D + Q, T[i] - X[i]))].push_back(-X[i]);
  }

  set<int> s;
  rep(q, Q) {
    for (auto dd : d[q]) {
      if (dd > 0)
        s.insert(dd);
      if (dd < 0) {
        if (s.find(-dd) != s.end())
          s.erase(s.find(-dd));
      }
    }
    if (s.size())
      printf("%d\n", *s.begin());
    else
      printf("-1\n");
  }

  return 0;
}
Problem p03033 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/abc128/submissions/8312583
# 写経


def solve():
    from heapq import heappush, heappop
    from collections import defaultdict
    from operator import itemgetter
    import sys

    input = sys.stdin.readline

    inf = 1 << 31

    n, q = map(int, input().split())

    S = [0] * n
    T = [0] * n
    X = [0] * n
    for i in range(n):
        S[i], T[i], X[i] = map(int, input().split())

    e = [
        *((t - x, 0, x) for t, x in zip(T, X)),
        *((s - x, 1, x) for s, x in zip(S, X)),
        *((int(input()), 2, idx) for idx in range(q)),
    ]
    e.sort(key=itemgetter(0))
    # 安定ソートなので、0,1,2順になる（はず

    cnt = defaultdict(int)
    cnt[inf] = 1
    h = [inf]
    ret = [-1] * q
    for time, type, x in e:
        if type == 0:
            cnt[x] -= 1
        elif type == 1:
            heappush(h, x)
            cnt[x] += 1
        else:
            while not (cnt[h[0]]):
                heappop(h)
            curr = h[0]
            if curr != inf:
                ret[x] = curr

    print(*ret, sep="\n")


if __name__ == "__main__":
    solve()


Fixed submission:
# https://atcoder.jp/contests/abc128/submissions/8312583
# 写経


def solve():
    from heapq import heappush, heappop
    from collections import defaultdict
    from operator import itemgetter
    import sys

    input = sys.stdin.readline

    inf = 1 << 31

    n, q = map(int, input().split())

    S = [0] * n
    T = [0] * n
    X = [0] * n
    for i in range(n):
        S[i], T[i], X[i] = map(int, input().split())

    e = [(t - x, 0, x) for t, x in zip(T, X)]
    e.extend([(s - x, 1, x) for s, x in zip(S, X)])
    e.extend([(int(input()), 2, idx) for idx in range(q)])
    # e = [*((t - x, 0, x) for t, x in zip(T, X)),
    #      *((s - x, 1, x) for s, x in zip(S, X)),
    #      *((int(input()), 2, idx) for idx in range(q))
    #      ]
    e.sort(key=itemgetter(0))
    # 安定ソートなので、0,1,2順になる（はず

    cnt = defaultdict(int)
    cnt[inf] = 1
    h = [inf]
    ret = [-1] * q
    for time, type, x in e:
        if type == 0:
            cnt[x] -= 1
        elif type == 1:
            heappush(h, x)
            cnt[x] += 1
        else:
            while not (cnt[h[0]]):
                heappop(h)
            curr = h[0]
            if curr != inf:
                ret[x] = curr

    print(*ret, sep="\n")


if __name__ == "__main__":
    solve()
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define maxn 200005
#define inf 1e9
#define eps 1e-10
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

int n, q, d[maxn], nxt[maxn], ans[maxn], pre[maxn];
struct node {
  int s, t, x;
} p[maxn];
struct node2 {
  int x, id;
};
set<int> s, tmp;
set<int>::iterator it1, it2;
map<int, int> mp;
vector<int> vec;

inline bool cmp(node a, node b) { return a.x < b.x; }

int main() {
  freopen("t1.in", "r", stdin);
  n = read();
  q = read();
  for (int i = 1; i <= n; i++)
    p[i].s = read(), p[i].t = read() - 1, p[i].x = read();
  sort(p + 1, p + n + 1, cmp);
  for (int i = 1; i <= q; i++)
    d[i] = read(), ans[i] = -1, s.insert(d[i]), mp[d[i]] = i;
  sort(d + 1, d + q + 1);
  for (int i = 1; i <= n; i++) {
    vec.clear();
    it1 = s.lower_bound(max(0, p[i].s - p[i].x));
    it2 = s.upper_bound(max(0, p[i].t - p[i].x));
    // cout<<*it1<<" "<<*it2<<endl;
    if (it1 == s.end())
      continue;
    for (set<int>::iterator it = it1; it != s.end() && it != it2; it++) {
      ans[mp[*it]] = p[i].x;
      vec.push_back(*it);
    }
    for (int i = 0; i < vec.size(); i++)
      s.erase(vec[i]);
  }
  for (int i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define maxn 200005
#define inf 1e9
#define eps 1e-10
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

int n, q, d[maxn], nxt[maxn], ans[maxn], pre[maxn];
struct node {
  int s, t, x;
} p[maxn];
struct node2 {
  int x, id;
};
set<int> s, tmp;
set<int>::iterator it1, it2;
map<int, int> mp;
vector<int> vec;

inline bool cmp(node a, node b) { return a.x < b.x; }

int main() {
  n = read();
  q = read();
  for (int i = 1; i <= n; i++)
    p[i].s = read(), p[i].t = read() - 1, p[i].x = read();
  sort(p + 1, p + n + 1, cmp);
  for (int i = 1; i <= q; i++)
    d[i] = read(), ans[i] = -1, s.insert(d[i]), mp[d[i]] = i;
  sort(d + 1, d + q + 1);
  for (int i = 1; i <= n; i++) {
    vec.clear();
    it1 = s.lower_bound(max(0, p[i].s - p[i].x));
    it2 = s.upper_bound(max(0, p[i].t - p[i].x));
    // cout<<*it1<<" "<<*it2<<endl;
    if (it1 == s.end())
      continue;
    for (set<int>::iterator it = it1; it != s.end() && it != it2; it++) {
      ans[mp[*it]] = p[i].x;
      vec.push_back(*it);
    }
    for (int i = 0; i < vec.size(); i++)
      s.erase(vec[i]);
  }
  for (int i = 1; i <= q; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03033 (C++)

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

#define N 200010
#define pii pair<int, int>
#define fi first
#define se second
int n, q;
struct node {
  int s, t, x;
  void scan() {
    scanf("%d%d%d", &s, &t, &x);
    s -= x;
    t -= x;
  }
  bool operator<(const node &other) const { return x < other.x; }
} qrr[N];
set<pii> se;
int res[N];

int main() {
  while (scanf("%d%d", &n, &q) != EOF) {
    memset(res, -1, sizeof res);
    se.clear();
    for (int i = 1; i <= n; ++i) {
      qrr[i].scan();
    }
    sort(qrr + 1, qrr + 1 + n);
    for (int i = 1, d; i <= q; ++i) {
      scanf("%d", &d);
      se.insert(pii(d, i));
    }
    for (int i = 1; i <= n; ++i) {
      if (qrr[i].t <= 0) {
        continue;
      }
      while (!se.empty()) {
        auto pos = se.lower_bound(pii(qrr[i].s, -1));
        if ((*pos).fi < qrr[i].t) {
          res[(*pos).se] = qrr[i].x;
          se.erase(pos);
        } else {
          break;
        }
      }
    }
    for (int i = 1; i <= q; ++i) {
      printf("%d\n", res[i]);
    }
  }
  return 0;
}


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

#define N 200010
#define pii pair<int, int>
#define fi first
#define se second
int n, q;
struct node {
  int s, t, x;
  void scan() {
    scanf("%d%d%d", &s, &t, &x);
    s -= x;
    t -= x;
  }
  bool operator<(const node &other) const { return x < other.x; }
} qrr[N];
set<pii> se;
int res[N];

int main() {
  while (scanf("%d%d", &n, &q) != EOF) {
    memset(res, -1, sizeof res);
    se.clear();
    for (int i = 1; i <= n; ++i) {
      qrr[i].scan();
    }
    sort(qrr + 1, qrr + 1 + n);
    for (int i = 1, d; i <= q; ++i) {
      scanf("%d", &d);
      se.insert(pii(d, i));
    }
    for (int i = 1; i <= n; ++i) {
      if (qrr[i].t <= 0) {
        continue;
      }
      while (!se.empty()) {
        auto pos = se.lower_bound(pii(qrr[i].s, -1));
        if (pos == se.end()) {
          break;
        }
        if ((*pos).fi < qrr[i].t) {
          res[(*pos).se] = qrr[i].x;
          se.erase(pos);
        } else {
          break;
        }
      }
    }
    for (int i = 1; i <= q; ++i) {
      printf("%d\n", res[i]);
    }
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#define MN 201000

std::set<int> st;

struct xxx {
  int t, o, w;
  xxx() {}
  xxx(int a, int b, int c) {
    t = a;
    o = b;
    w = c;
  }
} b[MN];

bool cmp(xxx a, xxx b) { return a.t == b.t ? a.o < b.o : a.t < b.t; }

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  int m = 0;
  for (int i = 1; i <= n; i++) {
    int s, t, c;
    scanf("%d%d%d", &s, &t, &c);
    s -= c;
    t -= c;
    b[++m] = xxx(s, 1, c);
    b[++m] = xxx(t, 0, c);
  }
  std::sort(b + 1, b + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= q; i++) {
    int d;
    scanf("%d", &d);
    while (j <= m && d >= b[j].t) {
      if (!b[j].o)
        st.erase(b[j].w);
      else
        st.insert(b[j].w);
      j++;
    }
    if (st.size() == 0)
      puts("-1");
    else
      printf("%d\n", *st.begin());
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#define MN 401000

std::set<int> st;

struct xxx {
  int t, o, w;
  xxx() {}
  xxx(int a, int b, int c) {
    t = a;
    o = b;
    w = c;
  }
} b[MN];

bool cmp(xxx a, xxx b) { return a.t == b.t ? a.o < b.o : a.t < b.t; }

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  int m = 0;
  for (int i = 1; i <= n; i++) {
    int s, t, c;
    scanf("%d%d%d", &s, &t, &c);
    s -= c;
    t -= c;
    b[++m] = xxx(s, 1, c);
    b[++m] = xxx(t, 0, c);
  }
  std::sort(b + 1, b + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= q; i++) {
    int d;
    scanf("%d", &d);
    while (j <= m && d >= b[j].t) {
      if (!b[j].o)
        st.erase(b[j].w);
      else
        st.insert(b[j].w);
      j++;
    }
    if (st.size() == 0)
      puts("-1");
    else
      printf("%d\n", *st.begin());
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
using ll = long long;

using pii = pair<int, int>;
int n, q, s[200010], t[200010], x[200010], d[200010];
set<int> block;
priority_queue<pii, vector<pii>, greater<pii>> st, ed;
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", s + i, t + i, x + i);
    s[i] -= x[i];
    t[i] -= x[i];
    st.emplace(s[i], i);
  }
  for (int i = 0; i < q; i++)
    scanf("%d", d + i);
  for (int i = 0; i < q; i++) {
    while (!ed.empty() && ed.top().first <= d[i]) {
      block.erase(x[ed.top().second]);
      ed.pop();
    }
    while (!st.empty() && st.top().first <= d[i]) {
      int id = st.top().first;
      if (d[i] < t[id]) {
        block.emplace(x[id]);
        ed.emplace(t[id], id);
      }
      st.pop();
    }
    if (block.empty())
      printf("-1\n");
    else
      printf("%d\n", *block.begin());
  }
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
using ll = long long;

using pii = pair<int, int>;
int n, q, s[200010], t[200010], x[200010], d[200010];
set<int> block;
priority_queue<pii, vector<pii>, greater<pii>> st, ed;
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", s + i, t + i, x + i);
    s[i] -= x[i];
    t[i] -= x[i];
    st.emplace(s[i], i);
  }
  for (int i = 0; i < q; i++)
    scanf("%d", d + i);
  for (int i = 0; i < q; i++) {
    while (!ed.empty() && ed.top().first <= d[i]) {
      block.erase(x[ed.top().second]);
      ed.pop();
    }
    while (!st.empty() && st.top().first <= d[i]) {
      int id = st.top().second;
      if (d[i] < t[id]) {
        block.emplace(x[id]);
        ed.emplace(t[id], id);
      }
      st.pop();
    }
    if (block.empty())
      printf("-1\n");
    else
      printf("%d\n", *block.begin());
  }
}
Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const int mod = 1e+9 + 7;
const int INF = 1e+9;

bool used[200005];

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

  int n, q;
  cin >> n >> q;
  vector<tuple<int, int, int>> tup(n);
  set<P> s;
  for (int i = 0; i < n; ++i) {
    int s, t, x;
    cin >> s >> t >> x;
    s -= x;
    t -= x;
    tup[i] = tie(x, s, t);
  }
  for (int i = 0; i < q; ++i) {
    int d;
    cin >> d;
    s.insert(P(d, i));
  }

  sort(tup.begin(), tup.end());
  vector<int> res(q, -1);
  for (int i = 0; i < n; ++i) {
    int x, st, term;
    tie(x, st, term) = tup[i];
    auto it = lower_bound(s.begin(), s.end(), P(st, -1));
    while (it != s.end()) {
      if (it->first >= term)
        break;
      res[it->second] = x;
      auto tmp = it++;
      s.erase(tmp);
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << res[i] << "\n";
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.1415926535897932; // acos(-1)
const double EPS = 1e-15;
const int mod = 1e+9 + 7;
const int INF = 1e+9;

bool used[200005];

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

  int n, q;
  cin >> n >> q;
  vector<tuple<int, int, int>> tup(n);
  set<P> s;
  for (int i = 0; i < n; ++i) {
    int s, t, x;
    cin >> s >> t >> x;
    s -= x;
    t -= x;
    tup[i] = tie(x, s, t);
  }
  for (int i = 0; i < q; ++i) {
    int d;
    cin >> d;
    s.insert(P(d, i));
  }

  sort(tup.begin(), tup.end());
  vector<int> res(q, -1);
  for (int i = 0; i < n; ++i) {
    int x, st, term;
    tie(x, st, term) = tup[i];
    auto it = s.lower_bound(P(st, -1));
    while (it != s.end()) {
      if (it->first >= term)
        break;
      res[it->second] = x;
      auto tmp = it++;
      s.erase(tmp);
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << res[i] << "\n";
  }
}
Problem p03033 (C++)

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

int main() {
  int n, q;
  cin >> n >> q;

  vector<pair<int, pair<int, int>>> closed;
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    closed.push_back({s - x, {1, x}});  // type :  1
    closed.push_back({t - x, {-1, x}}); // type : -1
  }
  sort(closed.begin(), closed.end()); // -1 < 1

  vector<int> d(q);
  for (int i = 0; i < q; i++) {
    cin >> d[i];
  }

  set<int> current; // set of closed points
  int j = 0;
  for (int i = 0; i < q; i++) {
    while (closed[j].first <= d[i]) {
      int type = closed[j].second.first;
      int x = closed[j].second.second;
      if (type == 1)
        current.insert(x);
      if (type == -1)
        current.erase(x);
      j += 1;
    }
    if (current.empty() == false)
      cout << *current.begin() << endl;
    if (current.empty() == true)
      cout << -1 << endl;
  }
  return 0;
}

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

int main() {
  int n, q;
  cin >> n >> q;

  vector<pair<int, pair<int, int>>> closed;
  for (int i = 0; i < n; i++) {
    int s, t, x;
    cin >> s >> t >> x;
    closed.push_back({s - x, {1, x}});  // type :  1
    closed.push_back({t - x, {-1, x}}); // type : -1
  }
  sort(closed.begin(), closed.end()); // -1 < 1

  vector<int> d(q);
  for (int i = 0; i < q; i++) {
    cin >> d[i];
  }

  set<int> current; // set of closed points
  int j = 0;
  for (int i = 0; i < q; i++) {
    while (j < 2 * n && closed[j].first <= d[i]) {
      int type = closed[j].second.first;
      int x = closed[j].second.second;
      if (type == 1)
        current.insert(x);
      if (type == -1)
        current.erase(x);
      j += 1;
    }
    if (current.empty() == false)
      cout << *current.begin() << endl;
    if (current.empty() == true)
      cout << -1 << endl;
  }
  return 0;
}
Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 1LL << 39
#define INF 100000000
#define MOD 1000000007LL
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define prique priority_queue
#define PI acos(-1)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef vector<ll> poly;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct edge {
  int to, cost;
};

signed main() {
  int n, q;
  cin >> n >> q;
  vector<pair<pair<int, int>, int>> l(n);
  vi s(n), t(n), x(n);
  set<int> d;
  map<int, int> m;
  rep(i, n) cin >> l[i].first.first >> l[i].first.second >> l[i].second;
  sort(all(l), [](auto &x, auto &y) { return x.second < y.second; });
  rep(i, n) {
    s[i] = l[i].first.first;
    t[i] = l[i].first.second;
    x[i] = l[i].second;
  }
  rep(i, q) {
    int a;
    cin >> a;
    d.insert(a);
    m[a] = i;
  }
  vi ans(q, -1);
  rep(i, n) {
    auto it = d.lower_bound(s[i] - x[i]);
    while (*it < t[i] - x[i]) {
      ans[m[*it]] = x[i];
      d.erase(it);
      it = d.lower_bound(s[i] - x[i]);
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 1LL << 39
#define INF 100000000
#define MOD 1000000007LL
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define prique priority_queue
#define PI acos(-1)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef vector<ll> poly;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct edge {
  int to, cost;
};

signed main() {
  int n, q;
  cin >> n >> q;
  vector<pair<pair<int, int>, int>> l(n);
  vi s(n), t(n), x(n);
  set<int> d;
  map<int, int> m;
  rep(i, n) cin >> l[i].first.first >> l[i].first.second >> l[i].second;
  sort(all(l), [](auto &x, auto &y) { return x.second < y.second; });
  rep(i, n) {
    s[i] = l[i].first.first;
    t[i] = l[i].first.second;
    x[i] = l[i].second;
  }
  rep(i, q) {
    int a;
    cin >> a;
    d.insert(a);
    m[a] = i;
  }
  vi ans(q, -1);
  rep(i, n) {
    set<int>::iterator it = d.lower_bound(s[i] - x[i]);
    while (it != d.end() && *it < t[i] - x[i]) {
      ans[m[*it]] = x[i];
      d.erase(it);
      it = d.lower_bound(s[i] - x[i]);
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
}