Problem p03717 (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

int dp[maxn][2][maxn][maxn];

int mn[4][maxn], mx[4][maxn];

int N, M;
vi vect;

int dpf(int idx, int prevC, int prev, int prev2) {
  if (dp[idx][prevC][prev][prev2] != -1)
    return dp[idx][prevC][prev][prev2];

  if (idx > N)
    return 1;
  int R, G, B;
  if (prevC == 1) {
    R = idx - 1;
    G = prev;
    B = prev2;
  }
  if (prevC == 2) {
    R = prev;
    G = idx - 1;
    B = prev2;
  }
  if (prevC == 3) {
    R = prev;
    G = prev2;
    B = idx - 1;
  }

  if (prevC == 0) {
    R = G = B = 0;
  }
  vect.clear();
  vect.pb(R), vect.pb(G), vect.pb(B);

  vi poss;
  // cout<<idx<<' '<<R<<' '<<G<<' '<<B<<'\n';

  bool yes;
  FOR(i, 1, 3) {
    yes = 1;
    FOR(j, 1, 3) {
      if (mx[j][idx] == -1)
        continue;
      int num = 0;
      // num of colors from mx[j][idx] to idx-1

      aFOR(k, vect) if (k != 0 && mx[j][idx] <= k) num++;

      if (vect[i - 1] != 0 && mx[j][idx] > vect[i - 1])
        num++;
      if (vect[i - 1] == 0)
        num++;

      if (num != j)
        yes = 0;

      num = 0;
      // num of colors from mn[j][idx] to idx-1

      aFOR(k, vect) if (k != 0 && mn[j][idx] <= k) num++;

      if (vect[i - 1] != 0 && mn[j][idx] > vect[i - 1])
        num++;
      if (vect[i - 1] == 0)
        num++;

      if (num != j)
        yes = 0;
    }

    if (yes)
      poss.pb(i);
  }

  dp[idx][prevC][prev][prev2] = 0;
  aFOR(i, poss) {
    if (i == 1) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, G, B);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
    if (i == 2) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, R, B);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
    if (i == 3) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, R, G);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
  }
  return dp[idx][prevC][prev][prev2];
}

int32_t main() {
  fast;

  cin >> N >> M;

  mem(mx, -1);
  mem(mn, -1);

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

    if (mn[c][b] == -1)
      mn[c][b] = a;
    else
      mn[c][b] = min(mn[c][b], a);
    mx[c][b] = max(mx[c][b], a);
  }

  mem(dp, -1);
  cout << dpf(1, 0, 0, 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 310
#define MOD 1000000007

int dp[maxn][2][maxn][maxn];

int mn[4][maxn], mx[4][maxn];

int N, M;
vi vect;

int dpf(int idx, int prevC, int prev, int prev2) {
  if (dp[idx][prevC][prev][prev2] != -1)
    return dp[idx][prevC][prev][prev2];

  if (idx > N)
    return 1;
  int R, G, B;
  if (prevC == 1) {
    R = idx - 1;
    G = prev;
    B = prev2;
  }
  if (prevC == 2) {
    R = prev;
    G = idx - 1;
    B = prev2;
  }
  if (prevC == 3) {
    R = prev;
    G = prev2;
    B = idx - 1;
  }

  if (prevC == 0) {
    R = G = B = 0;
  }
  vect.clear();
  vect.pb(R), vect.pb(G), vect.pb(B);

  vi poss;
  // cout<<idx<<' '<<R<<' '<<G<<' '<<B<<'\n';

  bool yes;
  FOR(i, 1, 3) {
    yes = 1;
    FOR(j, 1, 3) {
      if (mx[j][idx] == -1)
        continue;
      int num = 0;
      // num of colors from mx[j][idx] to idx-1

      aFOR(k, vect) if (k != 0 && mx[j][idx] <= k) num++;

      if (vect[i - 1] != 0 && mx[j][idx] > vect[i - 1])
        num++;
      if (vect[i - 1] == 0)
        num++;

      if (num != j)
        yes = 0;

      num = 0;
      // num of colors from mn[j][idx] to idx-1

      aFOR(k, vect) if (k != 0 && mn[j][idx] <= k) num++;

      if (vect[i - 1] != 0 && mn[j][idx] > vect[i - 1])
        num++;
      if (vect[i - 1] == 0)
        num++;

      if (num != j)
        yes = 0;
    }

    if (yes)
      poss.pb(i);
  }

  dp[idx][prevC][prev][prev2] = 0;
  aFOR(i, poss) {
    if (i == 1) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, G, B);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
    if (i == 2) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, R, B);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
    if (i == 3) {
      //~ cout<<idx+1<<' '<<i<<' '<<G<<' '<<B<<'\n';
      //~ continue;
      dp[idx][prevC][prev][prev2] += dpf(idx + 1, 1, R, G);
      dp[idx][prevC][prev][prev2] %= MOD;
    }
  }
  return dp[idx][prevC][prev][prev2];
}

int32_t main() {
  fast;

  cin >> N >> M;

  mem(mx, -1);
  mem(mn, -1);

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

    if (mn[c][b] == -1)
      mn[c][b] = a;
    else
      mn[c][b] = min(mn[c][b], a);
    mx[c][b] = max(mx[c][b], a);
  }

  mem(dp, -1);
  cout << dpf(1, 0, 0, 0);
}