Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;

const ll MOD = 1000000007;
const ll MAX_N = 100010, INF = 1e17;
struct LazySegmentTree {
  ll node[2 * MAX_N - 1], lazy[2 * MAX_N - 1], SIZE; // SIZE:nを２べきにしたもの
  bool lazyflag[2 * MAX_N - 1];

  // 初期化
  void init(int n_) {
    // 要素数を2べきに
    SIZE = 1;
    while (SIZE < n_)
      SIZE *= 2;

    // 全ての値をI_MAXに
    rep(i, 0, 2 * SIZE - 1) node[i] = INF;
  }

  // k番目のノードについて遅延評価を行う
  void eval(int k, int l, int r) {
    // 遅延配列が空でない場合、自ノード及び子ノードへの値の伝播が起こる
    if (lazyflag[k]) {
      node[k] = lazy[k];
      // 最下段かどうかのチェックをしよう
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyflag[k * 2 + 1] = lazyflag[k * 2 + 2] = true;
      }
      lazyflag[k] = false;
    }
  }

  void update(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = SIZE;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyflag[k] = true;
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  ll getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = SIZE;
    eval(k, l, r);
    if (b <= l || r <= a)
      return INF;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
LazySegmentTree S;
vector<PP> v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  v.resize(m);
  rep(i, 0, m) {
    cin >> v[i].fr.fr >> v[i].fr.sc >> v[i].sc;
    v[i].fr.fr--;
  }
  sor(v);
  S.init(n);
  S.update(0, 1, 0);
  rep(i, 0, m) {
    int l = v[i].fr.fr, r = v[i].fr.sc, c = v[i].sc;
    ll M = S.getmin(l, l + 1);
    if (M == INF)
      break;
    ll ng = r, ok = l;
    while (ng - ok > 1) {
      ll mid = (ng + ok) / 2;
      if (S.getmin(mid, mid + 1) <= M + c)
        ok = mid;
      else
        ng = mid;
    }
    if (ok + 1 < r)
      S.update(ok + 1, r, M + c);
  }
  cout << (S.getmin(n - 1, n) == INF ? -1 : S.getmin(n - 1, n)) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;

const ll MOD = 1000000007;
const ll MAX_N = 400010, INF = 1e17;
struct LazySegmentTree {
  ll node[2 * MAX_N - 1], lazy[2 * MAX_N - 1], SIZE; // SIZE:nを２べきにしたもの
  bool lazyflag[2 * MAX_N - 1];

  // 初期化
  void init(int n_) {
    // 要素数を2べきに
    SIZE = 1;
    while (SIZE < n_)
      SIZE *= 2;

    // 全ての値をI_MAXに
    rep(i, 0, 2 * SIZE - 1) node[i] = INF;
  }

  // k番目のノードについて遅延評価を行う
  void eval(int k, int l, int r) {
    // 遅延配列が空でない場合、自ノード及び子ノードへの値の伝播が起こる
    if (lazyflag[k]) {
      node[k] = lazy[k];
      // 最下段かどうかのチェックをしよう
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyflag[k * 2 + 1] = lazyflag[k * 2 + 2] = true;
      }
      lazyflag[k] = false;
    }
  }

  void update(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = SIZE;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyflag[k] = true;
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  ll getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = SIZE;
    eval(k, l, r);
    if (b <= l || r <= a)
      return INF;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
LazySegmentTree S;
vector<PP> v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  v.resize(m);
  rep(i, 0, m) {
    cin >> v[i].fr.fr >> v[i].fr.sc >> v[i].sc;
    v[i].fr.fr--;
  }
  sor(v);
  S.init(n);
  S.update(0, 1, 0);
  rep(i, 0, m) {
    int l = v[i].fr.fr, r = v[i].fr.sc, c = v[i].sc;
    ll M = S.getmin(l, l + 1);
    if (M == INF)
      break;
    ll ng = r, ok = l;
    while (ng - ok > 1) {
      ll mid = (ng + ok) / 2;
      if (S.getmin(mid, mid + 1) <= M + c)
        ok = mid;
      else
        ng = mid;
    }
    if (ok + 1 < r)
      S.update(ok + 1, r, M + c);
  }
  cout << (S.getmin(n - 1, n) == INF ? -1 : S.getmin(n - 1, n)) << "\n";
}