Problem p03033 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PP pair<int, int>
int inf = 1 << 30;
int N, Q;
int S[300010], T[300010], X;
vector<int> D;
vector<PP> xs;

int n, q, dat[500010];

void init(int &n_) {
  int n = 1;
  while (n < n_)
    n *= 2;
  n_ = n;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = inf;
}
void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
  if (r < 0)
    r = n;
  if (r <= a)
    return;
  if (b <= l)
    return;
  if (a <= l && r <= b) {
    dat[k] = min(dat[k], x);
    return;
  }

  update(a, b, x, k * 2 + 1, l, (r + l) / 2);
  update(a, b, x, k * 2 + 2, (r + l) / 2, r);
}
int find(int a, int b, int k = 0, int l = 0, int r = -1) {
  if (r < 0)
    r = n;
  if (r <= a)
    return inf;
  if (b <= l)
    return inf;
  if (a <= l && r <= b)
    return dat[k];
  int val1 = find(a, b, k * 2 + 1, l, (r + l) / 2);
  int val2 = find(a, b, k * 2 + 2, (r + l) / 2, r);
  return std::min(val1, val2);
}

int mini(int k) {
  if (k <= 0)
    return dat[0];
  return min(dat[k], mini((k - 1) / 2));
}

int main() {
  cin >> N >> Q;
  n = Q;
  init(n);
  for (int i = 0; i < N; i++) {
    cin >> S[i] >> T[i] >> X;
    xs.push_back({X, i});
  }
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.push_back(d);
  }
  for (int i = 0; i < N; i++) {
    int idx = xs[i].second;
    int X = xs[i].first;
    int lb = lower_bound(D.begin(), D.end(), S[idx] - X) - D.begin();
    int ub = lower_bound(D.begin(), D.end(), T[idx] - X) - D.begin();
    update(lb, ub, X);
  }
  for (int i = 0; i < Q; i++) {
    int ans = mini(i + n - 1);
    cout << (ans == 1 << 30 ? -1 : ans) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PP pair<int, int>
int inf = 1 << 30;
int N, Q;
int S[300010], T[300010], X;
vector<int> D;
vector<PP> xs;

int n, q, dat[600010];

void init(int &n_) {
  int n = 1;
  while (n < n_)
    n *= 2;
  n_ = n;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = inf;
}
void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
  if (r < 0)
    r = n;
  if (r <= a)
    return;
  if (b <= l)
    return;
  if (a <= l && r <= b) {
    dat[k] = min(dat[k], x);
    return;
  }

  update(a, b, x, k * 2 + 1, l, (r + l) / 2);
  update(a, b, x, k * 2 + 2, (r + l) / 2, r);
}
int find(int a, int b, int k = 0, int l = 0, int r = -1) {
  if (r < 0)
    r = n;
  if (r <= a)
    return inf;
  if (b <= l)
    return inf;
  if (a <= l && r <= b)
    return dat[k];
  int val1 = find(a, b, k * 2 + 1, l, (r + l) / 2);
  int val2 = find(a, b, k * 2 + 2, (r + l) / 2, r);
  return std::min(val1, val2);
}

int mini(int k) {
  if (k <= 0)
    return dat[0];
  return min(dat[k], mini((k - 1) / 2));
}

int main() {
  cin >> N >> Q;
  n = Q;
  init(n);
  for (int i = 0; i < N; i++) {
    cin >> S[i] >> T[i] >> X;
    xs.push_back({X, i});
  }
  for (int i = 0; i < Q; i++) {
    int d;
    cin >> d;
    D.push_back(d);
  }
  for (int i = 0; i < N; i++) {
    int idx = xs[i].second;
    int X = xs[i].first;
    int lb = lower_bound(D.begin(), D.end(), S[idx] - X) - D.begin();
    int ub = lower_bound(D.begin(), D.end(), T[idx] - X) - D.begin();
    update(lb, ub, X);
  }
  for (int i = 0; i < Q; i++) {
    int ans = mini(i + n - 1);
    cout << (ans == 1 << 30 ? -1 : ans) << endl;
  }
  return 0;
}