Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600060, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
struct seg {
  int l, r;
  bool operator<(const seg &s) const { return l < s.l; }
};
int n, q, s[maxn], t[maxn], x[maxn], tmp[maxn * 2], sz, tl, val[maxn],
    cov[maxn], d[maxn];
inline int binary(int x) { return lower_bound(tmp + 1, tmp + sz + 1, x) - tmp; }
void pushdown(int x) {
  if (cov[x] != 2e9) {
    val[x << 1] = min(val[x << 1], cov[x]);
    val[x << 1 | 1] = min(val[x << 1 | 1], cov[x]);
    cov[x << 1] = min(cov[x << 1], cov[x]);
    cov[x << 1 | 1] = min(cov[x << 1 | 1], cov[x]);
    cov[x] = 2e9;
  }
}
void build(int o, int l, int r) {
  cov[o] = val[o] = 2e9;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(lson);
  build(rson);
}
void update(int o, int l, int r, int ql, int qr, int v) {
  if (l >= ql && r <= qr)
    return void((cov[o] = min(cov[o], v), val[o] = min(val[o], v)));
  pushdown(o);
  int mid = (l + r) >> 1;
  if (mid >= ql)
    update(lson, ql, qr, v);
  if (mid < qr)
    update(rson, ql, qr, v);
}
int query(int o, int l, int r, int p) {
  if (l == r)
    return val[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  if (mid >= p)
    return query(lson, p);
  else
    return query(rson, p);
}
int main() {
  n = read();
  q = read();
  FOR(i, 1, n) {
    s[i] = read();
    t[i] = read();
    x[i] = read();
    tmp[++tl] = s[i] - x[i];
    tmp[++tl] = t[i] - 1 - x[i];
  }
  FOR(i, 1, q) d[i] = read(), tmp[++tl] = d[i];
  sort(tmp + 1, tmp + tl + 1);
  sz = unique(tmp + 1, tmp + tl + 1) - tmp - 1;
  build(1, 1, sz);
  FOR(i, 1, n)
  update(1, 1, sz, binary(s[i] - x[i]), binary(t[i] - 1 - x[i]), x[i]);
  FOR(i, 1, q) {
    int x = query(1, 1, sz, binary(d[i]));
    printf("%d\n", x == 2e9 ? -1 : x);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600060, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
struct seg {
  int l, r;
  bool operator<(const seg &s) const { return l < s.l; }
};
int n, q, s[maxn], t[maxn], x[maxn], tmp[maxn * 2], sz, tl, val[maxn * 4],
    cov[maxn * 4], d[maxn];
inline int binary(int x) { return lower_bound(tmp + 1, tmp + sz + 1, x) - tmp; }
void pushdown(int x) {
  if (cov[x] != 2e9) {
    val[x << 1] = min(val[x << 1], cov[x]);
    val[x << 1 | 1] = min(val[x << 1 | 1], cov[x]);
    cov[x << 1] = min(cov[x << 1], cov[x]);
    cov[x << 1 | 1] = min(cov[x << 1 | 1], cov[x]);
    cov[x] = 2e9;
  }
}
void build(int o, int l, int r) {
  cov[o] = val[o] = 2e9;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(lson);
  build(rson);
}
void update(int o, int l, int r, int ql, int qr, int v) {
  if (l >= ql && r <= qr)
    return void((cov[o] = min(cov[o], v), val[o] = min(val[o], v)));
  pushdown(o);
  int mid = (l + r) >> 1;
  if (mid >= ql)
    update(lson, ql, qr, v);
  if (mid < qr)
    update(rson, ql, qr, v);
}
int query(int o, int l, int r, int p) {
  if (l == r)
    return val[o];
  pushdown(o);
  int mid = (l + r) >> 1;
  if (mid >= p)
    return query(lson, p);
  else
    return query(rson, p);
}
int main() {
  n = read();
  q = read();
  FOR(i, 1, n) {
    s[i] = read();
    t[i] = read();
    x[i] = read();
    tmp[++tl] = s[i] - x[i];
    tmp[++tl] = t[i] - 1 - x[i];
  }
  FOR(i, 1, q) d[i] = read(), tmp[++tl] = d[i];
  sort(tmp + 1, tmp + tl + 1);
  sz = unique(tmp + 1, tmp + tl + 1) - tmp - 1;
  build(1, 1, sz);
  FOR(i, 1, n)
  update(1, 1, sz, binary(s[i] - x[i]), binary(t[i] - 1 - x[i]), x[i]);
  FOR(i, 1, q) {
    int x = query(1, 1, sz, binary(d[i]));
    printf("%d\n", x == 2e9 ? -1 : x);
  }
}