Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

const int MAX_N = 300;
const long long MOD = 1000000007;
int N, M;
int L[MAX_N], R[MAX_N], X[MAX_N];

std::vector<int> end_condition[MAX_N];
long long dp[MAX_N][MAX_N][MAX_N];

long long solve(int r, int g, int b) {
  if (dp[r][g][b] != -1) {
    return dp[r][g][b];
  }

  int pos = std::max(std::max(r, g), b);
  for (int i : end_condition[pos]) {
    int cnt = 0;
    if (L[i] <= r) {
      cnt++;
    }
    if (L[i] <= g) {
      cnt++;
    }
    if (L[i] <= b) {
      cnt++;
    }
    if (cnt != X[i]) {
      dp[r][g][b] = 0;
      return 0;
    }
  }

  long long ret;
  if (pos == N) {
    ret = 1;
  } else {
    ret = solve(pos + 1, g, b) + solve(r, pos + 1, b) + solve(r, g, pos + 1);
  }
  ret = ret % MOD;

  dp[r][g][b] = ret;
  return ret;
}

int main(int argc, char **argv) {
  std::cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    std::cin >> L[i] >> R[i] >> X[i];
  }

  for (int i = 1; i <= M; i++) {
    end_condition[R[i]].push_back(i);
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k < MAX_N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }

  std::cout << solve(0, 0, 0) << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

const int MAX_N = 310;
const long long MOD = 1000000007;
int N, M;
int L[MAX_N], R[MAX_N], X[MAX_N];

std::vector<int> end_condition[MAX_N];
long long dp[MAX_N][MAX_N][MAX_N];

long long solve(int r, int g, int b) {
  if (dp[r][g][b] != -1) {
    return dp[r][g][b];
  }

  int pos = std::max(std::max(r, g), b);
  for (int i : end_condition[pos]) {
    int cnt = 0;
    if (L[i] <= r) {
      cnt++;
    }
    if (L[i] <= g) {
      cnt++;
    }
    if (L[i] <= b) {
      cnt++;
    }
    if (cnt != X[i]) {
      dp[r][g][b] = 0;
      return 0;
    }
  }

  long long ret;
  if (pos == N) {
    ret = 1;
  } else {
    ret = solve(pos + 1, g, b) + solve(r, pos + 1, b) + solve(r, g, pos + 1);
  }
  ret = ret % MOD;

  dp[r][g][b] = ret;
  return ret;
}

int main(int argc, char **argv) {
  std::cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    std::cin >> L[i] >> R[i] >> X[i];
  }

  for (int i = 1; i <= M; i++) {
    end_condition[R[i]].push_back(i);
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      for (int k = 0; k < MAX_N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }

  std::cout << solve(0, 0, 0) << std::endl;

  return 0;
}