Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<pair<int, int>, ll> P;
const ll MOD = 1e9 + 7;

struct segtree {
  int N;
  ll dat[2 * 100000];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = 1e14 - 1;
  }
  // update k th element
  void update(int k, ll a) {
    k += N - 1; // leaf
    dat[k] = min(a, dat[k]);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // min [a, b)
  ll query(int a, int b) { return query(a, b, 0, 0, N); }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 1e14 - 1;
    if (a <= l && r <= b)
      return dat[k];
    int m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
  ll get(int i) { return dat[i + N - 1]; }
};

// bool operator<(P &lhs,P &rhs)
// {
// 	if(lhs.first.first==rhs.first.first)
// 		return lhs.second<rhs.second;
// 	else
// 		return lhs.first<rhs.first;
// }
P p[100000];
int main() {
  int n, m;
  ll ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    l--;
    r--;
    p[i] = P(make_pair(l, r), c);
  }
  sort(p, p + m);
  segtree t = segtree(n);
  t.update(0, 0);
  for (int i = 0; i < m; i++) {
    // cerr<<p[i].first.first<<endl;
    ll s = t.query(p[i].first.first, p[i].first.second + 1);
    if (s == 1e14 - 1)
      continue;
    t.update(p[i].first.second, s + p[i].second);
  }
  ans = t.get(n - 1);
  if (ans == 1e14 - 1)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<pair<int, int>, ll> P;
const ll MOD = 1e9 + 7;

struct segtree {
  int N;
  ll dat[2 * 400000];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = 1e14 - 1;
  }
  // update k th element
  void update(int k, ll a) {
    k += N - 1; // leaf
    dat[k] = min(a, dat[k]);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // min [a, b)
  ll query(int a, int b) { return query(a, b, 0, 0, N); }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 1e14 - 1;
    if (a <= l && r <= b)
      return dat[k];
    int m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
  ll get(int i) { return dat[i + N - 1]; }
};

// bool operator<(P &lhs,P &rhs)
// {
// 	if(lhs.first.first==rhs.first.first)
// 		return lhs.second<rhs.second;
// 	else
// 		return lhs.first<rhs.first;
// }
P p[100000];
int main() {
  int n, m;
  ll ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    l--;
    r--;
    p[i] = P(make_pair(l, r), c);
  }
  sort(p, p + m);
  segtree t = segtree(n);
  t.update(0, 0);
  for (int i = 0; i < m; i++) {
    // cerr<<p[i].first.first<<endl;
    ll s = t.query(p[i].first.first, p[i].first.second + 1);
    if (s == 1e14 - 1)
      continue;
    t.update(p[i].first.second, s + p[i].second);
  }
  ans = t.get(n - 1);
  if (ans == 1e14 - 1)
    ans = -1;
  cout << ans << endl;
  return 0;
}