Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 1000000007;
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 100000000000000000;

#define Int long long
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename E> struct SegmentTree {
  using H = function<E(E, E)>;
  Int n, height;
  H h;
  E ei;
  vector<E> laz;

  SegmentTree(H h, E ei) : h(h), ei(ei) {}

  void init(Int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    laz.assign(2 * n, ei);
  }

  inline void propagate(Int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    laz[k] = ei;
  }

  inline void thrust(Int k) {
    for (Int i = height; i; i--)
      propagate(k >> i);
  }

  void update(Int a, Int b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (Int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
  }

  E get_val(Int a) {
    thrust(a += n);
    return laz[a];
  }

  void set_val(Int a, E x) {
    thrust(a += n);
    laz[a] = x;
  }
};

// INSERT ABOVE HERE

int main() {
  ll n, m;
  cin >> n >> m;
  auto h = [&](ll x, ll y) { return min(x, y); };
  const ll INF = 1e18;
  SegmentTree<ll> seg(h, INF);
  seg.init(n);
  seg.set_val(0, 0);
  vector<ll> l(m), r(m), c(m);
  vector<vector<ll>> G(n + 1, vector<ll>(0));
  rep(i, m) {
    cin >> l[i] >> r[i] >> c[i];
    G[--l[i]].push_back(i);
  }
  rep(i, n) {
    ll d = seg.get_val(i);
    rep(j, G[i].size()) seg.update(l[i], r[i], d + c[i]);
  }
  ll ans = seg.get_val(n - 1);
  if (ans >= INF)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 1000000007;
ll MOD = 998244353;
int inf = 1000001000;
ll INF = 100000000000000000;

#define Int long long
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename E> struct SegmentTree {
  using H = function<E(E, E)>;
  Int n, height;
  H h;
  E ei;
  vector<E> laz;

  SegmentTree(H h, E ei) : h(h), ei(ei) {}

  void init(Int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    laz.assign(2 * n, ei);
  }

  inline void propagate(Int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    laz[k] = ei;
  }

  inline void thrust(Int k) {
    for (Int i = height; i; i--)
      propagate(k >> i);
  }

  void update(Int a, Int b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (Int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
  }

  E get_val(Int a) {
    thrust(a += n);
    return laz[a];
  }

  void set_val(Int a, E x) {
    thrust(a += n);
    laz[a] = x;
  }
};

// INSERT ABOVE HERE

int main() {
  ll n, m;
  cin >> n >> m;
  auto h = [&](ll x, ll y) { return min(x, y); };
  const ll INF = 1e18;
  SegmentTree<ll> seg(h, INF);
  seg.init(n);
  seg.set_val(0, 0);
  vector<ll> l(m), r(m), c(m);
  vector<vector<ll>> G(n + 1, vector<ll>(0));
  rep(i, m) {
    cin >> l[i] >> r[i] >> c[i];
    G[--l[i]].push_back(i);
  }
  rep(i, n) {
    ll d = seg.get_val(i);
    for (ll j : G[i])
      seg.update(l[j], r[j], d + c[j]);
  }
  ll ans = seg.get_val(n - 1);
  if (ans >= INF)
    ans = -1;
  cout << ans << endl;
}