Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template <class Monoid> class SegmentTree {
private:
  using T = typename Monoid::T;
  const size_t n;
  vector<T> d;

  void build() {
    for (size_t b = n - 1; b > 0; b--)
      d[b] = Monoid::op(d[b << 1], d[b << 1 | 1]);
  }

public:
  SegmentTree(size_t n) : n(n), d(2 * n, Monoid::identify()) {}

  template <typename InputItr>
  SegmentTree(InputItr first, InputItr last)
      : n(distance(first, last)), d(2 * n, Monoid::identify()) {
    copy(first, last, next(d.begin(), n));
    build();
  }

  /* set one value */
  void modify(size_t k, T v) {
    for (d[k += n] = v; k > 1; k >>= 1)
      d[k >> 1] = Monoid::op(d[k], d[k ^ 1]);
  }

  /* return [l, r) value */
  T query(size_t l, size_t r) {
    T res{Monoid::identify()};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res = Monoid::op(res, d[l++]);
      if (r & 1)
        res = Monoid::op(res, d[--r]);
    }
    return res;
  }

  T get(size_t i) { return query(i, i + 1); }
};

template <typename U = ll> struct RMQ {
  using T = U;
  static T op(const T &a, const T &b) { return min<T>(a, b); }
  static constexpr T identify() { return numeric_limits<T>::max(); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(n), r(n), c(n);

  REP(i, m) {
    cin >> l[i] >> r[i] >> c[i];
    --l[i];
    --r[i];
  }

  vector<int> o(m);
  iota(o.begin(), o.end(), 0);
  sort(o.begin(), o.end(), [&](int i, int j) { return r[i] < r[j]; });

  SegmentTree<RMQ<>> tree(n);
  tree.modify(0, 0);

  REP(i, m) {
    ll m = tree.query(l[o[i]], r[o[i]]);
    if (m == RMQ<>::identify())
      continue;

    ll now = tree.get(r[o[i]]);
    tree.modify(r[o[i]], min(now, m + c[o[i]]));
  }

  if (tree.get(n - 1) != RMQ<>::identify()) {
    cout << tree.get(n - 1) << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template <class Monoid> class SegmentTree {
private:
  using T = typename Monoid::T;
  const size_t n;
  vector<T> d;

  void build() {
    for (size_t b = n - 1; b > 0; b--)
      d[b] = Monoid::op(d[b << 1], d[b << 1 | 1]);
  }

public:
  SegmentTree(size_t n) : n(n), d(2 * n, Monoid::identify()) {}

  template <typename InputItr>
  SegmentTree(InputItr first, InputItr last)
      : n(distance(first, last)), d(2 * n, Monoid::identify()) {
    copy(first, last, next(d.begin(), n));
    build();
  }

  /* set one value */
  void modify(size_t k, T v) {
    for (d[k += n] = v; k > 1; k >>= 1)
      d[k >> 1] = Monoid::op(d[k], d[k ^ 1]);
  }

  /* return [l, r) value */
  T query(size_t l, size_t r) {
    T res{Monoid::identify()};
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res = Monoid::op(res, d[l++]);
      if (r & 1)
        res = Monoid::op(res, d[--r]);
    }
    return res;
  }

  T get(size_t i) { return query(i, i + 1); }
};

template <typename U = ll> struct RMQ {
  using T = U;
  static T op(const T &a, const T &b) { return min<T>(a, b); }
  static constexpr T identify() { return numeric_limits<T>::max(); }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m), c(m);

  REP(i, m) {
    cin >> l[i] >> r[i] >> c[i];
    --l[i];
    --r[i];
  }

  vector<int> o(m);
  iota(o.begin(), o.end(), 0);
  sort(o.begin(), o.end(), [&](int i, int j) { return r[i] < r[j]; });

  SegmentTree<RMQ<>> tree(n);
  tree.modify(0, 0);

  REP(i, m) {
    ll m = tree.query(l[o[i]], r[o[i]]);
    if (m == RMQ<>::identify())
      continue;

    ll now = tree.get(r[o[i]]);
    tree.modify(r[o[i]], min(now, m + c[o[i]]));
  }

  if (tree.get(n - 1) != RMQ<>::identify()) {
    cout << tree.get(n - 1) << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}