Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define ll long long
#define pb push_back
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) > (y) ? (y) : (x))
#define cls(name, x) memset(name, x, sizeof(name))
#define pos first
#define index second
#define mp make_pair
using namespace std;
const int inf = 1e9 + 10;
const ll llinf = 1e16 + 10;
const int maxn = 1e2 + 10;
const int maxm = 2e5 + 10;
const int mod = 1e9 + 7;
int n, m;
int dp[maxn][maxn]
      [maxn]; // dp[i][j][k]带表R最后出现在i,G最后出现在j,B最后出现在k的方案数
vector<pair<int, int>> v[maxn];
bool check(int posR, int posG, int posB) {
  int k = max(max(posR, posG), posB);
  for (int i = 0; i < v[k].size(); i++) {
    int l = v[k][i].first;
    int c = v[k][i].second;
    if ((posR >= l) + (posG >= l) + (posB >= l) != c)
      return false;
  }
  return true;
}
int main() {
  // freopen("in.txt","r",stdin);
  while (~scanf("%d %d", &n, &m)) {
    cls(dp, 0);
    for (int i = 1; i <= m; i++) {
      int l, r, c;
      scanf("%d %d %d", &l, &r, &c);
      v[r].pb(mp(l, c));
    }
    dp[0][0][0] = 1;
    int ans = 0;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= n; k++) {
          if (dp[i][j][k] == 0) // dp[i][j][k]==0代表这种方案不满足题意
            continue;
          if (!check(i, j, k)) {
            dp[i][j][k] = 0;
            continue;
          }
          int t = max(max(i, j), k) + 1;
          if (t - 1 == n)
            ans = (ans + dp[i][j][k]) % mod;
          dp[t][j][k] = (dp[t][j][k] + dp[i][j][k]) % mod;
          dp[i][t][k] = (dp[i][t][k] + dp[i][j][k]) % mod;
          dp[i][j][t] = (dp[i][j][t] + dp[i][j][k]) % mod;
        }
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define ll long long
#define pb push_back
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) > (y) ? (y) : (x))
#define cls(name, x) memset(name, x, sizeof(name))
#define pos first
#define index second
#define mp make_pair
using namespace std;
const int inf = 1e9 + 10;
const ll llinf = 1e16 + 10;
const int maxn = 3e2 + 10;
const int maxm = 2e5 + 10;
const int mod = 1e9 + 7;
int n, m;
int dp[maxn][maxn]
      [maxn]; // dp[i][j][k]带表R最后出现在i,G最后出现在j,B最后出现在k的方案数
vector<pair<int, int>> v[maxn];
bool check(int posR, int posG, int posB) {
  int k = max(max(posR, posG), posB);
  for (int i = 0; i < v[k].size(); i++) {
    int l = v[k][i].first;
    int c = v[k][i].second;
    if ((posR >= l) + (posG >= l) + (posB >= l) != c)
      return false;
  }
  return true;
}
int main() {
  // freopen("in.txt","r",stdin);
  while (~scanf("%d %d", &n, &m)) {
    cls(dp, 0);
    for (int i = 1; i <= m; i++) {
      int l, r, c;
      scanf("%d %d %d", &l, &r, &c);
      v[r].pb(mp(l, c));
    }
    dp[0][0][0] = 1;
    int ans = 0;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= n; k++) {
          if (dp[i][j][k] == 0) // dp[i][j][k]==0代表这种方案不满足题意
            continue;
          if (!check(i, j, k)) {
            dp[i][j][k] = 0;
            continue;
          }
          int t = max(max(i, j), k) + 1;
          if (t - 1 == n)
            ans = (ans + dp[i][j][k]) % mod;
          dp[t][j][k] = (dp[t][j][k] + dp[i][j][k]) % mod;
          dp[i][t][k] = (dp[i][t][k] + dp[i][j][k]) % mod;
          dp[i][j][t] = (dp[i][j][t] + dp[i][j][k]) % mod;
        }
    printf("%d\n", ans);
  }
  return 0;
}