Problem p00339 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vi v(n + 1), h(n + 1);
  vi t(m), s(m);
  rep(i, n) cin >> v[i + 1] >> h[i + 1];
  rep(i, m) cin >> t[i] >> s[i];

  vi dh(n);
  rep(i, n - 1) dh[i + 1] = dh[i] + abs(h[i + 2] - h[i + 1]);
  rep(i, n) v[i + 1] += v[i];
  rep(i, n) h[i + 1] += h[i];

  vector<vi> dp(m + 1, vi(n + 1, -1));
  vector<vi> cost(n + 1, vi(n + 1, -inf));

  rep(i, n + 1) for (int j = i; j <= n; j++) {
    int x = h[j] - h[i];
    if (i < j)
      x += dh[j - 1] - dh[i];
    cost[i][j] = x;
  }

  vi L(m + 1), R(m + 1, -1);
  R[0] = 0;
  dp[0][0] = 0;

  rep(i, m) {
    int nL = n + 1, nR = -1;
    dp[i + 1] = dp[i];

    for (int j = L[i]; j <= R[i]; j++) {

      int l = j - 1, r = n, mid;
      if (dp[i][j] + cost[j][n] < s[i])
        continue;
      while (l + 1 < r) {
        mid = (l + r) / 2;
        if (dp[i][j] + cost[j][mid] < s[i])
          l = mid;
        else
          r = mid;
      }
      nL = min(nL, r);
      nR = max(nR, r);

      if (i && dp[i - 1][j] == dp[i][j] && L[i - 1] <= j && j <= R[i - 1]) {
        r = R[i] + 1;
      }

      for (int k = r; k <= n && v[k] <= t[i]; k++) {
        int str = dp[i][j] + cost[j][k];
        nR = max(nR, k);

        if (dp[i + 1][k] < str) {
          /*
          if(k < check){
                  dbg(i, j, k, check, str);
                  dbg(dp[i-1]);
                  dbg(dp[i]);
                  dbg(dp[i+1]);
          }
          assert(k >= check);
          */
          dp[i + 1][k] = str;
        }
      }
    }
    L[i + 1] = nL;
    R[i + 1] = nR;
    rep(j, nL) dp[i + 1][j] = -1;
  }
  // rep(i, m + 1) dbg(i, dp[i]);

  int ans = -1;
  for (int i = L[m]; i <= R[m]; i++)
    if (dp[m][i] >= 0) {
      ans = i;
      break;
    }
  if (ans < 0)
    cout << -1 << endl;
  else
    cout << t[m - 1] - v[ans] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << __LINE__ << ": ";                                                  \\
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \\
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vi v(n + 1), h(n + 1);
  vi t(m), s(m);
  rep(i, n) cin >> v[i + 1] >> h[i + 1];
  rep(i, m) cin >> t[i] >> s[i];

  vi dh(n);
  rep(i, n - 1) dh[i + 1] = dh[i] + abs(h[i + 2] - h[i + 1]);
  rep(i, n) v[i + 1] += v[i];
  rep(i, n) h[i + 1] += h[i];

  vector<vi> dp(m + 1, vi(n + 1, -1));
  vector<vi> cost(n + 1, vi(n + 1, -inf));

  rep(i, n + 1) for (int j = i; j <= n; j++) {
    int x = h[j] - h[i];
    if (i < j)
      x += dh[j - 1] - dh[i];
    cost[i][j] = x;
  }

  vi L(m + 1), R(m + 1, -1);
  R[0] = 0;
  dp[0][0] = 0;

  rep(i, m) {
    int nL = n + 1, nR = -1;
    dp[i + 1] = dp[i];

    for (int j = L[i]; j <= R[i]; j++) {

      int l = j - 1, r = n, mid;
      if (dp[i][j] + cost[j][n] < s[i])
        continue;
      while (l + 1 < r) {
        mid = (l + r) / 2;
        if (dp[i][j] + cost[j][mid] < s[i])
          l = mid;
        else
          r = mid;
      }
      nL = min(nL, r);
      nR = max(nR, r);

      int check = r;
      if (i && dp[i - 1][j] == dp[i][j] && L[i - 1] <= j && j <= R[i - 1]) {
        // r = R[i] + 1;
        check = R[i] + 1;
      }

      for (int k = r; k <= n && v[k] <= t[i]; k++) {
        int str = dp[i][j] + cost[j][k];
        nR = max(nR, k);

        if (dp[i + 1][k] < str) {
          /*
          if(k < check){
                  dbg(i, j, k, check, str);
                  dbg(dp[i-1]);
                  dbg(dp[i]);
                  dbg(dp[i+1]);
          }
          */
          assert(k >= check);
          dp[i + 1][k] = str;
        }
      }
    }
    L[i + 1] = nL;
    R[i + 1] = nR;
    rep(j, nL) dp[i + 1][j] = -1;
  }
  // rep(i, m + 1) dbg(i, dp[i]);

  int ans = -1;
  for (int i = L[m]; i <= R[m]; i++)
    if (dp[m][i] >= 0) {
      ans = i;
      break;
    }
  if (ans < 0)
    cout << -1 << endl;
  else
    cout << t[m - 1] - v[ans] << endl;
  return 0;
}