Language: C++

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>

using namespace std;
using ll = long long;

#define rep(i, n)      for (int i = 0; i < (n); i++)
#define repr(i, n)     for (int i = (n) - 1; i >= 0; i--)
#define repe(i, l, r)  for (int i = (l); i < (r); i++)
#define reper(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define repi(i, l, r)  for (int i = (l); i <= (r); i++)
#define repir(i, l, r) for (int i = (r); i >= (l); i--)
#define range(a) a.begin(), a.end()
void init_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

template<class Vec, class Mat, class VV, class MV, class MM>
struct lazy_segtree {
  int N;
  vector<Vec> vec;
  vector<Mat> mat;
  Vec vec_ident;
  Mat mat_ident;

  VV vec_vec;
  MV mat_vec;
  MM mat_mat;

  lazy_segtree(
    int n,
    function<Vec(int)> init,
    Vec vec_ident_,
    Mat mat_ident_,
    VV vec_vec_,
    MV mat_vec_,
    MM mat_mat_) :
    vec_ident(vec_ident_),
    mat_ident(mat_ident_),
    vec_vec(vec_vec_),
    mat_vec(mat_vec_),
    mat_mat(mat_mat_)
   {
    N = 1;
    while (N < n) N *= 2;
    vec.resize(N * 2, vec_ident);
    mat.resize(N * 2, mat_ident);
    for (int i = 0; i < n; i++) {
      vec[i + N] = init(i);
    }
    for (int i = N - 1; i >= 1; i--) {
      pull(i);
    }
  }

  void apply(int k, Mat v) {
    mat[k] = mat_mat(v, mat[k]);
    vec[k] = mat_vec(v, vec[k]);
  }

  void push(int k) {
    apply(k << 1 | 0, mat[k]);
    apply(k << 1 | 1, mat[k]);
    mat[k] = mat_ident;
  }

  void pull(int k) {
    vec[k] = vec_vec(vec[k << 1], vec[k << 1 | 1]);
  }

  void update(int a, int b, Mat v, int k, int l, int r) {
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      apply(k, v);
      return;
    }
    push(k);
    update(a, b, v, k << 1 | 0, l, l + r >> 1);
    update(a, b, v, k << 1 | 1, l + r >> 1, r);
    pull(k);
  }

  void update(int a, int b, Mat v) {
    update(a, b, v, 1, 0, N);
  }

  Vec query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return vec_ident;
    if (a <= l && r <= b) return vec[k];
    push(k);
    Vec vl = query(a, b, k << 1 | 0, l, l + r >> 1);
    Vec vr = query(a, b, k << 1 | 1, l + r >> 1, r);
    return vec_vec(vl, vr);
  }

  Vec query(int a, int b) {
    return query(a, b, 1, 0, N);
  }
};

template<class Vec, class Mat, class VV, class MV, class MM>
lazy_segtree<Vec, Mat, VV, MV, MM> create_lazy_segtree(
  int n,
  function<Vec(int)> init,
  Vec vec_ident_,
  Mat mat_ident_,
  VV vec_vec_,
  MV mat_vec_,
  MM mat_mat_) {
  return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
}

auto lz_assign_plus(int n, function<pair<long long, long long>(int)> init) {
  using vec = pair<long long, long long>;
  using mat = long long;
  return create_lazy_segtree<vec, mat>(
    n,
    init,
    {0LL, 0LL},
    LLONG_MAX,
    [](vec a, vec b) -> vec { return {a.first + b.first, a.second + b.second}; },
    [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : vec(a * b.second, b.second); },
    [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
}

auto lz_assign_min(int n, function<long long(int)> init) {
  using vec = long long;
  using mat = long long;
  return create_lazy_segtree(
    n,
    init,
    LLONG_MAX,
    LLONG_MAX,
    [](vec a, vec b) -> vec { return min(a, b); },
    [](mat a, vec b) -> vec { return min(a, b); },
    [](mat a, mat b) -> mat { return min(a, b); });
}

auto lz_assign_max(int n, function<long long(int)> init) {
  using vec = long long;
  using mat = long long;
  return create_lazy_segtree(
    n,
    init,
    LLONG_MIN,
    LLONG_MAX,
    [](vec a, vec b) -> vec { return max(a, b); },
    [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : a; },
    [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
}

auto lz_plus_min(int n, function<long long(int)> init) {
  using vec = long long;
  using mat = long long;
  return create_lazy_segtree(
    n,
    init,
    LLONG_MAX,
    0LL,
    [](vec a, vec b) { return min(a, b); },
    [](mat a, vec b) { return a + b; },
    [](mat a, mat b) { return a + b; });
}

auto lz_plus_max(int n, function<long long(int)> init) {
  using vec = long long;
  using mat = long long;
  return create_lazy_segtree(
    n,
    init,
    LLONG_MIN,
    0LL,
    [](vec a, vec b) { return max(a, b); },
    [](mat a, vec b) { return a + b; },
    [](mat a, mat b) { return a + b; });
}



int main() { init_io();
  int N, M; cin >> N >> M;
  vector<vector<pair<int, int>>> G(N);
  rep(i, M) {
    int l, r, c; cin >> l >> r >> c; l--;
    G[l].emplace_back(r, c);
  }
  constexpr ll INF = 1e18;
  auto seg = lz_assign_min(N, [](int i) -> ll {
    if (i == 0) return 0;
    return INF;
  });
  rep(i, N) {
    ll curr = seg.query(i, i + 1);
    for (auto p : G[i]) {
      seg.update(i + 1, p.first, curr + p.second);
    }
  }
  ll ans = seg.query(N - 1, N);
  if (ans >= INF) {
    cout << -1 << endl;
  } else {
    cout << seg.query(N - 1, N) << endl;
  }
}

Compiler output:
a.cc:37:5: error: 'function' has not been declared
   37 |     function<Vec(int)> init,
      |     ^~~~~~~~
a.cc:37:13: error: expected ',' or '...' before '<' token
   37 |     function<Vec(int)> init,
      |             ^
a.cc: In constructor 'lazy_segtree<Vec, Mat, VV, MV, MM>::lazy_segtree(int, int)':
a.cc:43:15: error: 'vec_ident_' was not declared in this scope; did you mean 'vec_ident'?
   43 |     vec_ident(vec_ident_),
      |               ^~~~~~~~~~
      |               vec_ident
a.cc:44:15: error: 'mat_ident_' was not declared in this scope; did you mean 'mat_ident'?
   44 |     mat_ident(mat_ident_),
      |               ^~~~~~~~~~
      |               mat_ident
a.cc:45:13: error: 'vec_vec_' was not declared in this scope; did you mean 'vec_vec'?
   45 |     vec_vec(vec_vec_),
      |             ^~~~~~~~
      |             vec_vec
a.cc:46:13: error: 'mat_vec_' was not declared in this scope; did you mean 'mat_vec'?
   46 |     mat_vec(mat_vec_),
      |             ^~~~~~~~
      |             mat_vec
a.cc:47:13: error: 'mat_mat_' was not declared in this scope; did you mean 'mat_mat'?
   47 |     mat_mat(mat_mat_)
      |             ^~~~~~~~
      |             mat_mat
a.cc:54:20: error: there are no arguments to 'init' that depend on a template parameter, so a declaration of 'init' must be available [-fpermissive]
   54 |       vec[i + N] = init(i);
      |                    ^~~~
a.cc:54:20: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: At global scope:
a.cc:109:3: error: 'function' has not been declared
  109 |   function<Vec(int)> init,
      |   ^~~~~~~~
a.cc:109:11: error: expected ',' or '...' before '<' token
  109 |   function<Vec(int)> init,
      |           ^
a.cc: In function 'lazy_segtree<Vec, Mat, VV, MV, MM> create_lazy_segtree(int, int)':
a.cc:115:48: error: 'init' was not declared in this scope; did you mean 'int'?
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                ^~~~
      |                                                int
a.cc:115:54: error: 'vec_ident_' was not declared in this scope
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                      ^~~~~~~~~~
a.cc:115:66: error: 'mat_ident_' was not declared in this scope
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                                  ^~~~~~~~~~
a.cc:115:78: error: 'vec_vec_' was not declared in this scope
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                                              ^~~~~~~~
a.cc:115:88: error: 'mat_vec_' was not declared in this scope
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                                                        ^~~~~~~~
a.cc:115:98: error: 'mat_mat_' was not declared in this scope
  115 |   return lazy_segtree<Vec, Mat, VV, MV, MM>(n, init, vec_ident_, mat_ident_, vec_vec_, mat_vec_, mat_mat_);
      |                                                                                                  ^~~~~~~~
a.cc: At global scope:
a.cc:118:28: error: 'function' has not been declared
  118 | auto lz_assign_plus(int n, function<pair<long long, long long>(int)> init) {
      |                            ^~~~~~~~
a.cc:118:36: error: expected ',' or '...' before '<' token
  118 | auto lz_assign_plus(int n, function<pair<long long, long long>(int)> init) {
      |                                    ^
a.cc: In function 'auto lz_assign_plus(int, int)':
a.cc:123:5: error: 'init' was not declared in this scope; did you mean 'int'?
  123 |     init,
      |     ^~~~
      |     int
a.cc:125:5: error: 'LLONG_MAX' was not declared in this scope
  125 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc:10:1: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    9 | #include <queue>
  +++ |+#include <climits>
   10 | 
a.cc: In lambda function:
a.cc:127:43: error: 'LLONG_MAX' is not captured
  127 |     [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : vec(a * b.second, b.second); },
      |                                           ^~~~~~~~~
a.cc:127:6: note: the lambda has no capture-default
  127 |     [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : vec(a * b.second, b.second); },
      |      ^
a.cc:125:5: note: '<typeprefixerror>LLONG_MAX' declared here
  125 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc: In lambda function:
a.cc:128:43: error: 'LLONG_MAX' is not captured
  128 |     [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
      |                                           ^~~~~~~~~
a.cc:128:6: note: the lambda has no capture-default
  128 |     [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
      |      ^
a.cc:125:5: note: '<typeprefixerror>LLONG_MAX' declared here
  125 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc: At global scope:
a.cc:131:27: error: 'function' has not been declared
  131 | auto lz_assign_min(int n, function<long long(int)> init) {
      |                           ^~~~~~~~
a.cc:131:35: error: expected ',' or '...' before '<' token
  131 | auto lz_assign_min(int n, function<long long(int)> init) {
      |                                   ^
a.cc: In function 'auto lz_assign_min(int, int)':
a.cc:136:5: error: 'init' was not declared in this scope; did you mean 'int'?
  136 |     init,
      |     ^~~~
      |     int
a.cc:137:5: error: 'LLONG_MAX' was not declared in this scope
  137 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc:137:5: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: At global scope:
a.cc:144:27: error: 'function' has not been declared
  144 | auto lz_assign_max(int n, function<long long(int)> init) {
      |                           ^~~~~~~~
a.cc:144:35: error: expected ',' or '...' before '<' token
  144 | auto lz_assign_max(int n, function<long long(int)> init) {
      |                                   ^
a.cc: In function 'auto lz_assign_max(int, int)':
a.cc:149:5: error: 'init' was not declared in this scope; did you mean 'int'?
  149 |     init,
      |     ^~~~
      |     int
a.cc:150:5: error: 'LLONG_MIN' was not declared in this scope
  150 |     LLONG_MIN,
      |     ^~~~~~~~~
a.cc:150:5: note: 'LLONG_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:151:5: error: 'LLONG_MAX' was not declared in this scope
  151 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc:151:5: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In lambda function:
a.cc:153:43: error: 'LLONG_MAX' is not captured
  153 |     [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : a; },
      |                                           ^~~~~~~~~
a.cc:153:6: note: the lambda has no capture-default
  153 |     [](mat a, vec b) -> vec { return a == LLONG_MAX ? b : a; },
      |      ^
a.cc:151:5: note: '<typeprefixerror>LLONG_MAX' declared here
  151 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc: In lambda function:
a.cc:154:43: error: 'LLONG_MAX' is not captured
  154 |     [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
      |                                           ^~~~~~~~~
a.cc:154:6: note: the lambda has no capture-default
  154 |     [](mat a, mat b) -> mat { return a == LLONG_MAX ? b : a; });
      |      ^
a.cc:151:5: note: '<typeprefixerror>LLONG_MAX' declared here
  151 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc: At global scope:
a.cc:157:25: error: 'function' has not been declared
  157 | auto lz_plus_min(int n, function<long long(int)> init) {
      |                         ^~~~~~~~
a.cc:157:33: error: expected ',' or '...' before '<' token
  157 | auto lz_plus_min(int n, function<long long(int)> init) {
      |                                 ^
a.cc: In function 'auto lz_plus_min(int, int)':
a.cc:162:5: error: 'init' was not declared in this scope; did you mean 'int'?
  162 |     init,
      |     ^~~~
      |     int
a.cc:163:5: error: 'LLONG_MAX' was not declared in this scope
  163 |     LLONG_MAX,
      |     ^~~~~~~~~
a.cc:163:5: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: At global scope:
a.cc:170:25: error: 'function' has not been declared
  170 | auto lz_plus_max(int n, function<long long(int)> init) {
      |                         ^~~~~~~~
a.cc:170:33: error: expected ',' or '...' before '<' token
  170 | auto lz_plus_max(int n, function<long long(int)> init) {
      |                                 ^
a.cc: In function 'auto lz_plus_max(int, int)':
a.cc:175:5: error: 'init' was not declared in this scope; did you mean 'int'?
  175 |     init,
      |     ^~~~
      |     int
a.cc:176:5: error: 'LLONG_MIN' was not declared in this scope
  176 |     LLONG_MIN,
      |     ^~~~~~~~~
a.cc:176:5: note: 'LLONG_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int main()':
a.cc:193:31: error: invalid user-defined conversion from 'main()::<lambda(int)>' to 'int' [-fpermissive]
  193 |   auto seg = lz_assign_min(N, [](int i) -> ll {
      |                               ^~~~~~~~~~~~~~~~~
  194 |     if (i == 0) return 0;
      |     ~~~~~~~~~~~~~~~~~~~~~      
  195 |     return INF;
      |     ~~~~~~~~~~~                
  196 |   });
      |   ~                            
a.cc:193:31: note: candidate is: 'constexpr main()::<lambda(int)>::operator ll (*)(int)() const' (near match)
  193 |   auto seg = lz_assign_min(N, [](int i) -> ll {
      |                               ^
a.cc:193:31: note:   no known conversion from 'll (*)(i