Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
  return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <class T, class... Tail>
void tiedSort(vector<T> &a, vector<Tail> &...tail) {
  int n = a.size();
  using S = tuple<T, Tail...>;
  vector<S> s(n);
  for (int i = 0; i < n; i++)
    s[i] = make_tuple(a[i], tail[i]...);
  sort(s.begin(), s.end());
  for (int i = 0; i < n; i++)
    tie(a[i], tail[i]...) = s[i];
}

struct RangeUpdateQuery {
  int n;
  vector<int> d;
  vector<int> timestamp;
  int time;
  RangeUpdateQuery(int m) {
    for (n = 1; n < m; n *= 2)
      ;
    d.assign(n * 2, INF);
    time = 0;
    timestamp.assign(n * 2, 0);
  }
  void update(int l, int r, int x) {
    time++;
    for (; l && l + (l & -l) <= r; l += l & -l) {
      d[(n + l) / (l & -l)] = x;
      timestamp[(n + l) / (l & -l)] = time;
    }
    for (; l < r; r -= r & -r) {
      d[(n + r) / (r & -r) - 1] = x;
      timestamp[(n + r) / (r & -r) - 1] = time;
    }
  }
  int get(int i) {
    int ret = d[n + i];
    int max_time = timestamp[n + i];
    for (int j = (n + i) / 2; j > 0; j >>= 1) {
      if (max_time > timestamp[j])
        continue;
      max_time = timestamp[j];
      ret = d[j];
    }
    return ret;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M), C(M);
  rep(i, 0, M) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
  }

  tiedSort(L, R, C);

  RangeUpdateQuery ruq(N);
  ruq.update(0, 1, 0);

  rep(i, 0, M) {
    dump(i, L[i], R[i], C[i]);
    int mini = ruq.get(L[i]);
    if (mini == INF) {
      cout << -1 << endl;
      return 0;
    }
    int c = mini + C[i];

    auto f = [&](int x) { return ruq.get(x) > c; };
    auto binarySearch = [&](int ng, int ok) {
      if (f(ng))
        return ng;
      while (ng + 1 < ok) {
        int m = (ng + ok) / 2;
        if (f(m))
          ok = m;
        else
          ng = m;
      }
      return ok;
    };

    int l = binarySearch(L[i], N);
    dump(l);
    dump(ruq.get(l));

    vector<int> tmp(N);
    rep(i, 0, N) { tmp[i] = ruq.get(i); }
    dump(tmp);

    ruq.update(l, R[i], c);
  }
  int ans = ruq.get(N - 1);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
  return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <class T, class... Tail>
void tiedSort(vector<T> &a, vector<Tail> &...tail) {
  int n = a.size();
  using S = tuple<T, Tail...>;
  vector<S> s(n);
  for (int i = 0; i < n; i++)
    s[i] = make_tuple(a[i], tail[i]...);
  sort(s.begin(), s.end());
  for (int i = 0; i < n; i++)
    tie(a[i], tail[i]...) = s[i];
}

struct RangeUpdateQuery {
  int n;
  vector<int> d;
  vector<int> timestamp;
  int time;
  RangeUpdateQuery(int m) {
    for (n = 1; n < m; n *= 2)
      ;
    d.assign(n * 2, INF);
    time = 0;
    timestamp.assign(n * 2, 0);
  }
  void update(int l, int r, int x) {
    time++;
    for (; l && l + (l & -l) <= r; l += l & -l) {
      d[(n + l) / (l & -l)] = x;
      timestamp[(n + l) / (l & -l)] = time;
    }
    for (; l < r; r -= r & -r) {
      d[(n + r) / (r & -r) - 1] = x;
      timestamp[(n + r) / (r & -r) - 1] = time;
    }
  }
  int get(int i) {
    int ret = d[n + i];
    int max_time = timestamp[n + i];
    for (int j = (n + i) / 2; j > 0; j >>= 1) {
      if (max_time > timestamp[j])
        continue;
      max_time = timestamp[j];
      ret = d[j];
    }
    return ret;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M), C(M);
  rep(i, 0, M) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
  }

  tiedSort(L, R, C);

  RangeUpdateQuery ruq(N);
  ruq.update(0, 1, 0);

  rep(i, 0, M) {
    dump(i, L[i], R[i], C[i]);
    int mini = ruq.get(L[i]);
    if (mini == INF) {
      cout << -1 << endl;
      return 0;
    }
    int c = mini + C[i];

    auto f = [&](int x) { return ruq.get(x) > c; };
    auto binarySearch = [&](int ng, int ok) {
      if (f(ng))
        return ng;
      while (ng + 1 < ok) {
        int m = (ng + ok) / 2;
        if (f(m))
          ok = m;
        else
          ng = m;
      }
      return ok;
    };

    int l = binarySearch(L[i], N);
    dump(l);
    dump(ruq.get(l));

    // vector<int> tmp(N);
    // rep(i, 0, N) {
    //	tmp[i] = ruq.get(i);
    // }
    // dump(tmp);

    ruq.update(l, R[i], c);
  }
  int ans = ruq.get(N - 1);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}