Problem p03859 (C++)

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

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 301
#define MOD 1000000007
#define int ll
int N, M;
string S;

int A[maxn];

int dp[maxn][maxn];
int ss[maxn];

inline pi inter(pi i, pi j) { return pi(max(i.f, j.f), min(i.s, j.s)); }
inline int sz(pi i) { return i.s - i.f + 1; }

inline int qry(pi i) {
  if (i.f == 0)
    return ss[i.s];
  return ss[i.s] - ss[i.f - 1];
}

int dpf(int i, int j, int k) {
  // cout<<i<<' '<<j<<' '<<k<<'\n';
  if (k > sz(pi(i, j)))
    return 0;
  if (k < 0)
    return 0;
  if (A[i] > j) {
    k += qry(pi(j + 1, A[i]));
    j = A[i];
  }

  if (dp[i][k] != -1)
    return dp[i][k];

  if (i == N)
    return 1;

  if (k > 0)
    return dp[i][k] = (dpf(i + 1, j, k - 1) + dpf(i + 1, j, k)) % MOD;
  else
    return dp[i][k] = dpf(i + 1, j, k);
}

int32_t main() {
  fast;

  cin >> N >> M >> S;

  stack<pi> st;

  FOR(i, 0, M - 1) {
    int a, b;
    cin >> a >> b;

    while (!st.empty() && sz(inter(st.top(), pi(a, b))) == sz(st.top()))
      st.pop();

    if (!st.empty() && sz(inter(st.top(), pi(a, b))) == sz(pi(a, b)))
      continue;
    st.push(pi(a, b));
  }

  M = st.size();
  FOR(i, 0, N - 1) A[i] = i;

  DEC(i, M - 1, 0) {
    A[st.top().f - 1] = st.top().s - 1;
    st.pop();
  }

  if (S[0] == '1')
    ss[0] = 1;
  else
    ss[0] = 0;

  FOR(i, 1, N - 1) {
    if (S[i] == '1')
      ss[i] = ss[i - 1] + 1;
    else
      ss[i] = ss[i - 1];
  }
  mem(dp, -1);

  cout << dpf(0, A[0], qry(pi(0, A[0])));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 3001
#define MOD 1000000007
#define int ll
int N, M;
string S;

int A[maxn];

int dp[maxn][maxn];
int ss[maxn];

inline pi inter(pi i, pi j) { return pi(max(i.f, j.f), min(i.s, j.s)); }
inline int sz(pi i) { return i.s - i.f + 1; }

inline int qry(pi i) {
  if (i.f == 0)
    return ss[i.s];
  return ss[i.s] - ss[i.f - 1];
}

int dpf(int i, int j, int k) {
  // cout<<i<<' '<<j<<' '<<k<<'\n';
  if (k > sz(pi(i, j)))
    return 0;
  if (k < 0)
    return 0;
  if (A[i] > j) {
    k += qry(pi(j + 1, A[i]));
    j = A[i];
  }

  if (dp[i][k] != -1)
    return dp[i][k];

  if (i == N)
    return 1;

  if (k > 0)
    return dp[i][k] = (dpf(i + 1, j, k - 1) + dpf(i + 1, j, k)) % MOD;
  else
    return dp[i][k] = dpf(i + 1, j, k);
}

int32_t main() {
  fast;

  cin >> N >> M >> S;

  stack<pi> st;

  FOR(i, 0, M - 1) {
    int a, b;
    cin >> a >> b;

    while (!st.empty() && sz(inter(st.top(), pi(a, b))) == sz(st.top()))
      st.pop();

    if (!st.empty() && sz(inter(st.top(), pi(a, b))) == sz(pi(a, b)))
      continue;
    st.push(pi(a, b));
  }

  M = st.size();
  FOR(i, 0, N - 1) A[i] = i;

  DEC(i, M - 1, 0) {
    A[st.top().f - 1] = st.top().s - 1;
    st.pop();
  }

  if (S[0] == '1')
    ss[0] = 1;
  else
    ss[0] = 0;

  FOR(i, 1, N - 1) {
    if (S[i] == '1')
      ss[i] = ss[i - 1] + 1;
    else
      ss[i] = ss[i - 1];
  }
  mem(dp, -1);

  cout << dpf(0, A[0], qry(pi(0, A[0])));
}