Problem p02868 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define dbg(S)                                                                 \
  {                                                                            \
    int f = 1;                                                                 \
    for (auto x : S)                                                           \
      cerr << (f ? "[" : ", ") << x, f = 0;                                    \
    cerr << "]\n";                                                             \
  }

using namespace std;
using lint = long long;

const lint INF = 1LL << 61;

template <class T> class segment_tree {
  static const int N_MAX = 1e5 + 10;
  int n;
  T dat[2 * N_MAX];

  T query(int l, int r, int a, int b, int u) {
    if (l <= a && b <= r)
      return dat[u];

    T res = INF;
    int c = (a + b + 1) / 2;
    if (l < c && a < r)
      res = min(res, query(l, r, a, c, 2 * u));
    if (l < b && c < r)
      res = min(res, query(l, r, c, b, 2 * u + 1));
    return res;
  }

public:
  void build(int N, T val) {
    for (n = 1; n < N; n *= 2)
      ;
    rep(u, 2 * n) dat[u] = val;
  }

  void update(int u, T v) {
    u += n;
    dat[u] = min(dat[u], v);
    for (u /= 2; u >= 1; u /= 2)
      dat[u] = min(dat[2 * u], dat[2 * u + 1]);
  }

  T query(int l, int r) { return query(l, r, 0, n, 1); }
};

struct interval {
  int l, r;
  lint wt;
  bool operator<(const interval &I) const { return r < I.r; }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<interval> I(m);
  rep(i, m) {
    scanf("%d%d%lld", &I[i].l, &I[i].r, &I[i].wt);
    I[i].l--;
  }

  sort(I.begin(), I.end());

  segment_tree<lint> S;
  S.build(n, INF);
  S.update(0, 0);
  rep(i, m) {
    lint d = S.query(I[i].l, I[i].r);
    S.update(I[i].r - 1, d + I[i].wt);
  }

  lint ans = S.query(n - 1, n);
  printf("%lld\n", ans < INF ? ans : -1);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define dbg(S)                                                                 \
  {                                                                            \
    int f = 1;                                                                 \
    for (auto x : S)                                                           \
      cerr << (f ? "[" : ", ") << x, f = 0;                                    \
    cerr << "]\n";                                                             \
  }

using namespace std;
using lint = long long;

const lint INF = 1LL << 61;

template <class T> class segment_tree {
  static const int N_MAX = 1e5 + 10;
  int n;
  T dat[4 * N_MAX];

  T query(int l, int r, int a, int b, int u) {
    if (l <= a && b <= r)
      return dat[u];

    T res = INF;
    int c = (a + b + 1) / 2;
    if (l < c && a < r)
      res = min(res, query(l, r, a, c, 2 * u));
    if (l < b && c < r)
      res = min(res, query(l, r, c, b, 2 * u + 1));
    return res;
  }

public:
  void build(int N, T val) {
    for (n = 1; n < N; n *= 2)
      ;
    rep(u, 2 * n) dat[u] = val;
  }

  void update(int u, T v) {
    u += n;
    dat[u] = min(dat[u], v);
    for (u /= 2; u >= 1; u /= 2)
      dat[u] = min(dat[2 * u], dat[2 * u + 1]);
  }

  T query(int l, int r) { return query(l, r, 0, n, 1); }
};

struct interval {
  int l, r;
  lint wt;
  bool operator<(const interval &I) const { return r < I.r; }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<interval> I(m);
  rep(i, m) {
    scanf("%d%d%lld", &I[i].l, &I[i].r, &I[i].wt);
    I[i].l--;
  }

  sort(I.begin(), I.end());

  segment_tree<lint> S;
  S.build(n, INF);
  S.update(0, 0);
  rep(i, m) {
    lint d = S.query(I[i].l, I[i].r);
    S.update(I[i].r - 1, d + I[i].wt);
  }

  lint ans = S.query(n - 1, n);
  printf("%lld\n", ans < INF ? ans : -1);

  return 0;
}