Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

ll dp[330][330][330];
vector<pair<int, int>> v[330];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    v[r].push_back(make_pair(l, x));
  }

  dp[0][0][0] = 1;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      if (r == g && r != 0)
        continue;
      for (int b = 0; b <= n; b++) {
        if ((r == b || g == b) && b != 0)
          continue;
        if (dp[r][g][b] == 0)
          continue;

        int x = max(r, max(g, b));
        int z = min(r, min(g, b));
        int y = 0;
        if (x != r && z != r)
          y = r;
        else if (x != g && z != g)
          y = g;
        else if (x != b && z != b)
          y = b;

        bool ng = false;
        for (int l = 0; l < v[x].size(); l++) {
          int left = v[x][l].first;
          int type = v[x][l].second;
          if (type == 1) {
            if (y >= left) {
              ng = true;
              break;
            }
          } else if (type == 2) {
            if (z >= left || y < left) {
              ng = true;
              break;
            }
          } else {
            if (z < left) {
              ng = true;
              break;
            }
          }
        }

        if (ng)
          dp[r][g][b] = 0;
        dp[x + 1][g][b] = (dp[x + 1][g][b] + dp[r][g][b]) % MOD;
        dp[r][x + 1][b] = (dp[r][x + 1][b] + dp[r][g][b]) % MOD;
        dp[r][g][x + 1] = (dp[r][g][x + 1] + dp[r][g][b]) % MOD;
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == j && i != 0)
        continue;
      for (int k = 0; k <= n; k++) {
        if ((j == k || i == k) && k != 0)
          continue;
        int ma = max(i, max(j, k));
        if (ma == n)
          ans = (ans + dp[i][j][k]) % MOD;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int dp[330][330][330];
vector<pair<int, int>> v[330];

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    v[r].push_back(make_pair(l, x));
  }

  dp[0][0][0] = 1;
  for (int r = 0; r <= n; r++) {
    for (int g = 0; g <= n; g++) {
      if (r == g && r != 0)
        continue;
      for (int b = 0; b <= n; b++) {
        if ((r == b || g == b) && b != 0)
          continue;
        if (dp[r][g][b] == 0)
          continue;

        int x = max(r, max(g, b));
        int z = min(r, min(g, b));
        int y = 0;
        if (x != r && z != r)
          y = r;
        else if (x != g && z != g)
          y = g;
        else if (x != b && z != b)
          y = b;

        bool ng = false;
        for (int l = 0; l < v[x].size(); l++) {
          int left = v[x][l].first;
          int type = v[x][l].second;
          if (type == 1) {
            if (y >= left) {
              ng = true;
              break;
            }
          } else if (type == 2) {
            if (z >= left || y < left) {
              ng = true;
              break;
            }
          } else {
            if (z < left) {
              ng = true;
              break;
            }
          }
        }

        if (ng)
          dp[r][g][b] = 0;
        dp[x + 1][g][b] = (dp[x + 1][g][b] + dp[r][g][b]) % MOD;
        dp[r][x + 1][b] = (dp[r][x + 1][b] + dp[r][g][b]) % MOD;
        dp[r][g][x + 1] = (dp[r][g][x + 1] + dp[r][g][b]) % MOD;
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == j && i != 0)
        continue;
      for (int k = 0; k <= n; k++) {
        if ((j == k || i == k) && k != 0)
          continue;
        int ma = max(i, max(j, k));
        if (ma == n)
          ans = (ans + dp[i][j][k]) % MOD;
      }
    }
  }
  cout << ans << endl;

  return 0;
}