Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

using ll = long long;

constexpr int N = 100010;

// https://smijake3.hatenablog.com/entry/2019/04/28/021457#%E5%8C%BA%E9%96%93chmin%E3%82%AF%E3%82%A8%E3%83%AARMQRSQ%E3%82%92%E5%87%A6%E7%90%86%E3%81%99%E3%82%8B%E5%AE%9F%E8%A3%85
class SegmentTree {
  const ll inf = 1e18;
  int n, n0;
  ll max_v[4 * N], smax_v[4 * N];
  ll sum[4 * N], max_c[4 * N];

  void update_node_max(int k, ll x) {
    sum[k] += (x - max_v[k]) * max_c[k];
    max_v[k] = x;
  }

  void push(int k) {
    if (max_v[k] < max_v[2 * k + 1]) {
      update_node_max(2 * k + 1, max_v[k]);
    }
    if (max_v[k] < max_v[2 * k + 2]) {
      update_node_max(2 * k + 2, max_v[k]);
    }
  }

  void update(int k) {
    sum[k] = sum[2 * k + 1] + sum[2 * k + 2];

    if (max_v[2 * k + 1] < max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 2];
      max_c[k] = max_c[2 * k + 2];
      smax_v[k] = max(max_v[2 * k + 1], smax_v[2 * k + 2]);
    } else if (max_v[2 * k + 1] > max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1];
      smax_v[k] = max(smax_v[2 * k + 1], max_v[2 * k + 2]);
    } else {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1] + max_c[2 * k + 2];
      smax_v[k] = max(smax_v[2 * k + 1], smax_v[2 * k + 2]);
    }
  }

  void _update_min(ll x, int a, int b, int k, int l, int r) {
    if (b <= l || r <= a || max_v[k] <= x) {
      return;
    }
    if (a <= l && r <= b && smax_v[k] < x) {
      update_node_max(k, x);
      return;
    }

    push(k);
    _update_min(x, a, b, 2 * k + 1, l, (l + r) / 2);
    _update_min(x, a, b, 2 * k + 2, (l + r) / 2, r);
    update(k);
  }

  ll _query_max(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return max_v[k];
    }
    push(k);
    ll lv = _query_max(a, b, 2 * k + 1, l, (l + r) / 2);
    ll rv = _query_max(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(lv, rv);
  }

  ll _query_sum(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return sum[k];
    }
    push(k);
    ll lv = _query_sum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll rv = _query_sum(a, b, 2 * k + 2, (l + r) / 2, r);
    return lv + rv;
  }

public:
  SegmentTree(int n) : n(n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;

    for (int i = 0; i < n; ++i) {
      max_v[n0 - 1 + i] = sum[n0 - 1 + i] = (i == 0 ? 0 : inf);
      smax_v[n0 - 1 + i] = -inf;
      max_c[n0 - 1 + i] = 1;
    }

    for (int i = n; i < n0; ++i) {
      max_v[n0 - 1 + i] = smax_v[n0 - 1 + i] = -inf;
      sum[n0 - 1 + i] = max_c[n0 - 1 + i] = 0;
    }
    for (int i = n0 - 2; i >= 0; i--)
      update(i);
  }

  // range minimize query
  void update_min(int a, int b, ll x) { return _update_min(x, a, b, 0, 0, n0); }

  // range maximum query
  ll query_max(int a, int b) { return _query_max(a, b, 0, 0, n0); }

  // range sum query
  ll query_sum(int a, int b) { return _query_sum(a, b, 0, 0, n0); }
};

constexpr int MAX_N = N;
using P = pair<ll, ll>;

SegmentTree st(MAX_N);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<ll> l(m), r(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
  }
  vector<int> index(n);
  iota(index.begin(), index.end(), 0);
  sort(index.begin(), index.end(), [&](const int &lhs, const int &rhs) {
    return P(l[lhs], r[lhs]) < P(l[rhs], r[rhs]);
  });
  for (int i = 0; i < m; i++) {
    ll lx = l[index[i]], rx = r[index[i]];
    ll cur = st.query_sum(lx, lx + 1);
    st.update_min(lx, rx, cur + c[index[i]]);
  }
  ll ret = st.query_sum(n - 1, n);
  cout << (ret == 1e18 ? -1 : ret) << endl;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;

constexpr int N = 100010;

// https://smijake3.hatenablog.com/entry/2019/04/28/021457#%E5%8C%BA%E9%96%93chmin%E3%82%AF%E3%82%A8%E3%83%AARMQRSQ%E3%82%92%E5%87%A6%E7%90%86%E3%81%99%E3%82%8B%E5%AE%9F%E8%A3%85
class SegmentTree {
  const ll inf = 1e18;
  int n, n0;
  ll max_v[4 * N], smax_v[4 * N];
  ll sum[4 * N], max_c[4 * N];

  void update_node_max(int k, ll x) {
    sum[k] += (x - max_v[k]) * max_c[k];
    max_v[k] = x;
  }

  void push(int k) {
    if (max_v[k] < max_v[2 * k + 1]) {
      update_node_max(2 * k + 1, max_v[k]);
    }
    if (max_v[k] < max_v[2 * k + 2]) {
      update_node_max(2 * k + 2, max_v[k]);
    }
  }

  void update(int k) {
    sum[k] = sum[2 * k + 1] + sum[2 * k + 2];

    if (max_v[2 * k + 1] < max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 2];
      max_c[k] = max_c[2 * k + 2];
      smax_v[k] = max(max_v[2 * k + 1], smax_v[2 * k + 2]);
    } else if (max_v[2 * k + 1] > max_v[2 * k + 2]) {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1];
      smax_v[k] = max(smax_v[2 * k + 1], max_v[2 * k + 2]);
    } else {
      max_v[k] = max_v[2 * k + 1];
      max_c[k] = max_c[2 * k + 1] + max_c[2 * k + 2];
      smax_v[k] = max(smax_v[2 * k + 1], smax_v[2 * k + 2]);
    }
  }

  void _update_min(ll x, int a, int b, int k, int l, int r) {
    if (b <= l || r <= a || max_v[k] <= x) {
      return;
    }
    if (a <= l && r <= b && smax_v[k] < x) {
      update_node_max(k, x);
      return;
    }

    push(k);
    _update_min(x, a, b, 2 * k + 1, l, (l + r) / 2);
    _update_min(x, a, b, 2 * k + 2, (l + r) / 2, r);
    update(k);
  }

  ll _query_max(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return max_v[k];
    }
    push(k);
    ll lv = _query_max(a, b, 2 * k + 1, l, (l + r) / 2);
    ll rv = _query_max(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(lv, rv);
  }

  ll _query_sum(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return 0;
    }
    if (a <= l && r <= b) {
      return sum[k];
    }
    push(k);
    ll lv = _query_sum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll rv = _query_sum(a, b, 2 * k + 2, (l + r) / 2, r);
    return lv + rv;
  }

public:
  SegmentTree(int n) : n(n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;

    for (int i = 0; i < n; ++i) {
      max_v[n0 - 1 + i] = sum[n0 - 1 + i] = (i == 0 ? 0 : inf);
      smax_v[n0 - 1 + i] = -inf;
      max_c[n0 - 1 + i] = 1;
    }

    for (int i = n; i < n0; ++i) {
      max_v[n0 - 1 + i] = smax_v[n0 - 1 + i] = -inf;
      sum[n0 - 1 + i] = max_c[n0 - 1 + i] = 0;
    }
    for (int i = n0 - 2; i >= 0; i--)
      update(i);
  }

  // range minimize query
  void update_min(int a, int b, ll x) { return _update_min(x, a, b, 0, 0, n0); }

  // range maximum query
  ll query_max(int a, int b) { return _query_max(a, b, 0, 0, n0); }

  // range sum query
  ll query_sum(int a, int b) { return _query_sum(a, b, 0, 0, n0); }
};

constexpr int MAX_N = N;
using P = pair<ll, ll>;

SegmentTree st(MAX_N);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<ll> l(m), r(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
  }
  vector<int> index(m);
  iota(index.begin(), index.end(), 0);
  sort(index.begin(), index.end(), [&](const int &lhs, const int &rhs) {
    return P(l[lhs], r[lhs]) < P(l[rhs], r[rhs]);
  });
  for (int i = 0; i < m; i++) {
    ll lx = l[index[i]], rx = r[index[i]];
    ll cur = st.query_sum(lx, lx + 1);
    st.update_min(lx, rx, cur + c[index[i]]);
  }
  ll ret = st.query_sum(n - 1, n);
  cout << (ret == 1e18 ? -1 : ret) << endl;
}