Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

const long long TINF = 100000000000000000ll;

template <class T> class segmintree {
private:
  T ragin(int i, int j, int k, int a, int b) {
    return a <= j && k <= b   ? seg[i]
           : k <= a || b <= j ? TINF
                              : min(ragin(2 * i, j, (j + k) / 2, a, b),
                                    ragin(2 * i + 1, (j + k) / 2, k, a, b));
  }

public:
  T *seg;
  int S;
  segmintree(int n) {
    S = 0;
    while ((1 << S) <= n)
      S++;
    seg = (T *)calloc(sizeof(T), 1 << (S + 1));
    for (int i = 0; i < 1 << (S + 1); i++) {
      seg[i + (1 << S)] = TINF;
    }
  }
  void set(int i, T k) {
    int p = i + (1 << S);
    seg[p] = k;
    while (p > 1) {
      p /= 2;
      seg[p] = min(seg[2 * p], seg[2 * p + 1]);
    }
  }
  T rag(int a, int b) { return ragin(1, 0, 1 << S, a, b); }
  T val(int i) {
    int p = i + (1 << S);
    return seg[p];
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> L(m), R(m);
  vector<vector<int>> E(n);
  vector<long long> C(m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &L[i], &R[i], &C[i]);
    L[i]--, R[i]--;
    E[R[i]].push_back(i);
  }
  segmintree<long long> seg(n + 1);
  seg.set(0, 0);
  for (int i = 1; i < n; i++) {
    long long V = TINF;
    for (int x = 0; x < E[i].size(); x++) {
      int j = E[i][x];
      V = min(V, seg.rag(L[j], i) + C[j]);
      // printf("%d %d %lld\n",L[j],i,seg.rag(L[j],i)+C[j]);
    }
    seg.set(i, V);
  }
  long long ans = seg.val(n - 1);
  printf("%lld\n", ans == TINF ? -1 : ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

const long long TINF = 100000000000000000ll;

template <class T> class segmintree {
private:
  T ragin(int i, int j, int k, int a, int b) {
    return a <= j && k <= b   ? seg[i]
           : k <= a || b <= j ? TINF
                              : min(ragin(2 * i, j, (j + k) / 2, a, b),
                                    ragin(2 * i + 1, (j + k) / 2, k, a, b));
  }

public:
  T *seg;
  int S;
  segmintree(int n) {
    S = 0;
    while ((1 << S) <= n)
      S++;
    seg = (T *)calloc(sizeof(T), 1 << (S + 1));
    for (int i = 0; i < 1 << (S + 1); i++) {
      seg[i] = TINF;
    }
  }
  void set(int i, T k) {
    int p = i + (1 << S);
    seg[p] = k;
    while (p > 1) {
      p /= 2;
      seg[p] = min(seg[2 * p], seg[2 * p + 1]);
    }
  }
  T rag(int a, int b) { return ragin(1, 0, 1 << S, a, b); }
  T val(int i) {
    int p = i + (1 << S);
    return seg[p];
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> L(m), R(m);
  vector<vector<int>> E(n);
  vector<long long> C(m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &L[i], &R[i], &C[i]);
    L[i]--, R[i]--;
    E[R[i]].push_back(i);
  }
  segmintree<long long> seg(n + 1);
  seg.set(0, 0);
  for (int i = 1; i < n; i++) {
    long long V = TINF;
    for (int x = 0; x < E[i].size(); x++) {
      int j = E[i][x];
      V = min(V, seg.rag(L[j], i) + C[j]);
      // printf("%d %d %lld\n",L[j],i,seg.rag(L[j],i)+C[j]);
    }
    seg.set(i, V);
  }
  long long ans = seg.val(n - 1);
  printf("%lld\n", ans == TINF ? -1 : ans);
  return 0;
}