Problem p00339 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define MP make_pair
#define INF 1e9
#define F first
#define S second
using namespace std;
typedef long long LL;
#define int LL
typedef pair<int, int> P;
int N, M, n, ans = -1;
vector<int> v, h, t, s, vrsk;
int dp[1010][3010][2];
signed main() {
  cin >> N >> M;
  v.resize(N);
  h.resize(N);
  t.resize(M);
  s.resize(M);
  vrsk.resize(N);
  REP(i, N)
  cin >> v[i] >> h[i];
  REP(i, M)
  cin >> t[i] >> s[i];
  vrsk[0] = v[0];
  FOR(i, 1, N) { vrsk[i] = vrsk[i - 1] + v[i]; }
  dp[0][0][1] = h[0];
  FOR(i, 1, N) {
    if (t[0] >= vrsk[i])
      dp[0][i][1] = dp[0][i - 1][1] + h[i] + abs(h[i] - h[i - 1]);
  }
  REP(i, N) {
    if (dp[0][i][0] < s[0])
      dp[0][i][0] = -1;
    if (dp[0][i][1] < s[0])
      dp[0][i][1] = -1;
  }
  FOR(i, 1, M) {
    REP(j, N)
    if (t[i] >= vrsk[j])
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
    FOR(j, 1, N)
    if (t[i] >= vrsk[j])
      dp[i][j][1] = max(dp[i][j - 1][0] + h[j],
                        dp[i][j - 1][1] + h[j] + abs(h[j] - h[j - 1]));
    REP(j, N) {
      if (dp[i][j][0] < s[i])
        dp[i][j][0] = -1;
      if (dp[i][j][1] < s[i])
        dp[i][j][1] = -1;
    }
  }
  /*REP(i, M)
  {
          REP(j, N)
                  cout << dp[i][j][0] << " ";
          cout << endl;
  }
  cout << endl;
  REP(i, M)
  {
          REP(j, N)
                  cout << dp[i][j][1] << " ";
          cout << endl;
  }*/
  REP(i, N) {
    if (dp[M - 1][i][0] > 0 || dp[M - 1][i][1] > 0) {
      ans = t[M - 1] - vrsk[i];
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define MP make_pair
#define INF 1e9
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
int N, M, n, ans = -1;
vector<int> v, h, t, s;
int dp[1010][3010], vrsk[(1 << 15) - 1], hrsk[(1 << 15) - 1];
P bonus[(1 << 15) - 1];
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    bonus[i] = MP(INT_MAX, INT_MAX);
}
void update(int k, int a) {
  k += n - 1;
  bonus[k] = MP(a, k - n + 1);
  while (k > 0) {
    k = (k - 1) / 2;
    bonus[k] = min(bonus[k * 2 + 1], bonus[k * 2 + 2]);
  }
}

P query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return MP(INT_MAX, INT_MAX);
  if (a <= l && r <= b)
    return bonus[k];
  else {
    P vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    P vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
int main() {

  cin >> N >> M;
  v.resize(N);
  h.resize(N);
  t.resize(M);
  s.resize(M);
  init(N);
  REP(i, N) { cin >> v[i] >> h[i]; }
  REP(i, M) { cin >> t[i] >> s[i]; }
  REP(i, N - 1) { update(i, abs(h[i] - h[i + 1])); }
  vrsk[0] = v[0];
  FOR(i, 1, N) { vrsk[i] = vrsk[i - 1] + v[i]; }
  hrsk[0] = h[0];
  FOR(i, 1, N) { hrsk[i] = hrsk[i - 1] + h[i] + abs(h[i] - h[i - 1]); }
  int jstart = 0, jend = 0;
  REP(i, N) { dp[0][i] = hrsk[i]; }
  FOR(i, 1, M) {
    P p = MP(0, 0);
    if (jstart != 0 && jend != 0) {
      p = query(jstart, jend, 0, 0, n);
    }
    FOR(j, jstart, N) {
      if (j == jstart) {
        jstart = 0;
      }
      if (vrsk[j] > t[i]) {
        jend = j;
        break;
      }
      // cout << i <<" ";
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      dp[i][j] = max(dp[i][j], dp[i - 1][p.S] + hrsk[j] - hrsk[p.S] - p.F);
      if (dp[i][j] >= s[i] && !jstart) {
        jstart = j;
      }
    }
  }
  REP(i, N) {
    if (dp[M - 1][i] >= s[M - 1]) {
      ans = t[M - 1] - vrsk[i];
      break;
    }
  }
  /*REP(i, M)
  {
          REP(j, N)
                  cout << dp[i][j] << " ";
          cout << endl;
  }*/
  cout << ans << endl;
  return 0;
}