Problem p02868 (C++)

Buggy submission (status: Runtime Error):
/// kAAzuki Hoshino
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define INF 100000000000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 101010;
int t[MAXN], lazy[MAXN];

void init() {
  fill_n(t, MAXN, INF);
  fill_n(lazy, MAXN, INF);
}

void push(int v) {
  t[v * 2] = min(lazy[v], t[2 * v]);
  lazy[v * 2] = min(lazy[v], lazy[2 * v]);
  t[v * 2 + 1] = min(t[2 * v + 1], lazy[v]);
  lazy[v * 2 + 1] = min(lazy[v], lazy[2 * v + 1]);
  // lazy[v] = INF;
}

void update(int v, int tl, int tr, int l, int r, int addend) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    t[v] = min(addend, t[v]);
    lazy[v] = min(addend, lazy[v]);
  } else {
    push(v);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), addend);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
  }
}

int query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return INF;
  if (l <= tl && tr <= r)
    return t[v];
  push(v);
  int tm = (tl + tr) / 2;
  return min(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

int n, m, X[MAXN], T[MAXN], P[MAXN], B[MAXN], ans[MAXN];
vector<pair<int, int>> adj[MAXN];
int dist[MAXN];

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  init();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    adj[l].pb({r, c});
  }
  update(1, 0, n - 1, 0, 0, 0);
  for (int i = 0; i < n; i++) {
    dist[i] = query(1, 0, n - 1, i, i);
    for (auto t : adj[i]) {
      int cur = dist[i] + t.sc;
      int l = i, r = t.fs;
      update(1, 0, n - 1, l, r, cur);
    }
  }
  if (dist[n - 1] >= INF) {
    dist[n - 1] = -1;
  }
  cout << dist[n - 1] << endl;

  return 0;
}
///....


Fixed submission:
/// kAAzuki Hoshino
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define INF 100000000000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define MAXN 1 << 20
int t[MAXN], lazy[MAXN];

void init() {
  fill_n(t, MAXN, INF);
  fill_n(lazy, MAXN, INF);
}

void push(int v) {
  t[v * 2] = min(lazy[v], t[2 * v]);
  lazy[v * 2] = min(lazy[v], lazy[2 * v]);
  t[v * 2 + 1] = min(t[2 * v + 1], lazy[v]);
  lazy[v * 2 + 1] = min(lazy[v], lazy[2 * v + 1]);
  // lazy[v] = INF;
}

void update(int v, int tl, int tr, int l, int r, int addend) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    t[v] = min(addend, t[v]);
    lazy[v] = min(addend, lazy[v]);
  } else {
    push(v);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), addend);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend);
    t[v] = min(t[v * 2], t[v * 2 + 1]);
  }
}

int query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return INF;
  if (l <= tl && tr <= r)
    return t[v];
  push(v);
  int tm = (tl + tr) / 2;
  return min(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

int n, m, X[MAXN], T[MAXN], P[MAXN], B[MAXN], ans[MAXN];
vector<pair<int, int>> adj[MAXN];
int dist[MAXN];

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  init();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    adj[l].pb({r, c});
  }
  update(1, 0, n - 1, 0, 0, 0);
  for (int i = 0; i < n; i++) {
    dist[i] = query(1, 0, n - 1, i, i);
    for (auto t : adj[i]) {
      int cur = dist[i] + t.sc;
      int l = i, r = t.fs;
      update(1, 0, n - 1, l, r, cur);
    }
  }
  if (dist[n - 1] >= INF) {
    dist[n - 1] = -1;
  }
  cout << dist[n - 1] << endl;

  return 0;
}
///....