Problem p03033 (C++)

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

#ifdef DEBUG
#include "../include/debug.hpp"
#else
#define debug(...) 42
#endif

class LazySegTree {
public:
  int n;
  vector<int> nodes;

  LazySegTree(int _n) {
    n = 1 << (int)ceil(log2(_n));
    nodes.resize(n * 2 - 1, INT_MAX);
  }

  void update(int l, int r, int val) { _update(0, 0, n, l, r, val); }

  void _update(int k, int kl, int kr, int l, int r, int val) {
    if (kr <= l || r <= kl)
      return;
    if (l <= kl && kr <= r) {
      nodes[k] = min(nodes[k], val);
      return;
    }
    _update(k * 2 + 1, kl, (kl + kr) / 2, l, r, val);
    _update(k * 2 + 2, (kl + kr) / 2, kr, l, r, val);
  }

  int get(int k) {
    int res = INT_MAX;
    int i = 0, l = 0, r = n;
    while (i != n - 1 + k) {
      res = min(res, nodes[i]);
      if (k < (l + r) / 2) {
        i = i * 2 + 1;
        r = (l + r) / 2;
      } else {
        i = i * 2 + 2;
        l = (l + r) / 2;
      }
    }
    return min(res, nodes[n - 1 + k]);
  }
};

vector<int> co;
int z(int x) {
  int res = lower_bound(co.begin(), co.end(), x) - co.begin();
  return res;
}

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

  int N, Q;
  cin >> N >> Q;

  vector<int> S(N), T(N), X(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i] >> T[i] >> X[i];
    co.push_back(S[i] - X[i]);
    co.push_back(T[i] - X[i]);
  }

  vector<int> D(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> D[i];
    co.push_back(D[i]);
  }
  sort(co.begin(), co.end());
  co.erase(unique(co.begin(), co.end()), co.end());

  LazySegTree segt(500000);

  for (int i = 0; i < N; ++i) {
    segt.update(z(S[i] - X[i]), z(T[i] - X[i]), X[i]);
  }

  for (int i = 0; i < Q; ++i) {
    int di = segt.get(z(D[i]));
    cout << (di == INT_MAX ? -1 : di) << endl;
  }

  return 0;
}

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

#ifdef DEBUG
#include "../include/debug.hpp"
#else
#define debug(...) 42
#endif

class LazySegTree {
public:
  int n;
  vector<int> nodes;

  LazySegTree(int _n) {
    n = 1 << (int)ceil(log2(_n));
    nodes.resize(n * 2 - 1, INT_MAX);
  }

  void update(int l, int r, int val) { _update(0, 0, n, l, r, val); }

  void _update(int k, int kl, int kr, int l, int r, int val) {
    if (kr <= l || r <= kl)
      return;
    if (l <= kl && kr <= r) {
      nodes[k] = min(nodes[k], val);
      return;
    }
    _update(k * 2 + 1, kl, (kl + kr) / 2, l, r, val);
    _update(k * 2 + 2, (kl + kr) / 2, kr, l, r, val);
  }

  int get(int k) {
    int res = INT_MAX;
    int i = 0, l = 0, r = n;
    while (i != n - 1 + k) {
      res = min(res, nodes[i]);
      if (k < (l + r) / 2) {
        i = i * 2 + 1;
        r = (l + r) / 2;
      } else {
        i = i * 2 + 2;
        l = (l + r) / 2;
      }
    }
    return min(res, nodes[n - 1 + k]);
  }
};

vector<int> co;
int z(int x) {
  int res = lower_bound(co.begin(), co.end(), x) - co.begin();
  return res;
}

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

  int N, Q;
  cin >> N >> Q;

  vector<int> S(N), T(N), X(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i] >> T[i] >> X[i];
    co.push_back(S[i] - X[i]);
    co.push_back(T[i] - X[i]);
  }

  vector<int> D(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> D[i];
    co.push_back(D[i]);
  }
  sort(co.begin(), co.end());
  co.erase(unique(co.begin(), co.end()), co.end());

  LazySegTree segt((int)co.size());

  for (int i = 0; i < N; ++i) {
    segt.update(z(S[i] - X[i]), z(T[i] - X[i]), X[i]);
  }

  for (int i = 0; i < Q; ++i) {
    int di = segt.get(z(D[i]));
    cout << (di == INT_MAX ? -1 : di) << endl;
  }

  return 0;
}